Skip to content

Commit 879e2e8

Browse files
committed
Clipper.Core - renamed GetSegmentIntersectPt to GetLineIntersectPt
1 parent c5fc0b0 commit 879e2e8

File tree

12 files changed

+111
-118
lines changed

12 files changed

+111
-118
lines changed

CPP/Clipper2Lib/include/clipper2/clipper.core.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ namespace Clipper2Lib
941941
}
942942
#else
943943
template<typename T>
944-
inline bool GetSegmentIntersectPt(const Point<T>& ln1a, const Point<T>& ln1b,
944+
inline bool GetLineIntersectPt(const Point<T>& ln1a, const Point<T>& ln1b,
945945
const Point<T>& ln2a, const Point<T>& ln2b, Point<T>& ip)
946946
{
947947
// https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection

CPP/Clipper2Lib/src/clipper.engine.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*******************************************************************************
22
* Author : Angus Johnson *
3-
* Date : 15 June 2025 *
3+
* Date : 11 October 2025 *
44
* Website : https://www.angusj.com *
55
* Copyright : Angus Johnson 2010-2025 *
66
* Purpose : This is the main polygon clipping module *
@@ -1568,7 +1568,7 @@ namespace Clipper2Lib {
15681568
outrec->pts = prevOp;
15691569

15701570
Point64 ip;
1571-
GetSegmentIntersectPt(prevOp->pt, splitOp->pt,
1571+
GetLineIntersectPt(prevOp->pt, splitOp->pt,
15721572
splitOp->next->pt, nextNextOp->pt, ip);
15731573

15741574
#ifdef USINGZ
@@ -2356,7 +2356,7 @@ namespace Clipper2Lib {
23562356
void ClipperBase::AddNewIntersectNode(Active& e1, Active& e2, int64_t top_y)
23572357
{
23582358
Point64 ip;
2359-
if (!GetSegmentIntersectPt(e1.bot, e1.top, e2.bot, e2.top, ip))
2359+
if (!GetLineIntersectPt(e1.bot, e1.top, e2.bot, e2.top, ip))
23602360
ip = Point64(e1.curr_x, top_y); //parallel edges
23612361

23622362
//rounding errors can occasionally place the calculated intersection

CPP/Clipper2Lib/src/clipper.offset.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*******************************************************************************
22
* Author : Angus Johnson *
3-
* Date : 4 May 2025 *
3+
* Date : 11 October 2025 *
44
* Website : https://www.angusj.com *
55
* Copyright : Angus Johnson 2010-2025 *
66
* Purpose : Path Offset (Inflate/Shrink) *
@@ -239,7 +239,7 @@ void ClipperOffset::DoSquare(const Path64& path, size_t j, size_t k)
239239
{
240240
PointD pt4 = PointD(pt3.x + vec.x * group_delta_, pt3.y + vec.y * group_delta_);
241241
PointD pt = ptQ;
242-
GetSegmentIntersectPt(pt1, pt2, pt3, pt4, pt);
242+
GetLineIntersectPt(pt1, pt2, pt3, pt4, pt);
243243
//get the second intersect point through reflecion
244244
path_out.emplace_back(ReflectPoint(pt, ptQ));
245245
path_out.emplace_back(pt);
@@ -248,7 +248,7 @@ void ClipperOffset::DoSquare(const Path64& path, size_t j, size_t k)
248248
{
249249
PointD pt4 = GetPerpendicD(path[j], norms[k], group_delta_);
250250
PointD pt = ptQ;
251-
GetSegmentIntersectPt(pt1, pt2, pt3, pt4, pt);
251+
GetLineIntersectPt(pt1, pt2, pt3, pt4, pt);
252252
path_out.emplace_back(pt);
253253
//get the second intersect point through reflecion
254254
path_out.emplace_back(ReflectPoint(pt, ptQ));

CPP/Clipper2Lib/src/clipper.rectclip.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*******************************************************************************
22
* Author : Angus Johnson *
3-
* Date : 5 July 2024 *
3+
* Date : 11 October 2025 *
44
* Website : https://www.angusj.com *
5-
* Copyright : Angus Johnson 2010-2024 *
5+
* Copyright : Angus Johnson 2010-2025 *
66
* Purpose : FAST rectangular clipping *
77
* License : https://www.boost.org/LICENSE_1_0.txt *
88
*******************************************************************************/
@@ -73,8 +73,8 @@ namespace Clipper2Lib {
7373
bool GetSegmentIntersection(const Point64& p1,
7474
const Point64& p2, const Point64& p3, const Point64& p4, Point64& ip)
7575
{
76-
double res1 = CrossProduct(p1, p3, p4);
77-
double res2 = CrossProduct(p2, p3, p4);
76+
int res1 = CrossProductSign(p1, p3, p4);
77+
int res2 = CrossProductSign(p2, p3, p4);
7878
if (res1 == 0)
7979
{
8080
ip = p1;
@@ -93,8 +93,8 @@ namespace Clipper2Lib {
9393
}
9494
if ((res1 > 0) == (res2 > 0)) return false;
9595

96-
double res3 = CrossProduct(p3, p1, p2);
97-
double res4 = CrossProduct(p4, p1, p2);
96+
int res3 = CrossProductSign(p3, p1, p2);
97+
int res4 = CrossProductSign(p4, p1, p2);
9898
if (res3 == 0)
9999
{
100100
ip = p3;
@@ -112,7 +112,7 @@ namespace Clipper2Lib {
112112
if ((res3 > 0) == (res4 > 0)) return false;
113113

114114
// segments must intersect to get here
115-
return GetSegmentIntersectPt(p1, p2, p3, p4, ip);
115+
return GetLineIntersectPt(p1, p2, p3, p4, ip);
116116
}
117117

118118
inline bool GetIntersection(const Path64& rectPath,
@@ -223,7 +223,7 @@ namespace Clipper2Lib {
223223
const Point64& prev_pt, const Point64& curr_pt, const Point64& rect_mp)
224224
{
225225
if (AreOpposites(prev, curr))
226-
return CrossProduct(prev_pt, rect_mp, curr_pt) < 0;
226+
return CrossProductSign(prev_pt, rect_mp, curr_pt) < 0;
227227
else
228228
return HeadingClockwise(prev, curr);
229229
}

CSharp/Clipper2Lib/Clipper.Core.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ internal static long CheckCastInt64(double val)
671671

672672

673673
[MethodImpl(MethodImplOptions.AggressiveInlining)]
674-
public static bool GetSegmentIntersectPt(Point64 ln1a,
674+
public static bool GetLineIntersectPt(Point64 ln1a,
675675
Point64 ln1b, Point64 ln2a, Point64 ln2b, out Point64 ip)
676676
{
677677
double dy1 = (ln1b.Y - ln1a.Y);
@@ -688,7 +688,8 @@ public static bool GetSegmentIntersectPt(Point64 ln1a,
688688
double t = ((ln1a.X - ln2a.X) * dy2 - (ln1a.Y - ln2a.Y) * dx2) / det;
689689
if (t <= 0.0) ip = ln1a;
690690
else if (t >= 1.0) ip = ln1b;
691-
else {
691+
else
692+
{
692693
// avoid using constructor (and rounding too) as they affect performance //664
693694
ip.X = (long) (ln1a.X + t * dx1);
694695
ip.Y = (long) (ln1a.Y + t * dy1);
@@ -699,6 +700,36 @@ public static bool GetSegmentIntersectPt(Point64 ln1a,
699700
return true;
700701
}
701702

703+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
704+
public static bool GetLineIntersectPt(PointD ln1a,
705+
PointD ln1b, PointD ln2a, PointD ln2b, out PointD ip)
706+
{
707+
double dy1 = (ln1b.y - ln1a.y);
708+
double dx1 = (ln1b.x - ln1a.x);
709+
double dy2 = (ln2b.y - ln2a.y);
710+
double dx2 = (ln2b.x - ln2a.x);
711+
double det = dy1 * dx2 - dy2 * dx1;
712+
if (det == 0.0)
713+
{
714+
ip = new PointD();
715+
return false;
716+
}
717+
718+
double t = ((ln1a.x - ln2a.x) * dy2 - (ln1a.y - ln2a.y) * dx2) / det;
719+
if (t <= 0.0) ip = ln1a;
720+
else if (t >= 1.0) ip = ln1b;
721+
else
722+
{
723+
// avoid using constructor (and rounding too) as they affect performance //664
724+
ip.x = (ln1a.x + t * dx1);
725+
ip.y = (ln1a.y + t * dy1);
726+
#if USINGZ
727+
ip.Z = 0;
728+
#endif
729+
}
730+
return true;
731+
}
732+
702733
internal static bool SegsIntersect(Point64 seg1a,
703734
Point64 seg1b, Point64 seg2a, Point64 seg2b, bool inclusive = false)
704735
{

CSharp/Clipper2Lib/Clipper.Engine.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*******************************************************************************
22
* Author : Angus Johnson *
3-
* Date : 7 October 2025 *
3+
* Date : 11 October 2025 *
44
* Website : https://www.angusj.com *
55
* Copyright : Angus Johnson 2010-2025 *
66
* Purpose : This is the main polygon clipping module *
@@ -1917,7 +1917,7 @@ private void DisposeIntersectNodes()
19171917
[MethodImpl(MethodImplOptions.AggressiveInlining)]
19181918
private void AddNewIntersectNode(Active ae1, Active ae2, long topY)
19191919
{
1920-
if (!InternalClipper.GetSegmentIntersectPt(
1920+
if (!InternalClipper.GetLineIntersectPt(
19211921
ae1.bot, ae1.top, ae2.bot, ae2.top, out Point64 ip))
19221922
ip = new Point64(ae1.curX, topY);
19231923

@@ -2889,7 +2889,7 @@ private void DoSplitOp(OutRec outrec, OutPt splitOp)
28892889
OutPt nextNextOp = splitOp.next!.next!;
28902890
outrec.pts = prevOp;
28912891

2892-
InternalClipper.GetSegmentIntersectPt(
2892+
InternalClipper.GetLineIntersectPt(
28932893
prevOp.pt, splitOp.pt, splitOp.next.pt, nextNextOp.pt, out Point64 ip);
28942894

28952895
#if USINGZ

CSharp/Clipper2Lib/Clipper.Offset.cs

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*******************************************************************************
22
* Author : Angus Johnson *
3-
* Date : 7 October 2025 *
3+
* Date : 11 October 2025 *
44
* Website : https://www.angusj.com *
55
* Copyright : Angus Johnson 2010-2025 *
66
* Purpose : Path Offset (Inflate/Shrink) *
@@ -327,35 +327,6 @@ private static PointD GetAvgUnitVector(PointD vec1, PointD vec2)
327327
return NormalizeVector(new PointD(vec1.x + vec2.x, vec1.y + vec2.y));
328328
}
329329

330-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
331-
private static PointD IntersectPoint(PointD pt1a, PointD pt1b, PointD pt2a, PointD pt2b)
332-
{
333-
if (InternalClipper.IsAlmostZero(pt1a.x - pt1b.x)) //vertical
334-
{
335-
if (InternalClipper.IsAlmostZero(pt2a.x - pt2b.x)) return new PointD(0, 0);
336-
double m2 = (pt2b.y - pt2a.y) / (pt2b.x - pt2a.x);
337-
double b2 = pt2a.y - m2 * pt2a.x;
338-
return new PointD(pt1a.x, m2* pt1a.x + b2);
339-
}
340-
341-
if (InternalClipper.IsAlmostZero(pt2a.x - pt2b.x)) //vertical
342-
{
343-
double m1 = (pt1b.y - pt1a.y) / (pt1b.x - pt1a.x);
344-
double b1 = pt1a.y - m1 * pt1a.x;
345-
return new PointD(pt2a.x, m1* pt2a.x + b1);
346-
}
347-
else
348-
{
349-
double m1 = (pt1b.y - pt1a.y) / (pt1b.x - pt1a.x);
350-
double b1 = pt1a.y - m1 * pt1a.x;
351-
double m2 = (pt2b.y - pt2a.y) / (pt2b.x - pt2a.x);
352-
double b2 = pt2a.y - m2 * pt2a.x;
353-
if (InternalClipper.IsAlmostZero(m1 - m2)) return new PointD(0, 0);
354-
double x = (b2 - b1) / (m1 - m2);
355-
return new PointD(x, m1 * x + b1);
356-
}
357-
}
358-
359330
[MethodImpl(MethodImplOptions.AggressiveInlining)]
360331
private Point64 GetPerpendic(Point64 pt, PointD norm)
361332
{
@@ -456,7 +427,7 @@ private void DoSquare(Path64 path, int j, int k)
456427
PointD pt4 = new PointD(
457428
pt3.x + vec.x * _groupDelta,
458429
pt3.y + vec.y * _groupDelta);
459-
PointD pt = IntersectPoint(pt1, pt2, pt3, pt4);
430+
InternalClipper.GetLineIntersectPt(pt1, pt2, pt3, pt4, out PointD pt);
460431
#if USINGZ
461432
pt.z = ptQ.z;
462433
#endif
@@ -467,7 +438,7 @@ private void DoSquare(Path64 path, int j, int k)
467438
else
468439
{
469440
PointD pt4 = GetPerpendicD(path[j], _normals[k]);
470-
PointD pt = IntersectPoint(pt1, pt2, pt3, pt4);
441+
InternalClipper.GetLineIntersectPt(pt1, pt2, pt3, pt4, out PointD pt);
471442
#if USINGZ
472443
pt.z = ptQ.z;
473444
#endif

CSharp/Clipper2Lib/Clipper.RectClip.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*******************************************************************************
22
* Author : Angus Johnson *
3-
* Date : 7 October 2025 *
3+
* Date : 11 October 2025 *
44
* Website : https://www.angusj.com *
55
* Copyright : Angus Johnson 2010-2025 *
66
* Purpose : FAST rectangular clipping *
@@ -330,7 +330,7 @@ private static bool GetSegmentIntersection(Point64 p1,
330330
}
331331

332332
// segments must intersect to get here
333-
return InternalClipper.GetSegmentIntersectPt(p1, p2, p3, p4, out ip);
333+
return InternalClipper.GetLineIntersectPt(p1, p2, p3, p4, out ip);
334334
}
335335

336336

Delphi/Clipper2Lib/Clipper.Core.pas

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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

335335
function 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

340342
function PointInPolygon(const pt: TPoint64; const polygon: TPath64): TPointInPolygonResult;
341343
function Path2ContainsPath1(const path1, path2: TPath64): Boolean;
@@ -2147,18 +2149,18 @@ function DistanceSqr(const pt1, pt2: TPointD): double;
21472149

21482150
function PerpendicDistFromLineSqrd(const pt, linePt1, linePt2: TPoint64): double;
21492151
var
2150-
a,b,c: double;
2152+
a,b,c,d: double;
21512153
begin
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);
21622164
end;
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;
22572259
end;
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;
22622264
var
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;
22812312
end;
22822313
//------------------------------------------------------------------------------
22832314

0 commit comments

Comments
 (0)