22
33(* ******************************************************************************
44* Author : Angus Johnson *
5- * Date : 10 October 2025 *
5+ * Date : 11 October 2025 *
66* Website : https://www.angusj.com *
77* Copyright : Angus Johnson 2010-2024 *
88* Purpose : Core Clipper Library module *
@@ -334,8 +334,10 @@ procedure AppendPaths(var paths: TPathsD; const extra: TPathsD); overload;
334334
335335function ArrayOfPathsToPaths (const ap: TArrayOfPaths): TPaths64;
336336
337- function GetSegmentIntersectPt (const ln1a, ln1b, ln2a, ln2b: TPoint64;
338- out ip: TPoint64): Boolean;
337+ function GetLineIntersectPt (const ln1a, ln1b, ln2a, ln2b: TPoint64;
338+ out ip: TPoint64): Boolean; overload;
339+ function GetLineIntersectPt (const ln1a, ln1b, ln2a, ln2b: TPointD;
340+ out ip: TPointD): Boolean; overload;
339341
340342function PointInPolygon (const pt: TPoint64; const polygon: TPath64): TPointInPolygonResult;
341343function Path2ContainsPath1 (const path1, path2: TPath64): Boolean;
@@ -2147,18 +2149,18 @@ function DistanceSqr(const pt1, pt2: TPointD): double;
21472149
21482150function PerpendicDistFromLineSqrd (const pt, linePt1, linePt2: TPoint64): double;
21492151var
2150- a,b,c: double;
2152+ a,b,c,d : double;
21512153begin
21522154 // perpendicular distance of point (x0,y0) = (a*x0 + b*y0 + C)/Sqrt(a*a + b*b)
2153- // where ax + by +c = 0 is the equation of the line
2155+ // where ax + by +c = 0 is the equation of a line
21542156 // see https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
2155- a := (linePt1.Y - linePt2.Y) ;
2156- b := (linePt2.X - linePt1.X) ;
2157- c := a * linePt1.X + b * linePt1.Y ;
2158- c := a * pt.x + b * pt. y - c ;
2159- if (a = 0 ) and (b = 0 ) then
2157+ a := pt.x - linePt1.x ;
2158+ b := pt.y - linePt1.y ;
2159+ c := linePt2.x - linePt1.x ;
2160+ d := linePt2. y - linePt1.y ;
2161+ if (c = 0 ) and (d = 0 ) then
21602162 Result := 0 else
2161- Result := (c * c ) / (a * a + b * b );
2163+ Result := Sqr(a * d - c * b ) / (c * c + d * d );
21622164end ;
21632165// ---------------------------------------------------------------------------
21642166
@@ -2247,7 +2249,7 @@ function SegmentsIntersect(const s1a, s1b, s2a, s2b: TPoint64;
22472249 else
22482250 Result := (cp < 0 ) and (t > cp);
22492251 if not Result then Exit;
2250- t := ((s1a.x-s2a.x) * dy1 - (s1a.y-s2a.y) * dx1) / cp ;
2252+ t := ((s1a.x-s2a.x) * dy1 - (s1a.y-s2a.y) * dx1);
22512253 if (t = 0 ) then Result := false
22522254 else if (t > 0 ) then
22532255 Result := (cp > 0 ) and (t < cp)
@@ -2257,7 +2259,7 @@ function SegmentsIntersect(const s1a, s1b, s2a, s2b: TPoint64;
22572259end ;
22582260// ------------------------------------------------------------------------------
22592261
2260- function GetSegmentIntersectPt (const ln1a, ln1b, ln2a, ln2b: TPoint64;
2262+ function GetLineIntersectPt (const ln1a, ln1b, ln2a, ln2b: TPoint64;
22612263 out ip: TPoint64): Boolean;
22622264var
22632265 dx1,dy1, dx2,dy2, t, cp: double;
@@ -2272,12 +2274,41 @@ function GetSegmentIntersectPt(const ln1a, ln1b, ln2a, ln2b: TPoint64;
22722274 if not Result then Exit;
22732275 t := ((ln1a.x-ln2a.x) * dy2 - (ln1a.y-ln2a.y) * dx2) / cp;
22742276 if t <= 0.0 then ip := ln1a
2275- else if t >= 1.0 then ip := ln1b;
2276- ip.X := Trunc(ln1a.X + t * dx1);
2277- ip.Y := Trunc(ln1a.Y + t * dy1);
2277+ else if t >= 1.0 then ip := ln1b
2278+ else
2279+ begin
2280+ ip.X := Trunc(ln1a.X + t * dx1);
2281+ ip.Y := Trunc(ln1a.Y + t * dy1);
22782282{ $IFDEF USINGZ}
2279- ip.Z := 0 ;
2283+ ip.Z := 0 ;
22802284{ $ENDIF}
2285+ end ;
2286+ end ;
2287+ // ------------------------------------------------------------------------------
2288+
2289+ function GetLineIntersectPt (const ln1a, ln1b, ln2a, ln2b: TPointD;
2290+ out ip: TPointD): Boolean;
2291+ var
2292+ dx1,dy1, dx2,dy2, t, cp: double;
2293+ begin
2294+ dy1 := (ln1b.y - ln1a.y);
2295+ dx1 := (ln1b.x - ln1a.x);
2296+ dy2 := (ln2b.y - ln2a.y);
2297+ dx2 := (ln2b.x - ln2a.x);
2298+ cp := dy1 * dx2 - dy2 * dx1;
2299+ Result := (cp <> 0.0 );
2300+ if not Result then Exit;
2301+ t := ((ln1a.x-ln2a.x) * dy2 - (ln1a.y-ln2a.y) * dx2) / cp;
2302+ if t <= 0.0 then ip := ln1a
2303+ else if t >= 1.0 then ip := ln1b
2304+ else
2305+ begin
2306+ ip.X := Trunc(ln1a.X + t * dx1);
2307+ ip.Y := Trunc(ln1a.Y + t * dy1);
2308+ { $IFDEF USINGZ}
2309+ ip.Z := 0 ;
2310+ { $ENDIF}
2311+ end ;
22812312end ;
22822313// ------------------------------------------------------------------------------
22832314
0 commit comments