...
type TPunto = record
x,y: Double;
end;
type TSolucion = record
Interseccion: Boolean;
X,Y: Double;
end;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
public
function LineIntersection(A, B, C, D:TPunto):TSolucion;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.LineIntersection(A, B, C, D:TPunto):TSolucion;
var
a1, b1, c1, a2, b2, c2, det:double;
begin
a1 := B.y - A.y;
b1 := A.x - B.x;
c1 := a1*(A.x) + b1*(A.y);
a2 := D.y - C.y;
b2 := C.x - D.x;
c2 := a2*(C.x)+ b2*(C.y);
det := a1*b2 - a2*b1;
Result.Interseccion:=(det <> 0);
Result.x:=-1;
Result.y:=-1;
if Result.Interseccion then
begin
Result.x := (b2*c1 - b1*c2)/det;
Result.y := (a1*c2 - a2*c1)/det;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
A, B, C, D:TPunto;
solucion:TSolucion;
begin
A.X:=1; A.Y:=2;
B.X:=4; B.Y:=5;
C.X:=2; C.Y:=4;
D.X:=5; D.Y:=4;
Solucion:=LineIntersection(A, B, C, D);
if not Solucion.Interseccion then
Showmessage('Rectas paralelas ')
else
ShowMessage(format('Las rectas de intersectan en el punto (%f,%f)',[Solucion.X, Solucion.Y]));
end;
end.