Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- {$mode objfpc}{$H+}
- interface
- type
- TPointF = packed record
- x, y: single;
- end;
- TLineDef = record
- origin, dir: TPointF;
- end;
- function IntersectLine(line1, line2: TLineDef): TPointF;
- function IntersectLine(line1, line2: TLineDef; out parallel: boolean): TPointF;
- implementation
- function IntersectLine(line1, line2: TLineDef): TPointF;
- var
- parallel: boolean;
- begin
- Result := IntersectLine(line1, line2, parallel);
- end;
- function IntersectLine(line1, line2: TLineDef; out parallel: boolean): TPointF;
- var
- divFactor: double;
- begin
- parallel := False;
- //if lines are parallel
- if ((line1.dir.x = line2.dir.x) and (line1.dir.y = line2.dir.y)) or
- ((abs(line1.dir.y) < 1e-6) and (abs(line2.dir.y) < 1e-6)) then
- begin
- parallel := True;
- //return the center of the segment between line origins
- Result.x := (line1.origin.x + line2.origin.x) / 2;
- Result.y := (line1.origin.y + line2.origin.y) / 2;
- end
- else
- if abs(line1.dir.y) < 1e-6 then //line1 is horizontal
- begin
- Result.y := line1.origin.y;
- Result.x := line2.origin.x + (Result.y - line2.origin.y) /
- line2.dir.y * line2.dir.x;
- end
- else
- if abs(line2.dir.y) < 1e-6 then //line2 is horizontal
- begin
- Result.y := line2.origin.y;
- Result.x := line1.origin.x + (Result.y - line1.origin.y) /
- line1.dir.y * line1.dir.x;
- end
- else
- begin
- divFactor := line1.dir.x / line1.dir.y - line2.dir.x / line2.dir.y;
- if abs(divFactor) < 1e-6 then //almost parallel
- begin
- parallel := True;
- //return the center of the segment between line origins
- Result.x := (line1.origin.x + line2.origin.x) / 2;
- Result.y := (line1.origin.y + line2.origin.y) / 2;
- end
- else
- begin
- Result.y := (line2.origin.x - line1.origin.x + line1.origin.y *
- line1.dir.x / line1.dir.y - line2.origin.y * line2.dir.x /
- line2.dir.y) / divFactor;
- Result.x := line1.origin.x + (Result.y - line1.origin.y) /
- line1.dir.y * line1.dir.x;
- end;
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement