program Cohen_Sutherland;
uses crt, graph;

type TBod = record
     x, y: integer;
end;

type THranice = record
     xmin, ymin, xmax, ymax: integer;
end;

const
  soubor='soubor.txt';

var
   grDriver: Integer;
   grMode: Integer;
   ErrCode: Integer;
   kod1, kod2: integer;
   x1,x2,y1,y2,pocet,i:integer;
   f:text;
   bod1,bod2: TBod;
   hranice: THranice;


procedure NastavHranice(x,y:integer);
begin
     if (hranice.xmin>x) then begin
        hranice.xmin:=x;
        end
        else hranice.xmax:=x;
     if (hranice.ymin>y) then begin
        hranice.ymin:=y;
        end
        else hranice.ymax:=y;
end;

function KodBodu(bod:TBod):integer;
begin
     KodBodu:=0;
     if (bod.y>hranice.ymax) then begin  {0}
        if (bod.x<hranice.xmin) then begin KodBodu:=10; end
        else if (bod.x>hranice.xmax) then begin KodBodu:=6; end
        else KodBodu:=2;
     end;

     if ((bod.y<=hranice.ymax) and (bod.y>=hranice.ymin)) then begin {1}
        if (bod.x<hranice.xmin) then begin KodBodu:=8; end
        else if (bod.x>hranice.xmax) then begin KodBodu:=4 end
        else KodBodu:=0;
     end;

     if (bod.y<hranice.ymin) then begin {2}
        if (bod.x<hranice.xmin) then begin KodBodu:=9; end
        else if (bod.x>hranice.xmax) then begin KodBodu:=5; end
        else KodBodu:=1;
     end;

end;



procedure orizni;
var a,b,pom:real;
begin

     a:=(bod2.y-bod1.y)/(bod2.x-bod1.x);
     b:=((bod2.x*bod1.y)-(bod1.x*bod2.y))/(bod2.x-bod1.x);

     if (bod1.x<hranice.xmin) then begin
        bod1.x:=hranice.xmin;
        bod1.y:=round (a*bod1.x+b);
        outtext('1');
     end;
     if (bod1.x>hranice.xmax) then begin
        bod1.x:=hranice.xmax;
        bod1.y:=round(a*bod1.x + b);
        outtext('2');
    end;

    if (bod2.x<hranice.xmin) then begin
        bod2.x:=hranice.xmin;
        bod2.y:=round(a*(hranice.xmax-bod1.x))+bod1.y;
        outtext('3');
     end;
     if (bod2.x>hranice.xmax) then begin
        bod2.x:=hranice.xmax;
        bod2.y:=round(a*(hranice.xmax-bod1.x))+bod1.y;
        outtext('4');
    end;


     if (bod1.y<hranice.ymin) then begin
        bod1.y:=hranice.ymin;
        bod1.x:=round((bod1.y-b)/a);
        outtext('5');
     end;
     if (bod1.y>hranice.ymax) then begin
        bod1.y:=hranice.ymax;
        bod1.x:=round((bod1.y-b)/a);
        outtext('6');
    end;
    if (bod2.y<hranice.ymin) then begin
        bod2.y:=hranice.ymin;
        bod2.x:=round((bod2.y-b)/a);
        outtext('7');
    end;
    if (bod2.y>hranice.ymax) then begin
        bod2.y:=hranice.ymax;
        bod2.x:=round((bod2.y-b)/a);
        outtext('8');
    end;
end;


procedure volba(kod1,kod2:integer);
begin
     if ((kod1 or kod2)=0) then begin writeln ('usecka je cela uvnitr, ', kod1, kod2, kod1 and kod2); end;
     if ((kod1 and kod2)<>0) then begin writeln ('usecka je cela mimo, ', kod1, kod2,kod1 and kod2); end
     else begin
          writeln ('nutny dalsi orez, ', kod1, kod2, kod1 and kod2);
          orizni;
     end;
end;

begin
     grDriver := Detect;
     InitGraph(grDriver, grMode,' ');
     ErrCode := GraphResult;
     if ErrCode = grOk then begin
        hranice.xmin:=GetMaxX; hranice.xmax:=GetMaxX;
        hranice.ymin:=GetMaxY; hranice.ymax:=GetMaxY;
        setcolor(red);
        NastavHranice(100,150); NastavHranice(400,300);
        rectangle(hranice.xmin, hranice.ymin, hranice.xmax, hranice.ymax);

        assign(f, soubor);
        reset(f);

        while not Eof(f) do begin
            setcolor(white);
            readln(f,x1,y1,x2, y2);
            bod1.x:=x1; bod2.x:=x2; bod1.y:=y1; bod2.y:=y2;
            line(bod1.x,bod1.y, bod2.x, bod2.y);
            kod1:=KodBodu(bod1);
            kod2:=KodBodu(bod2);
            volba(kod1,kod2);
            setcolor(green);
            line(bod1.x, bod1.y, bod2.x, bod2.y);
        end;
        close(f);
        readln;
        CloseGraph;
     end
     else
         Writeln('chyba grafiky:', GraphErrorMsg(ErrCode));

end.
