Finds where LineA and LineB intersect. I'm keeping them as TBoxs since there isn't anything called TLine... at least not that I know of. Maybe TPoints would be better but this served it purpose so I thought I'd share it with the community.
SCAR Code:
program New;
var
Pt: TPoint;
function Intersect(LineA, LineB: TBox): TPoint;
var
A, B, C: array [0..1] of Integer;
Det: Integer;
begin
A[0]:= LineA.y2 - LineA.y1;
B[0]:= LineA.x1 - LineA.x2;
C[0]:= (A[0] * LineA.x1) + (B[0] * LineA.y1);
A[1]:= LineB.y2 - LineB.y1;
B[1]:= LineB.x1 - LineB.x2;
C[1]:= (A[1] * LineB.x1) + (B[1] * LineB.y1);
Det:= (A[0] * B[1]) - (A[1] * B[0]);
if(Det <> 0)then
begin
Result.x:= ((B[1] * C[0]) - (B[0] * C[1])) div Det;
Result.y:= ((A[0] * C[1]) - (A[1] * C[0])) div Det;
end else
begin
Result.x:= -1;
Result.y:= -1;
end;
end;
begin
Pt:= Intersect(IntToBox(0, 0, 4, 4), IntToBox(4, 0, 0, 4));
Writeln('x:= ' + inttostr(Pt.x));
Writeln('y:= ' + inttostr(Pt.y));
end.