22
33(* ******************************************************************************
44* Author : Angus Johnson *
5- * Date : 17 July 2023 *
5+ * Date : 14 February 2024 *
66* Website : http://www.angusj.com *
7- * Copyright : Angus Johnson 2010-2023 *
7+ * Copyright : Angus Johnson 2010-2024 *
88* Purpose : Core Clipper Library module *
99* Contains structures and functions used throughout the library *
1010* License : http://www.boost.org/LICENSE_1_0.txt *
@@ -64,6 +64,7 @@ TPointD = record
6464 function GetWidth : Int64; { $IFDEF INLINING} inline; { $ENDIF}
6565 function GetHeight : Int64; { $IFDEF INLINING} inline; { $ENDIF}
6666 function GetIsEmpty : Boolean; { $IFDEF INLINING} inline; { $ENDIF}
67+ function GetIsValid : Boolean; { $IFDEF INLINING} inline; { $ENDIF}
6768 function GetMidPoint : TPoint64; { $IFDEF INLINING} inline; { $ENDIF}
6869 public
6970 Left : Int64;
@@ -78,6 +79,7 @@ TPointD = record
7879 property Width: Int64 read GetWidth;
7980 property Height: Int64 read GetHeight;
8081 property IsEmpty: Boolean read GetIsEmpty;
82+ property IsValid: Boolean read GetIsValid;
8183 property MidPoint: TPoint64 read GetMidPoint;
8284 end ;
8385
@@ -86,6 +88,7 @@ TPointD = record
8688 function GetWidth : double; { $IFDEF INLINING} inline; { $ENDIF}
8789 function GetHeight : double; { $IFDEF INLINING} inline; { $ENDIF}
8890 function GetIsEmpty : Boolean; { $IFDEF INLINING} inline; { $ENDIF}
91+ function GetIsValid : Boolean; { $IFDEF INLINING} inline; { $ENDIF}
8992 function GetMidPoint : TPointD; { $IFDEF INLINING} inline; { $ENDIF}
9093 public
9194 Left : double;
@@ -99,6 +102,7 @@ TPointD = record
99102 property Width: double read GetWidth;
100103 property Height: double read GetHeight;
101104 property IsEmpty: Boolean read GetIsEmpty;
105+ property IsValid: Boolean read GetIsValid;
102106 property MidPoint: TPointD read GetMidPoint;
103107 end ;
104108
@@ -168,8 +172,8 @@ function DistanceSqr(const pt1, pt2: TPoint64): double; overload;
168172 { $IFDEF INLINING} inline; { $ENDIF}
169173function DistanceSqr (const pt1, pt2: TPointD): double; overload;
170174 { $IFDEF INLINING} inline; { $ENDIF}
171- function DistanceFromLineSqrd (const pt, linePt1, linePt2: TPoint64): double; overload;
172- function DistanceFromLineSqrd (const pt, linePt1, linePt2: TPointD): double; overload;
175+ function PerpendicDistFromLineSqrd (const pt, linePt1, linePt2: TPoint64): double; overload;
176+ function PerpendicDistFromLineSqrd (const pt, linePt1, linePt2: TPointD): double; overload;
173177
174178function SegmentsIntersect (const s1a, s1b, s2a, s2b: TPoint64;
175179 inclusive: Boolean = false): boolean; { $IFDEF INLINING} inline; { $ENDIF}
@@ -311,7 +315,7 @@ procedure AppendPaths(var paths: TPathsD; const extra: TPathsD); overload;
311315
312316function ArrayOfPathsToPaths (const ap: TArrayOfPaths): TPaths64;
313317
314- function GetIntersectPoint (const ln1a, ln1b, ln2a, ln2b: TPoint64;
318+ function GetSegmentIntersectPt (const ln1a, ln1b, ln2a, ln2b: TPoint64;
315319 out ip: TPoint64): Boolean;
316320
317321function PointInPolygon (const pt: TPoint64; const polygon: TPath64): TPointInPolygonResult;
@@ -333,8 +337,14 @@ procedure QuickSort(SortList: TPointerList;
333337
334338procedure CheckPrecisionRange (var precision: integer);
335339
340+ function Iif (eval: Boolean; trueVal, falseVal: Boolean): Boolean; overload;
341+ function Iif (eval: Boolean; trueVal, falseVal: integer): integer; overload;
342+ function Iif (eval: Boolean; trueVal, falseVal: Int64): Int64; overload;
343+ function Iif (eval: Boolean; trueVal, falseVal: double): double; overload;
344+
336345const
337346 MaxInt64 = 9223372036854775807 ;
347+ MinInt64 = -MaxInt64;
338348 MaxCoord = MaxInt64 div 4 ;
339349 MinCoord = - MaxCoord;
340350 invalid64 = MaxInt64;
@@ -346,6 +356,11 @@ procedure CheckPrecisionRange(var precision: integer);
346356 InvalidPtD : TPointD = (X: invalidD; Y: invalidD);
347357
348358 NullRectD : TRectD = (left: 0 ; top: 0 ; right: 0 ; Bottom: 0 );
359+ InvalidRect64 : TRect64 =
360+ (left: invalid64; top: invalid64; right: invalid64; bottom: invalid64);
361+ InvalidRectD : TRectD =
362+ (left: invalidD; top: invalidD; right: invalidD; bottom: invalidD);
363+
349364 Tolerance : Double = 1.0E-12 ;
350365
351366 // https://github.com/AngusJohnson/Clipper2/discussions/564
@@ -378,6 +393,12 @@ function TRect64.GetIsEmpty: Boolean;
378393end ;
379394// ------------------------------------------------------------------------------
380395
396+ function TRect64.GetIsValid : Boolean;
397+ begin
398+ result := left <> invalid64;
399+ end ;
400+ // ------------------------------------------------------------------------------
401+
381402function TRect64.GetMidPoint : TPoint64;
382403begin
383404 result := Point64((Left + Right) div 2 , (Top + Bottom) div 2 );
@@ -450,6 +471,12 @@ function TRectD.GetIsEmpty: Boolean;
450471end ;
451472// ------------------------------------------------------------------------------
452473
474+ function TRectD.GetIsValid : Boolean;
475+ begin
476+ result := left <> invalidD;
477+ end ;
478+ // ------------------------------------------------------------------------------
479+
453480function TRectD.GetMidPoint : TPointD;
454481begin
455482 result := PointD((Left + Right) *0.5 , (Top + Bottom) *0.5 );
@@ -633,6 +660,34 @@ procedure TListEx.Swap(idx1, idx2: integer);
633660// Miscellaneous Functions ...
634661// ------------------------------------------------------------------------------
635662
663+ function Iif (eval: Boolean; trueVal, falseVal: Boolean): Boolean;
664+ { $IFDEF INLINING} inline; { $ENDIF}
665+ begin
666+ if eval then Result := trueVal else Result := falseVal;
667+ end ;
668+ // ------------------------------------------------------------------------------
669+
670+ function Iif (eval: Boolean; trueVal, falseVal: integer): integer;
671+ { $IFDEF INLINING} inline; { $ENDIF}
672+ begin
673+ if eval then Result := trueVal else Result := falseVal;
674+ end ;
675+ // ------------------------------------------------------------------------------
676+
677+ function Iif (eval: Boolean; trueVal, falseVal: Int64): Int64;
678+ { $IFDEF INLINING} inline; { $ENDIF}
679+ begin
680+ if eval then Result := trueVal else Result := falseVal;
681+ end ;
682+ // ------------------------------------------------------------------------------
683+
684+ function Iif (eval: Boolean; trueVal, falseVal: double): double;
685+ { $IFDEF INLINING} inline; { $ENDIF}
686+ begin
687+ if eval then Result := trueVal else Result := falseVal;
688+ end ;
689+ // ------------------------------------------------------------------------------
690+
636691procedure CheckPrecisionRange (var precision: integer);
637692begin
638693 if (precision < -MaxDecimalPrecision) or (precision > MaxDecimalPrecision) then
@@ -1831,7 +1886,7 @@ function DistanceSqr(const pt1, pt2: TPointD): double;
18311886end ;
18321887// ------------------------------------------------------------------------------
18331888
1834- function DistanceFromLineSqrd (const pt, linePt1, linePt2: TPoint64): double;
1889+ function PerpendicDistFromLineSqrd (const pt, linePt1, linePt2: TPoint64): double;
18351890var
18361891 a,b,c: double;
18371892begin
@@ -1842,19 +1897,23 @@ function DistanceFromLineSqrd(const pt, linePt1, linePt2: TPoint64): double;
18421897 b := (linePt2.X - linePt1.X);
18431898 c := a * linePt1.X + b * linePt1.Y;
18441899 c := a * pt.x + b * pt.y - c;
1845- Result := (c * c) / (a * a + b * b);
1900+ if (a = 0 ) and (b = 0 ) then
1901+ Result := 0 else
1902+ Result := (c * c) / (a * a + b * b);
18461903end ;
18471904// ---------------------------------------------------------------------------
18481905
1849- function DistanceFromLineSqrd (const pt, linePt1, linePt2: TPointD): double;
1906+ function PerpendicDistFromLineSqrd (const pt, linePt1, linePt2: TPointD): double;
18501907var
18511908 a,b,c: double;
18521909begin
18531910 a := (linePt1.Y - linePt2.Y);
18541911 b := (linePt2.X - linePt1.X);
18551912 c := a * linePt1.X + b * linePt1.Y;
18561913 c := a * pt.x + b * pt.y - c;
1857- Result := (c * c) / (a * a + b * b);
1914+ if (a = 0 ) and (b = 0 ) then
1915+ Result := 0 else
1916+ Result := (c * c) / (a * a + b * b);
18581917end ;
18591918// ---------------------------------------------------------------------------
18601919
@@ -1934,7 +1993,7 @@ function __Trunc(val: double): Int64; {$IFDEF INLINE} inline; {$ENDIF}
19341993end ;
19351994// ------------------------------------------------------------------------------
19361995
1937- function GetIntersectPoint (const ln1a, ln1b, ln2a, ln2b: TPoint64;
1996+ function GetSegmentIntersectPt (const ln1a, ln1b, ln2a, ln2b: TPoint64;
19381997 out ip: TPoint64): Boolean;
19391998var
19401999 dx1,dy1, dx2,dy2, t, cp: double;
@@ -2119,20 +2178,6 @@ function GetClosestPointOnSegment(const pt, seg1, seg2: TPoint64): TPoint64;
21192178end ;
21202179// ------------------------------------------------------------------------------
21212180
2122- function PerpendicDistFromLineSqrd (const pt, line1, line2: TPoint64): double; overload;
2123- var
2124- a,b,c,d: double;
2125- begin
2126- a := pt.X - line1.X;
2127- b := pt.Y - line1.Y;
2128- c := line2.X - line1.X;
2129- d := line2.Y - line1.Y;
2130- if (c = 0 ) and (d = 0 ) then
2131- result := 0 else
2132- result := Sqr(a * d - c * b) / (c * c + d * d);
2133- end ;
2134- // ------------------------------------------------------------------------------
2135-
21362181procedure RDP (const path: TPath64; startIdx, endIdx: integer;
21372182 epsilonSqrd: double; var boolArray: TArrayOfBoolean); overload;
21382183var
@@ -2162,20 +2207,6 @@ procedure RDP(const path: TPath64; startIdx, endIdx: integer;
21622207end ;
21632208// ------------------------------------------------------------------------------
21642209
2165- function PerpendicDistFromLineSqrd (const pt, line1, line2: TPointD): double; overload;
2166- var
2167- a,b,c,d: double;
2168- begin
2169- a := pt.X - line1.X;
2170- b := pt.Y - line1.Y;
2171- c := line2.X - line1.X;
2172- d := line2.Y - line1.Y;
2173- if (c = 0 ) and (d = 0 ) then
2174- result := 0 else
2175- result := Sqr(a * d - c * b) / (c * c + d * d);
2176- end ;
2177- // ------------------------------------------------------------------------------
2178-
21792210procedure RDP (const path: TPathD; startIdx, endIdx: integer;
21802211 epsilonSqrd: double; var boolArray: TArrayOfBoolean); overload;
21812212var
0 commit comments