Skip to content

Commit

Permalink
Fix poly triangulation bug
Browse files Browse the repository at this point in the history
  • Loading branch information
slackydev committed Jul 29, 2024
1 parent 273ca49 commit 7737e1b
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions Source/simba.geometry.pas
Original file line number Diff line number Diff line change
Expand Up @@ -396,22 +396,20 @@ class function TSimbaGeometry.LineInPolygon(a1, a2: TPoint; const Polygon: TPoin

class function TSimbaGeometry.TriangulatePolygon(Polygon: TPointArray; MinArea: Single=0; MaxDepth: Int32=0): TTriangleArray;
var
i,j,rshift: Int32;
i,j: Int32;
A,B,C: TPoint;
tmp1,tmp2: TPointArray;
valid: Boolean;
begin
tmp1 := specialize Reversed<TPoint>(Polygon);
SetLength(tmp2, Length(Polygon));

rshift := 0;
while Length(tmp1) > 3 do
begin
Inc(j);
valid := False;
i := 0;
rshift := 0;
while i < Length(tmp1) do
while i < High(tmp1) do
begin
A := tmp1[i];
B := tmp1[(i+1) mod Length(tmp1)];
Expand All @@ -426,28 +424,27 @@ class function TSimbaGeometry.TriangulatePolygon(Polygon: TPointArray; MinArea:
Result[High(Result)].B := B;
Result[High(Result)].C := C;
end;

tmp2[rshift+i] := A;
tmp2[rshift+i+1] := C;
tmp2[i] := A;
tmp2[i+1] := C;
valid := True;
Inc(i,2);
if (B = tmp1[0]) then Inc(rshift);
end else
begin
tmp2[rshift+i] := A;
tmp2[i] := A;
Inc(i);
end;
end;

if not valid then Exit();
//Remove all duplicates without changing order
//This is actually not bad here.
if (i-rshift) > Length(tmp1) then SetLength(tmp1, i-rshift);
Move(tmp2[rshift], tmp1[0], (i-rshift)*SizeOf(TPoint));
if (i) > Length(tmp1) then SetLength(tmp1, i);
Move(tmp2[0], tmp1[0], i*SizeOf(TPoint));

tmp1 := tmp1.Unique();
end;

if Length(tmp1) = 3 then
begin
SetLength(Result, Length(Result)+1);
Expand Down

0 comments on commit 7737e1b

Please sign in to comment.