Skip to content

Commit 0d1bc79

Browse files
authored
Fixes for Box/Rectangle/Cube (#485)
* Fix GetScaled behavior * Add GetScaled overloads with TScale
1 parent 5c8862a commit 0d1bc79

File tree

5 files changed

+74
-11
lines changed

5 files changed

+74
-11
lines changed

src/Maths/Silk.NET.Maths/Box2D.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,24 @@ public Box2D<T> GetTranslated(Vector2D<T> distance)
9898
/// <returns>The calculated box.</returns>
9999
public Box2D<T> GetScaled(Vector2D<T> scale, Vector2D<T> anchor)
100100
{
101-
var min = (scale * (Min - anchor)) + Min;
102-
var max = (scale * (Max - anchor)) + Max;
101+
var min = (scale * (Min - anchor)) + anchor;
102+
var max = (scale * (Max - anchor)) + anchor;
103103
return new(min, max);
104104
}
105105

106+
/// <summary>
107+
/// Calculates a new box scaled by the given scale around the given anchor.
108+
/// </summary>
109+
/// <param name="scale">The scale.</param>
110+
/// <param name="anchor">The anchor.</param>
111+
/// <typeparam name="TScale">The type of the scale.</typeparam>
112+
/// <returns>The calculated box.</returns>
113+
public Box2D<T> GetScaled<TScale>(Vector2D<TScale> scale, Vector2D<T> anchor)
114+
where TScale : unmanaged, IFormattable, IEquatable<TScale>, IComparable<TScale>
115+
{
116+
return this.As<TScale>().GetScaled(scale, anchor.As<TScale>()).As<T>();
117+
}
118+
106119
/// <summary>
107120
/// Calculates a box inflated to contain the given point.
108121
/// </summary>

src/Maths/Silk.NET.Maths/Box3D.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,26 @@ public Box3D<T> GetTranslated(Vector3D<T> distance)
103103
/// <returns>The calculated box.</returns>
104104
public Box3D<T> GetScaled(Vector3D<T> scale, Vector3D<T> anchor)
105105
{
106-
var min = (scale * (Min - anchor)) + Min;
107-
var max = (scale * (Max - anchor)) + Max;
106+
var min = (scale * (Min - anchor)) + anchor;
107+
var max = (scale * (Max - anchor)) + anchor;
108108
return new(min, max);
109109
}
110+
111+
/// <summary>
112+
/// Calculates a new box scaled by the given scale around the given anchor.
113+
/// </summary>
114+
/// <typeparam name="TScale">The type of the scale.</typeparam>
115+
/// <param name="scale">The scale.</param>
116+
/// <param name="anchor">The anchor.</param>
117+
/// <returns>The calculated box.</returns>
118+
public Box3D<T> GetScaled<TScale>(Vector3D<TScale> scale, Vector3D<T> anchor)
119+
where TScale : unmanaged, IFormattable, IEquatable<TScale>, IComparable<TScale>
120+
{
121+
var convertedAnchor = anchor.As<TScale>();
122+
var min = (scale * (Min.As<TScale>() - convertedAnchor)) + convertedAnchor;
123+
var max = (scale * (Max.As<TScale>() - convertedAnchor)) + convertedAnchor;
124+
return new Box3D<T>(min.As<T>(), max.As<T>());
125+
}
110126

111127
/// <summary>
112128
/// Calculates a box inflated to contain the given point.

src/Maths/Silk.NET.Maths/Cube.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,26 @@ public Cube<T> GetTranslated(Vector3D<T> distance)
115115
/// <returns>The calculated cube.</returns>
116116
public Cube<T> GetScaled(Vector3D<T> scale, Vector3D<T> anchor)
117117
{
118-
var origMax = Max;
119-
var min = (scale * (Origin - anchor)) + Origin;
120-
var max = (scale * (origMax - anchor)) + origMax;
118+
var min = (scale * (Origin - anchor)) + anchor;
119+
var max = (scale * (Max - anchor)) + anchor;
121120
return new(min, max - min);
122121
}
122+
123+
/// <summary>
124+
/// Calculates a new cube scaled by the given scale around the given anchor.
125+
/// </summary>
126+
/// <typeparam name="TScale">The type of the scale.</typeparam>
127+
/// <param name="scale">The scale.</param>
128+
/// <param name="anchor">The anchor.</param>
129+
/// <returns>The calculated cube.</returns>
130+
public Cube<T> GetScaled<TScale>(Vector3D<TScale> scale, Vector3D<T> anchor)
131+
where TScale : unmanaged, IFormattable, IEquatable<TScale>, IComparable<TScale>
132+
{
133+
var convertedAnchor = anchor.As<TScale>();
134+
var min = (scale * (Origin.As<TScale>() - convertedAnchor)) + convertedAnchor;
135+
var max = (scale * (Max.As<TScale>() - convertedAnchor)) + convertedAnchor;
136+
return new(min.As<T>(), (max - min).As<T>());
137+
}
123138

124139
/// <summary>
125140
/// Calculates a cube inflated to contain the given point.

src/Maths/Silk.NET.Maths/PublicAPI.Unshipped.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ Silk.NET.Maths.Rectangle<T>.As<TOther>() -> Silk.NET.Maths.Rectangle<TOther>
2020
Silk.NET.Maths.Sphere<T>.As<TOther>() -> Silk.NET.Maths.Sphere<TOther>
2121
Silk.NET.Maths.Vector2D<T>.As<TOther>() -> Silk.NET.Maths.Vector2D<TOther>
2222
Silk.NET.Maths.Vector3D<T>.As<TOther>() -> Silk.NET.Maths.Vector3D<TOther>
23-
Silk.NET.Maths.Vector4D<T>.As<TOther>() -> Silk.NET.Maths.Vector4D<TOther>
23+
Silk.NET.Maths.Vector4D<T>.As<TOther>() -> Silk.NET.Maths.Vector4D<TOther>
24+
Silk.NET.Maths.Box2D<T>.GetScaled<TScale>(Silk.NET.Maths.Vector2D<TScale> scale, Silk.NET.Maths.Vector2D<T> anchor) -> Silk.NET.Maths.Box2D<T>
25+
Silk.NET.Maths.Box3D<T>.GetScaled<TScale>(Silk.NET.Maths.Vector3D<TScale> scale, Silk.NET.Maths.Vector3D<T> anchor) -> Silk.NET.Maths.Box3D<T>
26+
Silk.NET.Maths.Rectangle<T>.GetScaled<TScale>(Silk.NET.Maths.Vector2D<TScale> scale, Silk.NET.Maths.Vector2D<T> anchor) -> Silk.NET.Maths.Rectangle<T>
27+
Silk.NET.Maths.Cube<T>.GetScaled<TScale>(Silk.NET.Maths.Vector3D<TScale> scale, Silk.NET.Maths.Vector3D<T> anchor) -> Silk.NET.Maths.Cube<T>

src/Maths/Silk.NET.Maths/Rectangle.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,26 @@ public Rectangle<T> GetTranslated(Vector2D<T> distance)
111111
/// <returns>The calculated rectangle.</returns>
112112
public Rectangle<T> GetScaled(Vector2D<T> scale, Vector2D<T> anchor)
113113
{
114-
var origMax = Max;
115-
var min = (scale * (Origin - anchor)) + Origin;
116-
var max = (scale * (origMax - anchor)) + origMax;
114+
var min = (scale * (Origin - anchor)) + anchor;
115+
var max = (scale * (Max - anchor)) + anchor;
117116
return new(min, max - min);
118117
}
118+
119+
/// <summary>
120+
/// Calculates a new rectangle scaled by the given scale around the given anchor.
121+
/// </summary>
122+
/// <typeparam name="TScale">The type of the scale.</typeparam>
123+
/// <param name="scale">The scale.</param>
124+
/// <param name="anchor">The anchor.</param>
125+
/// <returns>The calculated rectangle.</returns>
126+
public Rectangle<T> GetScaled<TScale>(Vector2D<TScale> scale, Vector2D<T> anchor)
127+
where TScale : unmanaged, IFormattable, IEquatable<TScale>, IComparable<TScale>
128+
{
129+
var convertedAnchor = anchor.As<TScale>();
130+
var min = (scale * (Origin.As<TScale>() - convertedAnchor)) + convertedAnchor;
131+
var max = (scale * (Max.As<TScale>() - convertedAnchor)) + convertedAnchor;
132+
return new(min.As<T>(), (max - min).As<T>());
133+
}
119134

120135
/// <summary>
121136
/// Calculates a rectangle inflated to contain the given point.

0 commit comments

Comments
 (0)