Skip to content

Commit 739f2e3

Browse files
committed
Minor bugfix in polygon offsetting (#593)
1 parent 6222af4 commit 739f2e3

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

CPP/Clipper2Lib/src/clipper.offset.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*******************************************************************************
22
* Author : Angus Johnson *
3-
* Date : 16 July 2023 *
3+
* Date : 7 August 2023 *
44
* Website : http://www.angusj.com *
55
* Copyright : Angus Johnson 2010-2023 *
66
* Purpose : Path Offset (Inflate/Shrink) *
@@ -354,6 +354,17 @@ void ClipperOffset::OffsetPoint(Group& group, Path64& path, size_t j, size_t k)
354354

355355
void ClipperOffset::OffsetPolygon(Group& group, Path64& path)
356356
{
357+
// when the path is contracting, make sure
358+
// there is sufficient space to do so. //#593
359+
// nb: this will have a small impact on performance
360+
double a = Area(path);
361+
// contracting when orientation is opposite offset direction
362+
if ((a < 0) != (group_delta_ < 0))
363+
{
364+
Rect64 rec = GetBounds(path);
365+
if (std::fabs(group_delta_) * 2 > rec.Width()) return;
366+
}
367+
357368
for (Path64::size_type j = 0, k = path.size() -1; j < path.size(); k = j, ++j)
358369
OffsetPoint(group, path, j, k);
359370
group.paths_out.push_back(group.path);

CSharp/Clipper2Lib/Clipper.Offset.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*******************************************************************************
22
* Author : Angus Johnson *
3-
* Date : 16 July 2023 *
3+
* Date : 7 August 2023 *
44
* Website : http://www.angusj.com *
55
* Copyright : Angus Johnson 2010-2023 *
66
* Purpose : Path Offset (Inflate/Shrink) *
@@ -498,6 +498,16 @@ private void OffsetPoint(Group group, Path64 path, int j, ref int k)
498498
[MethodImpl(MethodImplOptions.AggressiveInlining)]
499499
private void OffsetPolygon(Group group, Path64 path)
500500
{
501+
// when the path is contracting, make sure
502+
// there is sufficient space to do so. //#593
503+
//nb: this will have a small impact on performance
504+
double a = Clipper.Area(path);
505+
if ((a < 0) != (_groupDelta < 0))
506+
{
507+
Rect64 rec = Clipper.GetBounds(path);
508+
if (Math.Abs(_groupDelta) * 2 > rec.Width) return;
509+
}
510+
501511
group.outPath = new Path64();
502512
int cnt = path.Count, prev = cnt - 1;
503513
for (int i = 0; i < cnt; i++)

Delphi/Clipper2Lib/Clipper.Offset.pas

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
(*******************************************************************************
44
* Author : Angus Johnson *
5-
* Date : 16 July 2023 *
5+
* Date : 7 August 2023 *
66
* Website : http://www.angusj.com *
77
* Copyright : Angus Johnson 2010-2023 *
88
* Purpose : Path Offset (Inflate/Shrink) *
@@ -429,7 +429,19 @@ procedure TClipperOffset.BuildNormals;
429429
procedure TClipperOffset.OffsetPolygon;
430430
var
431431
i,j: integer;
432+
a: double;
433+
rec: TRect64;
432434
begin
435+
//when the path is contracting, make sure
436+
//there is sufficient space to do so. //#593
437+
//nb: this will have a small impact on performance
438+
a := Area(fInPath);
439+
if (a < 0) <> (fGroupDelta < 0) then
440+
begin
441+
rec := GetBounds(fInPath);
442+
if Abs(fGroupDelta) * 2 >= rec.Width then Exit;
443+
end;
444+
433445
j := high(fInPath);
434446
for i := 0 to high(fInPath) do
435447
OffsetPoint(i, j);

0 commit comments

Comments
 (0)