Unit Math;
Interface
Uses TypeLib;
{---------------------------------------------------------------}
function min(a,b : real) : real;
{}
function max(a,b : real) : real;
{}
function RayCrossL(Ray,L : TRay; var P : TPoint) :boolean;
{Функция возвращает true если луч Ray пересекает отрезок L;
P- Координаты пересечения}
{---------------------------------------------------------------}
Implementation
{---------------------------------------------------------------}
function min;
begin
 if a>b then min:=b
 else min:=a;
end;

function max;
begin
 if a>b then max:=a
 else max:=b;
end;

function RayCrossL;
var
 a1,b1,c1  : real;
 a2,b2,c2  : real;
 x,y       : real;
begin
 RayCrossL:=false;
 with Ray do begin a1:=y2-y1; b1:=x1-x2; c1:=-x1*a1-y1*b1; end;
 with L do begin a2:=y2-y1; b2:=x1-x2; c2:=-x1*a2-y1*b2; end;
 if A2*B1-A1*B2=0 then exit;
 x:=(B1*C2-B2*C1)/(B2*A1-B1*A2);
 y:=(a1*c2-a2*c1)/(a2*b1-a1*b2);
 {Проверяем на принадлежность точки пересечения отрезку}
 with L do
  if not ((((x2>=x1) and (x2>=x) and (x>=x1)) or ((x2<x1) and (x2<x) and (x<x1)))
      and (((y2>=y1) and (y2>=y) and (y>=y1)) or ((y2<y1) and (y2<y) and (y<y1))))
        then Exit;
 {Проверяем на принадлежность лучу}
 with Ray do
  if not ((((x2>=x1) and (x2>=x) and (x>=x1)) or ((x2<x1) and (x2<x) and (x<x1)))
      and (((y2>=y1) and (y2>=y) and (y>=y1)) or ((y2<y1) and (y2<y) and (y<y1))))
        then Exit;
 P.x:=x; P.y:=y;
 RayCrossL:=true;
end;
{---------------------------------------------------------------}
End.