Skip to content

Commit

Permalink
Add RandomPoint methods to TTriangle
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Dec 31, 2024
1 parent df8ab8f commit 208d711
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
35 changes: 28 additions & 7 deletions Source/script/imports/simba.import_triangle.pas
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,30 @@ procedure _LapeTriangle_Incircle(const Params: PParamArray; const Result: Pointe
TCircle(Result^) := TTriangle(Params^[0]^).Incircle();
end;

(*
TTriangle.RandomPoint
---------------------
```
function TTriangle.RandomPoint(): TPoint;
```
*)
procedure _LapeTriangle_RandomPoint(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PPoint(Result)^ := TTriangle(Params^[0]^).RandomPoint();
end;

(*
TTriangle.RandomPointCenter
---------------------------
```
function TTriangle.RandomPointCenter(): TPoint;
```
*)
procedure _LapeTriangle_RandomPointCenter(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PPoint(Result)^ := TTriangle(Params^[0]^).RandomPointCenter();
end;

(*
TTriangle.Area
--------------
Expand Down Expand Up @@ -276,9 +300,6 @@ procedure _LapeTriangle_Mean_Read(const Params: PParamArray; const Result: Point
TPoint(Result^) := TTriangle(Params^[0]^).Mean;
end;




(*
in
--
Expand All @@ -297,14 +318,14 @@ procedure ImportTriangle(Script: TSimbaScript);
begin
DumpSection := 'TTriangle';

addGlobalFunc('function TTriangle.Create(A, B, C: TPoint): TTriangle; static; overload', @_LapeTriangle_Create);
addGlobalFunc('function TTriangle.Create(A, B, C: TPoint): TTriangle; static', @_LapeTriangle_Create);

addGlobalFunc('function TTriangle.Centroid(): TPoint', @_LapeTriangle_Centroid);
addGlobalFunc('function TTriangle.SymmedianPoint(): TPoint', @_LapeTriangle_SymmedianPoint);
addGlobalFunc('function TTriangle.Incenter(): TPoint', @_LapeTriangle_Incenter);

addGlobalFunc('function TTriangle.Rotate(Angle: Double): TTriangle', @_LapeTriangle_Rotate);
addGlobalFunc('function TTriangle.Contains(P: TPoint): Boolean; overload', @_LapeTriangle_Contains);
addGlobalFunc('function TTriangle.Contains(P: TPoint): Boolean', @_LapeTriangle_Contains);
addGlobalFunc('function TTriangle.Offset(P: TPoint): TTriangle', @_LapeTriangle_Offset);
addGlobalFunc('function TTriangle.Extract(Points: TPointArray): TPointArray', @_LapeTriangle_Extract);
addGlobalFunc('function TTriangle.Exclude(Points: TPointArray): TPointArray', @_LapeTriangle_Exclude);
Expand All @@ -315,8 +336,8 @@ procedure ImportTriangle(Script: TSimbaScript);
addGlobalFunc('function TTriangle.Circumcircle(): TCircle', @_LapeTriangle_Circumcircle);
addGlobalFunc('function TTriangle.Incircle(): TCircle', @_LapeTriangle_Incircle);

//addGlobalFunc('function TTriangle.RandomPoint: TPoint', @_LapeTriangle_RandomPoint);
//addGlobalFunc('function TTriangle.RandomPointCenter: TPoint', @_LapeTriangle_RandomPointCenter);
addGlobalFunc('function TTriangle.RandomPoint: TPoint', @_LapeTriangle_RandomPoint);
addGlobalFunc('function TTriangle.RandomPointCenter: TPoint', @_LapeTriangle_RandomPointCenter);

addProperty('TTriangle', 'Area', 'Integer', @_LapeTriangle_Area_Read);
addProperty('TTriangle', 'Corners', 'TPointArray', @_LapeTriangle_Corners_Read);
Expand Down
33 changes: 33 additions & 0 deletions Source/simba.vartype_triangle.pas
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
Project: Simba (https://github.com/MerlijnWajer/Simba)
License: GNU General Public License (https://www.gnu.org/licenses/gpl-3.0)
}

{
Osada, R., Funkhouser, T., Chazelle, B., & Dobkin, D. (2002). Shape distributions. ACM Transactions on Graphics (TOG), 21(4), 807-832.
RandomPoint
RandomPointCenter
}
unit simba.vartype_triangle;

{$i simba.inc}
Expand Down Expand Up @@ -41,6 +48,9 @@ interface
function Circumcircle(): TCircle;
function Incircle(): TCircle;

function RandomPoint(): TPoint;
function RandomPointCenter(): TPoint;

property Corners: TPointArray read GetCorners;
property Mean: TPoint read GetMean;
property Area: Integer read GetArea;
Expand Down Expand Up @@ -306,6 +316,29 @@ function TTriangleHelper.Incircle(): TCircle;
Result.Radius := Round(p.DistanceTo(q));
end;

function TTriangleHelper.RandomPoint(): TPoint;
var
r1,r2,s1: Double;
begin
r1 := Random();
r2 := Random();
s1 := Sqrt(r1);

Result.X := Round(A.X * (1.0 - s1) + B.X * (1.0 - r2) * s1 + C.X * r2 * s1);
Result.Y := Round(A.Y * (1.0 - s1) + B.Y * (1.0 - r2) * s1 + C.Y * r2 * s1);
end;

function TTriangleHelper.RandomPointCenter(): TPoint;
var
r1,r2,s1: Double;
begin
r1 := RandomMean(0.0, 1.0);
r2 := RandomMean(0.0, 1.0);
s1 := Sqrt(r1);

Result.X := Round(A.X * (1.0 - s1) + B.X * (1.0 - r2) * s1 + C.X * r2 * s1);
Result.Y := Round(A.Y * (1.0 - s1) + B.Y * (1.0 - r2) * s1 + C.Y * r2 * s1);
end;

operator in(const P: TPoint; const Triangle: TTriangle): Boolean;
begin
Expand Down

0 comments on commit 208d711

Please sign in to comment.