Advertisement
Guest User

Untitled

a guest
Feb 18th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.07 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. type
  8.   TPointF = packed record
  9.     x, y: single;
  10.   end;
  11.  
  12.   TLineDef = record
  13.     origin, dir: TPointF;
  14.   end;
  15.  
  16. function IntersectLine(line1, line2: TLineDef): TPointF;
  17. function IntersectLine(line1, line2: TLineDef; out parallel: boolean): TPointF;
  18.  
  19. implementation
  20.  
  21. function IntersectLine(line1, line2: TLineDef): TPointF;
  22. var
  23.   parallel: boolean;
  24. begin
  25.   Result := IntersectLine(line1, line2, parallel);
  26. end;
  27.  
  28. function IntersectLine(line1, line2: TLineDef; out parallel: boolean): TPointF;
  29. var
  30.   divFactor: double;
  31. begin
  32.   parallel := False;
  33.   //if lines are parallel
  34.   if ((line1.dir.x = line2.dir.x) and (line1.dir.y = line2.dir.y)) or
  35.     ((abs(line1.dir.y) < 1e-6) and (abs(line2.dir.y) < 1e-6)) then
  36.   begin
  37.     parallel := True;
  38.     //return the center of the segment between line origins
  39.     Result.x := (line1.origin.x + line2.origin.x) / 2;
  40.     Result.y := (line1.origin.y + line2.origin.y) / 2;
  41.   end
  42.   else
  43.   if abs(line1.dir.y) < 1e-6 then //line1 is horizontal
  44.   begin
  45.     Result.y := line1.origin.y;
  46.     Result.x := line2.origin.x + (Result.y - line2.origin.y) /
  47.       line2.dir.y * line2.dir.x;
  48.   end
  49.   else
  50.   if abs(line2.dir.y) < 1e-6 then //line2 is horizontal
  51.   begin
  52.     Result.y := line2.origin.y;
  53.     Result.x := line1.origin.x + (Result.y - line1.origin.y) /
  54.       line1.dir.y * line1.dir.x;
  55.   end
  56.   else
  57.   begin
  58.     divFactor := line1.dir.x / line1.dir.y - line2.dir.x / line2.dir.y;
  59.     if abs(divFactor) < 1e-6 then //almost parallel
  60.     begin
  61.       parallel := True;
  62.       //return the center of the segment between line origins
  63.       Result.x := (line1.origin.x + line2.origin.x) / 2;
  64.       Result.y := (line1.origin.y + line2.origin.y) / 2;
  65.     end
  66.     else
  67.     begin
  68.       Result.y := (line2.origin.x - line1.origin.x + line1.origin.y *
  69.         line1.dir.x / line1.dir.y - line2.origin.y * line2.dir.x /
  70.         line2.dir.y) / divFactor;
  71.       Result.x := line1.origin.x + (Result.y - line1.origin.y) /
  72.         line1.dir.y * line1.dir.x;
  73.     end;
  74.   end;
  75. end;
  76.  
  77. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement