program Cohen_Sutherland;
uses crt, graph;

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

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

var
   grDriver: Integer;
   grMode: Integer;
   ErrCode: Integer;
   kod1, kod2: integer;
   br:integer;

   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 zacatekkonec(kod1,kod2:integer);
begin
     if ((kod1 or kod2)=0) then begin writeln ('usecka je cela uvnitr, ', kod1, kod2, kod1 and kod2); end
     else 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);
     end;
end;

procedure orizni;
var a,b,pom:real;
begin
     writeln('bod oriyni',bod1.x, ' ', bod2.x);

     a:=(bod2.y-bod1.y)/(bod2.x-bod1.x);
     {b:=((bod2.x*bod1.y)-(bod1.x*bod2.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
        writeln('pred ', bod1.x);
        bod1.x:=hranice.xmin;
        writeln('po ', bod1.x);
        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*bod2.x + b);
        outtext('3');
     end;
     if (bod2.x>hranice.xmax) then begin
        bod2.x:=hranice.xmax;
        {bod2.y:=round((a*bod2.x));
        }
        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;

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;
        bod1.x:=70; bod1.y:=350; bod2.x:=350; bod2.y:=370;
    {  bod1.x:=50; bod1.y:=300; bod2.x:=500; bod2.y:=400;  }
        NastavHranice(50,200); NastavHranice(300,400);
        setcolor(blue);  setbkcolor(black);
        rectangle(hranice.xmin, hranice.ymin, hranice.xmax, hranice.ymax);

        setcolor(blue);
        line(bod1.x, bod1.y, bod2.x, bod2.y);
        kod1:=KodBodu(bod1);
        kod2:=KodBodu(bod2);
        writeln (kod1, ' ' , kod2);
        zacatekkonec(kod1,kod2);
        orizni;
        setcolor(red);
        line(bod1.x, bod1.y, bod2.x, bod2.y);




        readln;
        CloseGraph;
        writeln (hranice.xmin, ' ', hranice.ymin, ' ', hranice.xmax, ' ', hranice.ymax);
        readln;
     end
     else
         Writeln('Graphics error:', GraphErrorMsg(ErrCode));

end.
