Changeset 49 for trunk/UGeometric.pas


Ignore:
Timestamp:
Nov 26, 2017, 12:32:16 AM (6 years ago)
Author:
chronos
Message:
  • Modified: Use generic object list instead of TObjectList.
  • Fixed: Avoid division by zero in line intersection calculation.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGeometric.pas

    r44 r49  
    1919function RotatePoint(Center, P: TPoint; Angle: Double): TPoint;
    2020function RotatePoints(Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
    21 function LineIntersect(LineAP1, LineAP2, LineBP1, LineBP2: TPoint): TPoint;
     21function LineIntersect(LineAP1, LineAP2, LineBP1, LineBP2: TPoint;
     22  out Intersection: TPoint): Boolean;
    2223function ArcTan2Point(Point: TPoint): Float;
    2324function ArcTanPoint(Point: TPoint): Float;
     
    100101end;
    101102
    102 function LineIntersect(LineAP1, LineAP2, LineBP1, LineBP2: TPoint): TPoint;
     103function LineIntersect(LineAP1, LineAP2, LineBP1, LineBP2: TPoint;
     104  out Intersection: TPoint): Boolean;
    103105Var
    104106  LDetLineA, LDetLineB, LDetDivInv: Double;
    105107  LDiffLA, LDiffLB: TPoint;
     108  D: Double;
    106109begin
     110  if (LineAP1 = LineAP2) or (LineBP1 = LineBP2) then begin
     111    Result := False;
     112    Exit;
     113  end;
    107114  LDetLineA := LineAP1.X * LineAP2.Y - LineAP1.Y * LineAP2.X;
    108115  LDetLineB := LineBP1.X * LineBP2.Y - LineBP1.Y * LineBP2.X;
     
    111118  LDiffLB := SubPoint(LineBP1, LineBP2);
    112119
    113   LDetDivInv := 1 / ((LDiffLA.X * LDiffLB.Y) - (LDiffLA.Y * LDiffLB.X));
     120  D := ((LDiffLA.X * LDiffLB.Y) - (LDiffLA.Y * LDiffLB.X));
     121  if D = 0 then begin
     122    // Parallel lines without intersection
     123    Result := False;
     124    Exit;
     125  end;
     126  LDetDivInv := 1 / D;
    114127
    115   Result.X := Trunc(((LDetLineA * LDiffLB.X) - (LDiffLA.X * LDetLineB)) * LDetDivInv);
    116   Result.Y := Trunc(((LDetLineA * LDiffLB.Y) - (LDiffLA.Y * LDetLineB)) * LDetDivInv);
     128  Intersection.X := Trunc(((LDetLineA * LDiffLB.X) - (LDiffLA.X * LDetLineB)) * LDetDivInv);
     129  Intersection.Y := Trunc(((LDetLineA * LDiffLB.Y) - (LDiffLA.Y * LDetLineB)) * LDetDivInv);
     130  Result := True;
    117131end;
    118132
Note: See TracChangeset for help on using the changeset viewer.