From 208d7115fda0658983b6dd1b4763637eff896c5a Mon Sep 17 00:00:00 2001 From: Olly Date: Tue, 31 Dec 2024 20:18:28 +0000 Subject: [PATCH] Add RandomPoint methods to TTriangle --- .../script/imports/simba.import_triangle.pas | 35 +++++++++++++++---- Source/simba.vartype_triangle.pas | 33 +++++++++++++++++ 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/Source/script/imports/simba.import_triangle.pas b/Source/script/imports/simba.import_triangle.pas index a1a5b1d09..0eeeaf747 100644 --- a/Source/script/imports/simba.import_triangle.pas +++ b/Source/script/imports/simba.import_triangle.pas @@ -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 -------------- @@ -276,9 +300,6 @@ procedure _LapeTriangle_Mean_Read(const Params: PParamArray; const Result: Point TPoint(Result^) := TTriangle(Params^[0]^).Mean; end; - - - (* in -- @@ -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); @@ -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); diff --git a/Source/simba.vartype_triangle.pas b/Source/simba.vartype_triangle.pas index 71fcc9999..568283d76 100644 --- a/Source/simba.vartype_triangle.pas +++ b/Source/simba.vartype_triangle.pas @@ -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} @@ -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; @@ -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