Skip to content

Commit

Permalink
FixRad/FixD > NormalizeRad/Deg
Browse files Browse the repository at this point in the history
Radians > DegToRad
Degrees > RadToDeg
  • Loading branch information
ollydev committed Sep 2, 2023
1 parent 9c24f38 commit dd7f748
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 83 deletions.
6 changes: 3 additions & 3 deletions Source/colormath/simba.colormath_aca.pas
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ function MeanAngle(Arr: TDoubleArray): Double;
y := 0;
for i:=0 to High(Arr) do
begin
x += Cos(Radians(Arr[i]));
y += Sin(Radians(Arr[i]));
x += Cos(DegToRad(Arr[i]));
y += Sin(DegToRad(Arr[i]));
end;
Result := FixD(Degrees(ArcTan2(y / Length(Arr), x / Length(Arr))));
Result := DegNormalize(RadToDeg(ArcTan2(y / Length(Arr), x / Length(Arr))));
end;

function NormalizeMods(Mods: TChannelMultipliers): TChannelMultipliers;
Expand Down
8 changes: 4 additions & 4 deletions Source/finders/simba.finder_dtm.pas
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
interface

uses
Classes, SysUtils, Graphics,
Classes, SysUtils, Graphics, Math,
simba.mufasatypes, simba.colormath, simba.target, simba.simplelock,
simba.dtm;

Expand Down Expand Up @@ -279,9 +279,9 @@ TMatch = record X,Y: Integer; Deg: Double; end;
SetLength(RotatedPoints, Length(SearchPoints));
SetLength(Table, SearchHeight, SearchWidth);

StartDegrees := FixD(StartDegrees);
StartDegrees := DegNormalize(StartDegrees);
if (EndDegrees <> 360) then
EndDegrees := FixD(EndDegrees);
EndDegrees := DegNormalize(EndDegrees);
if (StartDegrees > EndDegrees) then
EndDegrees := EndDegrees + 360;

Expand All @@ -298,7 +298,7 @@ TMatch = record X,Y: Integer; Deg: Double; end;

Inc(AngleSteps);

RotateDTMPoints(RotatedPoints, Radians(SearchDegree), DTMBounds);
RotateDTMPoints(RotatedPoints, DegToRad(SearchDegree), DTMBounds);

MainPointArea.X1 := Abs(DTMBounds.X1);
MainPointArea.Y1 := Abs(DTMBounds.Y1);
Expand Down
74 changes: 39 additions & 35 deletions Source/script/imports/simba/simba.import_math.pas
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@ procedure _LapeDistanceEx(const Params: PParamArray; const Result: Pointer); LAP
PInteger(Result)^ := Distance(PPoint(Params^[0])^, PPoint(Params^[1])^);
end;

(*
FixD
~~~~
> function FixD(Deg: Double): Double;
*)
procedure _LapeFixD(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PDouble(Result)^ := FixD(PDouble(Params^[0])^);
end;

(*
LogN
~~~~
Expand Down Expand Up @@ -93,23 +83,43 @@ procedure _LapeRol(const Params: PParamArray; const Result: Pointer); LAPE_WRAPP
end;

(*
Radians
~~~~~~~
> function Radians(Deg: Double): Double;
DegToRad
~~~~~~~~
> function DegToRad(Deg: Double): Double;
*)
procedure _LapeDegToRad(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PDouble(Result)^ := DegToRad(PDouble(Params^[0])^);
end;

(*
RadToDeg
~~~~~~~~
> function RadToDeg(Rad: Double): Double;
*)
procedure _LapeRadToDeg(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PDouble(Result)^ := RadToDeg(PDouble(Params^[0])^);
end;

(*
RadNormalize
~~~~~~~~~~~~
> function RadNormalize(Rad: Double): Double;
*)
procedure _LapeRadians(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
procedure _LapeRadNormalize(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PDouble(Result)^ := Radians(PDouble(Params^[0])^);
PDouble(Result)^ := DegToRad(DegNormalize(RadToDeg(PDouble(Params^[0])^)));
end;

(*
Degrees
~~~~~~~
> function Degrees(Rad: Double): Double;
DegNormalize
~~~~~~~~~~~~
> function DegNormalize(Deg: Double): Double;
*)
procedure _LapeDegrees(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
procedure _LapeDegNormalize(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PDouble(Result)^ := Degrees(PDouble(Params^[0])^);
PDouble(Result)^ := DegNormalize(PDouble(Params^[0])^);
end;

(*
Expand All @@ -132,16 +142,6 @@ procedure _LapeLog10(const Params: PParamArray; const Result: Pointer); LAPE_WRA
PDouble(Result)^ := Log10(PDouble(Params^[0])^);
end;

(*
FixRad
~~~~~~
> function FixRad(Rad: Double): Double;
*)
procedure _LapeFixRad(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PDouble(Result)^ := FixRad(PDouble(Params^[0])^);
end;

(*
NextPower2
~~~~~~~~~~
Expand Down Expand Up @@ -358,16 +358,20 @@ procedure ImportMath(Compiler: TSimbaScript_Compiler);

addGlobalFunc('function Distance(const X1, Y1, X2, Y2: Integer): Integer; overload', @_LapeDistance);
addGlobalFunc('function Distance(const P1, P2: TPoint): Integer; overload', @_LapeDistanceEx);
addGlobalFunc('function FixD(Deg: Double): Double', @_LapeFixD);

addGlobalFunc('function Sar(x: Integer; Shift: Byte): Integer', @_LapeSar);
addGlobalFunc('function Ror(x: UInt32; Shift: Byte): UInt32', @_LapeRor);
addGlobalFunc('function Rol(x: UInt32; Shift: Byte): UInt32', @_LapeRol);
addGlobalFunc('function Radians(Deg: Double): Double', @_LapeRadians);
addGlobalFunc('function Degrees(Rad: Double): Double', @_LapeDegrees);
addGlobalFunc('function LogN(base, x: Double): Double', @_LapeLogn);

addGlobalFunc('function LogN(base, x: Double): Double', @_LapeLogN);
addGlobalFunc('function Log2(x: Double): Double', @_LapeLog2);
addGlobalFunc('function Log10(x: Double): Double', @_LapeLog10);
addGlobalFunc('function FixRad(Rad: Double): Double', @_LapeFixRad);

addGlobalFunc('function DegToRad(Deg: Double): Double', @_LapeDegToRad);
addGlobalFunc('function RadToDeg(Rad: Double): Double', @_LapeRadToDeg);
addGlobalFunc('function RadNormalize(Rad: Double): Double', @_LapeRadNormalize);
addGlobalFunc('function DegNormalize(Deg: Double): Double', @_LapeDegNormalize);

addGlobalFunc('function NextPower2(const n: Integer): Integer', @_LapeNextPower2);

addGlobalFunc('function Modulo(const X, Y: Integer): Integer; overload', @_LapeModulo);
Expand Down
2 changes: 1 addition & 1 deletion Source/simba.geometry.pas
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class function TSimbaGeometry.RotatePoints(const Points: TPointArray; Radians, X

class function TSimbaGeometry.AngleBetween(const P1, P2: TPoint): Double;
begin
Result := Modulo(Degrees(ArcTan2(P2.Y - P1.Y, P2.X - P1.X)) - 90, 360);
Result := Modulo(RadToDeg(ArcTan2(P2.Y - P1.Y, P2.X - P1.X)) - 90, 360);
end;

class function TSimbaGeometry.DeltaAngle(const DegreesA, DegreesB: Double; R: Double): Double;
Expand Down
2 changes: 1 addition & 1 deletion Source/simba.image.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,7 @@ procedure TSimbaImage.DrawHSLCircle(ACenter: TPoint; Radius: Integer);
for Y := Bounds.Y1 to Bounds.Y2 do
for X := Bounds.X1 to Bounds.X2 do
begin
HSL.H := Degrees(ArcTan2(Y - ACenter.Y, X - ACenter.X));
HSL.H := RadToDeg(ArcTan2(Y - ACenter.Y, X - ACenter.X));
HSL.S := Hypot(ACenter.X - X, ACenter.Y - Y) / Radius * 100;
HSL.L := 50;
if (HSL.S < 100) then
Expand Down
32 changes: 4 additions & 28 deletions Source/simba.math.pas
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ interface
SQRT_3 = Double(1.73205080756888);
SQRT_5 = Double(2.23606797749979);

function FixRad(const Rad: Extended): Extended;
function FixD(const Degrees: Extended): Extended;
function Distance(const X1, Y1, X2, Y2: Integer): Integer; inline; overload;
function Distance(const P1, P2: TPoint): Integer; inline; overload;
function Radians(const e: Extended): Extended;
function Degrees(const e: Extended): Extended;
function NextPower2(const n: Integer): Integer;

function IsNumber(const n: Double): Boolean; inline; overload;
Expand All @@ -35,7 +31,7 @@ function Modulo(const X, Y: Double): Double; inline; overload;
function Modulo(const X, Y: Single): Single; inline; overload;
function Modulo(const X, Y: Integer): Integer; inline; overload;

function CeilTo(const a: Single; const Precision: Int8 = 0): Double;
function CeilTo(const n: Double; const Precision: Int8 = 0): Double;

implementation

Expand Down Expand Up @@ -79,11 +75,6 @@ function NextPower2(const n: Integer): Integer;
Result := Result + 1;
end;

function FixRad(const Rad: Extended): Extended;
begin
Result := DegToRad(DegNormalize(RadToDeg(Rad)));
end;

function Distance(const X1, Y1, X2, Y2: Integer): Integer;
begin
Result := Round(Sqrt(Sqr(ValReal(X2) - ValReal(X1)) + Sqr(ValReal(Y2) - ValReal(Y1)))); // convert to ValReal to prevent integer overflows
Expand All @@ -94,27 +85,12 @@ function Distance(const P1, P2: TPoint): Integer;
Result := Round(Sqrt(Sqr(ValReal(P2.X) - ValReal(P1.X)) + Sqr(ValReal(P2.Y) - ValReal(P1.Y)))); // convert to ValReal to prevent integer overflows
end;

function FixD(const Degrees: Extended): Extended;
begin
Result := DegNormalize(Degrees);
end;

function Radians(const e: Extended): Extended;
begin
Result := DegToRad(e);
end;

function Degrees(const e: Extended): Extended;
begin
Result := RadToDeg(e);
end;

function CeilTo(const a: Single; const Precision: Int8 = 0): Double;
function CeilTo(const n: Double; const Precision: Int8 = 0): Double;
begin
if (Precision = 0) then
Result := Ceil(a)
Result := Ceil(n)
else
Result := RoundTo(a + 0.5 * 10**(-Double(Precision)), -Precision);
Result := RoundTo(n + 0.5 * 10**(-Double(Precision)), -Precision);
end;

end.
Expand Down
16 changes: 8 additions & 8 deletions Source/simba.tpa.pas
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,8 @@ class function TPointArrayHelper.CreateFromSimplePolygon(Center: TPoint; Sides:
begin
ptx := Center.X + Size;
pty := Center.Y + Size;
SinR := Sin(Radians(360.0 / Sides));
CosR := Cos(Radians(360.0 / Sides));
SinR := Sin(DegToRad(360.0 / Sides));
CosR := Cos(DegToRad(360.0 / Sides));

Result := [Point(Round(ptx),Round(pty))];
for i:=1 to Sides-1 do
Expand Down Expand Up @@ -1297,8 +1297,8 @@ function TPointArrayHelper.ExtractPie(StartDegree, EndDegree, MinRadius, MaxRadi
Over180: Boolean;
Buffer: TSimbaPointBuffer;
begin
StartD := FixD(StartDegree);
EndD := FixD(EndDegree);
StartD := DegNormalize(StartDegree);
EndD := DegNormalize(EndDegree);

if (not SameValue(StartD, EndD)) then // if StartD = EndD, then we have a circle...
begin
Expand All @@ -1315,11 +1315,11 @@ function TPointArrayHelper.ExtractPie(StartDegree, EndDegree, MinRadius, MaxRadi
end;

// a is the midPoint, B is the left limit line, C is the right Limit Line, X the point we are checking
BminusAx := Cos(Radians(StartD - 90)); // creating the two unit vectors
BminusAy := Sin(Radians(StartD - 90)); // I use -90 or else it will start at the right side instead of top
BminusAx := Cos(DegToRad(StartD - 90)); // creating the two unit vectors
BminusAy := Sin(DegToRad(StartD - 90)); // I use -90 or else it will start at the right side instead of top

CminusAx := Cos(Radians(EndD - 90));
CminusAy := Sin(Radians(EndD - 90));
CminusAx := Cos(DegToRad(EndD - 90));
CminusAy := Sin(DegToRad(EndD - 90));

Buffer.Init(Length(Self) div 2);
for I := 0 to High(Self) do
Expand Down
2 changes: 1 addition & 1 deletion Tests/finder_dtmrotated.simba
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var
begin
dtm.FromString('DTM:eJxjY2Bg2MLFwLARiLcD8TYg3gTEO4BYnZmBQQWKtYFYC4g1gDhAj5tB34AdTMMwiE8qAAD40Aht');
img := TImage.CreateFromString('IMG:eJytmP1vU9cZx51iEaeAiWVP3F1DVu06I2RxiqImWS/ysmr8sB/LJMYE036apk1U7TqJkgAtqNsg4WW8lBTbeRkkrMDKhkpHEdB1z3ls3zgvMKZ2/8f+gqt9n3N8Y8d2QpEGRFi5z+d7z3nOeb7nOX77rV//MhQK/UJ+1oX847/50ZK/WC54e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w4VzrBGh8CZuVeZ5hkL4ZR/P3uIhr48T2gkyLlIo1xbtCJNjqMCtcnahUcMvJamlgOsxGGekYbOw59H/CxkPsKnT4pm4dtDUMEsrjwNKK+hTqk6EA15JqdS+HVeG1zl/hB3uxiGuX/YsiXAd/23hMYhAAPd9uW3x9VHQu0UBq5kQS5FbG2JwDUEWQbyGd2uxDihBJHuaK1e1ejhTYaMAk+B2VLlNMnapPdmOV45RLa/HE++xesgaUjU6UdVEBm/vxoybSljU49DzSEiLD9eF4fQUZR/2HLd26XvGk0XPCZGZ1VRWTkOR2w3D2o3qmhjBCudGjJR8t8KT39FafUYqaZbIodCyiBxSItEBif5AotcotKA1qdKevpzswi5uYMnpiFpRsvqVEcBS9qomEiUzC1w5GgUsijoUCGBBemlZwF+Y4+IQzxzmUMkyU/D/tVguhLC8fdqSu5HXFCVT7Va7I72eI0eXtIWysvdvHKMPj+qUmDtrraAXw1EZ46oUsjo1ioUzRb1CDNNqUTVKG1cKeQ0iqPp2MiIVCczneQQU5sOCq+b4rqWY96ePPRk/wvcK7KW1WSRwZ28PHH4Him38DLJkiG0BEDYAJ+RmhQuRJqSdEODVVeIJDbAVkbZdfzGgEZV6BqG/rVqN2PZVbGEX4qVkhyg5hHJzwk64GOH8FWkCK50AuifsS7tYDY9LuFkn5FET3VWggzfB5BBdtDlGywQWZYgMEcSrKG4BCLfnEI2zbTlWYSQskaXGyKeYorLL8aeS7qE3cZOeyGJu4jKIQT8xLyE/lZB/D0mCIbNaENkL8adD3CCC36/13F7atoSnyIK9ECliHb09XrekXbstvFKa5xZlP64Ne70SJk7UsWYQiidE9YFkP6kPtBvUdNAuZLCP7EX5xhdjymB1XWkG7396GzsgKl9CkrgfjR+U78Cu/vEAbm+5oxQyf1r+BzHw+AM=');
imgRotated := img.RotateNN(Radians(270), False);
imgRotated := img.RotateNN(DegToRad(270), False);

Target.SetImage(imgRotated);

Expand Down
2 changes: 1 addition & 1 deletion Tests/tpa_exclude.simba
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ begin
Assert(ExcludedTPA.Length() = 769);

// Quad
ExcludedTPA := TestTPA.ExcludeQuad(TQuad.CreateFromBox([20,20,30,30]).Rotate(Radians(45)));
ExcludedTPA := TestTPA.ExcludeQuad(TQuad.CreateFromBox([20,20,30,30]).Rotate(DegToRad(45)));
Assert(ExcludedTPA.Bounds() = TestTPA.Bounds());
Assert(ExcludedTPA.Length() = 848);

Expand Down
2 changes: 1 addition & 1 deletion Tests/tpa_extract.simba
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ begin
Assert(ExtractedTPA.Bounds() = [15,15,35,35]);

// Quad
ExtractedTPA := TestTPA.ExtractQuad(TQuad.CreateFromBox([20,20,30,30]).Rotate(Radians(45)));
ExtractedTPA := TestTPA.ExtractQuad(TQuad.CreateFromBox([20,20,30,30]).Rotate(DegToRad(45)));
Assert(ExtractedTPA.Bounds() = [18,18,32,32]);

// Pie
Expand Down

0 comments on commit dd7f748

Please sign in to comment.