Skip to content

Commit

Permalink
Extra params for TriangulatePolygon
Browse files Browse the repository at this point in the history
  • Loading branch information
slackydev committed Jul 27, 2024
1 parent a03dd46 commit 273ca49
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
11 changes: 8 additions & 3 deletions Source/script/imports/simba.import_math.pas
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,22 @@ procedure _LapeIsConvexPolygon(const Params: PParamArray; const Result: Pointer)
(*
TriangulatePolygon
-----------
> function TriangulatePolygon(Polygon: TPointArray): TTriangleArray;
> function TriangulatePolygon(Polygon: TPointArray; MinArea: Double=0; MaxDepth: Int32=0): TTriangleArray;
Break the polygon into triangles, the smallest possible polygon. The order of the
input does matter, if it fails, try to reverse the Poly with Poly.Reversed()
This is a custom algorithm by slacky, based around the concept of trimming "ears",
if you dont like the output, you may have more luck with rolling the Polygon before calling.
Two default params exists as well, `MinArea` and `MaxDepth`, they work in tandom,
`MinArea` parameter is for setting a minimum size of triangles added to result, and as this method
works iteratively, removing triangles in a circle around the shape over and over, `MaxDepth` refers
to the max number of rounds it has moved around the shape before it ignores `MinArea` paramater.
*)
procedure _LapeTriangulatePolygon(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PTriangleArray(Result)^ := TSimbaGeometry.TriangulatePolygon(PPointArray(Params^[0])^);
PTriangleArray(Result)^ := TSimbaGeometry.TriangulatePolygon(PPointArray(Params^[0])^, PSingle(Params^[1])^, PInt32(Params^[2])^);
end;

(*
Expand Down Expand Up @@ -428,7 +433,7 @@ procedure ImportMath(Compiler: TSimbaScript_Compiler);
addGlobalFunc('function Modulo(const X, Y: Double): Double; overload', @_LapeModuloF);

addGlobalFunc('function IsConvexPolygon(const Polygon: TPointArray): Boolean', @_LapeIsConvexPolygon);
addGlobalFunc('function TriangulatePolygon(const Polygon: TPointArray): TTriangleArray', @_LapeTriangulatePolygon);
addGlobalFunc('function TriangulatePolygon(const Polygon: TPointArray; MinArea: Single=0; MaxDepth: Int32=0): TTriangleArray', @_LapeTriangulatePolygon);
addGlobalFunc('function LineInPolygon(a1, a2: TPoint; const Polygon: TPointArray): Boolean', @_LapeLineInPolygon);

addGlobalFunc('function DeltaAngle(const DegreesA, DegreesB: Double; R: Double = 360): Double', @_LapeDeltaAngle);
Expand Down
18 changes: 11 additions & 7 deletions Source/simba.geometry.pas
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ TSimbaGeometry = class

class function IsConvexPolygon(const Polygon: TPointArray): Boolean;
class function LineInPolygon(a1, a2: TPoint; const Polygon: TPointArray): Boolean;
class function TriangulatePolygon(Polygon: TPointArray): TTriangleArray;
class function TriangulatePolygon(Polygon: TPointArray; MinArea: Single=0; MaxDepth: Int32=0): TTriangleArray;
class function PolygonArea(const Polygon: TPointArray): Double; static; inline;
class function ExpandPolygon(const Polygon: TPointArray; Amount: Integer): TPointArray; static;
class function CrossProduct(const r, p, q: TPoint): Int64; static; overload; inline;
Expand Down Expand Up @@ -394,9 +394,9 @@ class function TSimbaGeometry.LineInPolygon(a1, a2: TPoint; const Polygon: TPoin
Result := True;
end;

class function TSimbaGeometry.TriangulatePolygon(Polygon: TPointArray): TTriangleArray;
class function TSimbaGeometry.TriangulatePolygon(Polygon: TPointArray; MinArea: Single=0; MaxDepth: Int32=0): TTriangleArray;
var
i,rshift: Int32;
i,j,rshift: Int32;
A,B,C: TPoint;
tmp1,tmp2: TPointArray;
valid: Boolean;
Expand All @@ -407,6 +407,7 @@ class function TSimbaGeometry.TriangulatePolygon(Polygon: TPointArray): TTriangl
rshift := 0;
while Length(tmp1) > 3 do
begin
Inc(j);
valid := False;
i := 0;
rshift := 0;
Expand All @@ -418,10 +419,13 @@ class function TSimbaGeometry.TriangulatePolygon(Polygon: TPointArray): TTriangl

if (CrossProduct(A,B,C) >= 0) and LineInPolygon(A,C, Polygon) then
begin
SetLength(Result, Length(Result)+1);
Result[High(Result)].A := A;
Result[High(Result)].B := B;
Result[High(Result)].C := C;
if (j >= MaxDepth) or (PolygonArea([A,B,C]) > MinArea) then
begin
SetLength(Result, Length(Result)+1);
Result[High(Result)].A := A;
Result[High(Result)].B := B;
Result[High(Result)].C := C;
end;

tmp2[rshift+i] := A;
tmp2[rshift+i+1] := C;
Expand Down

0 comments on commit 273ca49

Please sign in to comment.