Skip to content

Commit

Permalink
TBox: Clip & Normalize are now functions as everything else is.
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Jul 24, 2023
1 parent 2b7b6c2 commit f98270a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 40 deletions.
55 changes: 29 additions & 26 deletions Source/box.inc
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ type
function Intersect(P: TPoint): TPoint;
function Corners: TPointArray;

procedure Clip(Other: TBox);
procedure Normalize;
function Clip(Other: TBox): TBox;
function Normalize: TBox;

property Width: Integer read GetWidth;
property Height: Integer read GetHeight;
property Center: TPoint read GetCenter;
end;

function Box(X1, Y1, X2, Y2: Integer): TBox;
function Box(Mid: TPoint; XRad, YRad: Integer): TBox;
function Box(const X1, Y1, X2, Y2: Integer): TBox; inline;
function Box(const Mid: TPoint; const XRad, YRad: Integer): TBox; inline;

operator = (const Left, Right: TBox): Boolean;

Expand All @@ -61,10 +61,8 @@ const
{$IFDEF BODY}
function TBoxHelper.GetCenter: TPoint;
begin
Result := TPoint.Create(
(Self.X2 + Self.X1 + 1) div 2,
(Self.Y2 + Self.Y1 + 1) div 2
);
Result.X := (Self.X2 + Self.X1 + 1) div 2;
Result.Y := (Self.Y2 + Self.Y1 + 1) div 2;
end;

function TBoxHelper.GetWidth: Integer;
Expand Down Expand Up @@ -100,7 +98,7 @@ end;

function TBoxHelper.IsDefault: Boolean;
begin
Result := CompareMem(@Self, @System.Default(TBox), SizeOf(TBox));
Result := (Int64(TopLeft) = 0) and (Int64(BottomRight) = 0);
end;

function TBoxHelper.RandomPoint: TPoint;
Expand Down Expand Up @@ -329,36 +327,40 @@ begin
Result := [TPoint.Create(X1, Y1), TPoint.Create(X2, Y1), TPoint.Create(X2, Y2), TPoint.Create(X1, Y2)];
end;

procedure TBoxHelper.Clip(Other: TBox);
function TBoxHelper.Clip(Other: TBox): TBox;
begin
if (Self.X1 < Other.X1) then Self.X1 := Other.X1;
if (Self.X1 > Other.X2) then Self.X1 := Other.X2;
if (Self.X2 < Other.X1) then Self.X2 := Other.X1;
if (Self.X2 > Other.X2) then Self.X2 := Other.X2;
Result := Self;

if (Self.Y1 < Other.Y1) then Self.Y1 := Other.Y1;
if (Self.Y1 > Other.Y2) then Self.Y1 := Other.Y2;
if (Self.Y2 < Other.Y1) then Self.Y2 := Other.Y1;
if (Self.Y2 > Other.Y2) then Self.Y2 := Other.Y2;
if (Result.X1 < Other.X1) then Result.X1 := Other.X1;
if (Result.X1 > Other.X2) then Result.X1 := Other.X2;
if (Result.X2 < Other.X1) then Result.X2 := Other.X1;
if (Result.X2 > Other.X2) then Result.X2 := Other.X2;

if (Result.Y1 < Other.Y1) then Result.Y1 := Other.Y1;
if (Result.Y1 > Other.Y2) then Result.Y1 := Other.Y2;
if (Result.Y2 < Other.Y1) then Result.Y2 := Other.Y1;
if (Result.Y2 > Other.Y2) then Result.Y2 := Other.Y2;
end;

procedure TBoxHelper.Normalize;
function TBoxHelper.Normalize: TBox;
begin
if (Self.X1 > Self.X2) then
Swap(Self.X1, Self.X2);
if (Self.Y1 > Self.Y2) then
Swap(Self.Y1, Self.Y2);
Result := Self;

if (Result.X1 > Result.X2) then
Swap(Result.X1, Result.X2);
if (Result.Y1 > Result.Y2) then
Swap(Result.Y1, Result.Y2);
end;

function Box(X1, Y1, X2, Y2: Integer): TBox;
function Box(const X1, Y1, X2, Y2: Integer): TBox;
begin
Result.X1 := X1;
Result.Y1 := Y1;
Result.X2 := X2;
Result.Y2 := Y2;
end;

function Box(Mid: TPoint; XRad, YRad: Integer): TBox;
function Box(const Mid: TPoint; const XRad, YRad: Integer): TBox;
begin
Result.X1 := Mid.X-XRad;
Result.Y1 := Mid.Y-YRad;
Expand All @@ -368,7 +370,8 @@ end;

operator = (const Left, Right: TBox): Boolean;
begin
Result := CompareMem(@Left, @Right, SizeOf(TBox));
Result := (Int64(Left.TopLeft) = Int64(Right.TopLeft)) and
(Int64(Left.BottomRight) = Int64(Right.BottomRight));
end;
{$ENDIF}

4 changes: 2 additions & 2 deletions Source/point.inc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type
function Random(Value: Integer): TPoint; overload;
end;

function Point(X, Y: Integer): TPoint;
function Point(const X, Y: Integer): TPoint; inline;

operator = (const Left, Right: TPoint): Boolean;
operator + (const Left, Right: TPoint): TPoint;
Expand Down Expand Up @@ -141,7 +141,7 @@ begin
Result := Left.InBox(Right);
end;

function Point(X, Y: Integer): TPoint;
function Point(const X, Y: Integer): TPoint;
begin
Result.X := X;
Result.Y := Y;
Expand Down
24 changes: 12 additions & 12 deletions Source/script/imports/simba/simba.import_box.pas
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ implementation
~~~
function Box(X1, Y1, X2, Y2: Integer): TBox;
*)
procedure _LapeBox(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
procedure _LapeBox1(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PBox(Result)^ := Box(PInteger(Params^[0])^, PInteger(Params^[1])^, PInteger(Params^[2])^, PInteger(Params^[3])^);
end;
Expand All @@ -31,7 +31,7 @@ procedure _LapeBox(const Params: PParamArray; const Result: Pointer); LAPE_WRAPP
~~~
function Box(Mid: TPoint; XRad, YRad: Integer): TBox;
*)
procedure _LapeBoxEx(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
procedure _LapeBox2(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PBox(Result)^ := Box(PPoint(Params^[0])^, PInteger(Params^[1])^, PInteger(Params^[2])^);
end;
Expand Down Expand Up @@ -253,21 +253,21 @@ procedure _LapeBox_Intersect(const Params: PParamArray; const Result: Pointer);
(*
TBox.Clip
~~~~~~~~~
procedure TBox.Clip(Other: TBox);
function TBox.Clip(Other: TBox): TBox;
*)
procedure _LapeBox_Clip(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
procedure _LapeBox_Clip(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PBox(Params^[0])^.Clip(PBox(Params^[1])^);
PBox(Result)^ := PBox(Params^[0])^.Clip(PBox(Params^[1])^);
end;

(*
TBox.Normalize
~~~~~~~~~~~~~~
procedure TBox.Normalize;
function TBox.Normalize: TBox;
*)
procedure _LapeBox_Normalize(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
procedure _LapeBox_Normalize(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PBox(Params^[0])^.Normalize();
PBox(Result)^ := PBox(Params^[0])^.Normalize();
end;

(*
Expand Down Expand Up @@ -352,8 +352,8 @@ procedure ImportBox(Compiler: TSimbaScript_Compiler);
begin
ImportingSection := 'TBox';

addGlobalFunc('function Box(X1, Y1, X2, Y2: Integer): TBox; overload', @_LapeBox);
addGlobalFunc('function Box(Mid: TPoint; XRad, YRad: Integer): TBox; overload', @_LapeBoxEx);
addGlobalFunc('function Box(X1, Y1, X2, Y2: Integer): TBox; overload', @_LapeBox1);
addGlobalFunc('function Box(Mid: TPoint; XRad, YRad: Integer): TBox; overload', @_LapeBox2);

addGlobalFunc('function TBox.Create(X1, Y1, X2, Y2: Integer): TBox; static; overload;', @_LapeBox_Create1);
addGlobalFunc('function TBox.Create(Center: TPoint; XRad, YRad: Integer): TBox; static; overload;', @_LapeBox_Create2);
Expand All @@ -380,8 +380,8 @@ procedure ImportBox(Compiler: TSimbaScript_Compiler);
addGlobalFunc('function TBox.NearestEdge(P: TPoint): TPoint;', @_LapeBox_NearestEdge);
addGlobalFunc('function TBox.Intersect(P: TPoint): TPoint;', @_LapeBox_Intersect);

addGlobalFunc('procedure TBox.Clip(Other: TBox);', @_LapeBox_Clip);
addGlobalFunc('procedure TBox.Normalize;', @_LapeBox_Normalize);
addGlobalFunc('function TBox.Clip(Other: TBox): TBox', @_LapeBox_Clip);
addGlobalFunc('function TBox.Normalize: TBox', @_LapeBox_Normalize);
addGlobalFunc('function TBox.Corners: TPointArray;', @_LapeBox_Corners);

addGlobalFunc('function TBox.Width: Integer;', @_LapeBox_Width);
Expand Down

0 comments on commit f98270a

Please sign in to comment.