Find crosspoint of two lines (Views: 30)
Problem/Question/Abstract: How to calculate the crosspoint of two lines Answer: If you want to know if 2 Lines crossing each other, you can use this function below. The lines are given in with the beginning- and end-points. You will find the Coordinates of the Crosspoint in Cross, if the Function anwers True = we did found a crossing; function GetCrossPointOfLines(pA1, pA2, pB1, pB2: TPoint; var Cross: TPoint): Boolean; var h, i, j, k, l, m: Integer; o, p: Extended; begin k := pB1.Y - pA1.Y; l := pA2.Y - pA1.Y; m := pB2.Y - pB1.Y; h := pA2.X - pA1.X; i := pB1.X - pA1.X; j := pB2.X - pB1.x; Result := false; if Abs(j * l - m * h) > 0 then begin p := (k * h - i * l) / (j * l - m * h); o := (k * j - i * m) / (j * l - m * h); if (o >= 0.0) and (o <= 1.0) and (p >= 0.0) and (p <= 1.0) then begin Cross.X := Round(pA1.X + o * h); Cross.Y := Round(pA1.Y + o * l); Result := True; end; end; end; |