Skip to content

Commit

Permalink
make Box.Inflate work like System.Drawing.Rectangle.Inflate
Browse files Browse the repository at this point in the history
  • Loading branch information
MV10 committed Oct 15, 2023
1 parent bc9c634 commit 7146451
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 89 deletions.
23 changes: 12 additions & 11 deletions src/OpenTK.Mathematics/Geometry/Box2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,28 +261,29 @@ public Box2 Scaled(Vector2 scale, Vector2 anchor)
}

/// <summary>
/// Inflate this Box2 to encapsulate a given point.
/// Inflates this Box2 by the given size in all directions. A negative size will shrink the box to a maximum of -HalfSize.
/// Use the <see cref="Extend"/> method for the point-encapsulation functionality in earlier API versions.
/// </summary>
/// <param name="point">The point to inflate to.</param>
[Obsolete("Use " + nameof(Extend) + " instead. This function will have it's implementation changed in the future.")]
public void Inflate(Vector2 point)
/// <param name="size">The size to inflate by.</param>
public void Inflate(Vector2 size)
{
_min = Vector2.ComponentMin(_min, point);
_max = Vector2.ComponentMax(_max, point);
size = Vector2.ComponentMax(size, -HalfSize);
_min -= size;
_max += size;
}

/// <summary>
/// Inflate this Box2 to encapsulate a given point.
/// Inflates this Box2 by the given size in all directions. A negative size will shrink the box to a maximum of -HalfSize.
/// Use the <see cref="Extended"/> method for the point-encapsulation functionality in earlier API versions.
/// </summary>
/// <param name="point">The point to inflate to.</param>
/// <param name="size">The size to inflate by.</param>
/// <returns>The inflated box.</returns>
[Pure]
[Obsolete("Use " + nameof(Extended) + " instead. This function will have it's implementation changed in the future.")]
public Box2 Inflated(Vector2 point)
public Box2 Inflated(Vector2 size)
{
// create a local copy of this box
Box2 box = this;
box.Inflate(point);
box.Inflate(size);
return box;
}

Expand Down
23 changes: 12 additions & 11 deletions src/OpenTK.Mathematics/Geometry/Box2d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,28 +261,29 @@ public Box2d Scaled(Vector2d scale, Vector2d anchor)
}

/// <summary>
/// Inflate this Box2d to encapsulate a given point.
/// Inflates this Box2d by the given size in all directions. A negative size will shrink the box to a maximum of -HalfSize.
/// Use the <see cref="Extend"/> method for the point-encapsulation functionality in earlier API versions.
/// </summary>
/// <param name="point">The point to query.</param>
[Obsolete("Use " + nameof(Extend) + " instead. This function will have it's implementation changed in the future.")]
public void Inflate(Vector2d point)
/// <param name="size">The size to inflate by.</param>
public void Inflate(Vector2d size)
{
_min = Vector2d.ComponentMin(_min, point);
_max = Vector2d.ComponentMax(_max, point);
size = Vector2d.ComponentMax(size, -HalfSize);
_min -= size;
_max += size;
}

/// <summary>
/// Inflate this Box2d to encapsulate a given point.
/// Inflates this Box2d by the given size in all directions. A negative size will shrink the box to a maximum of -HalfSize.
/// Use the <see cref="Extended"/> method for the point-encapsulation functionality in earlier API versions.
/// </summary>
/// <param name="point">The point to query.</param>
/// <param name="size">The size to inflate by.</param>
/// <returns>The inflated box.</returns>
[Pure]
[Obsolete("Use " + nameof(Extended) + " instead. This function will have it's implementation changed in the future.")]
public Box2d Inflated(Vector2d point)
public Box2d Inflated(Vector2d size)
{
// create a local copy of this box
Box2d box = this;
box.Inflate(point);
box.Inflate(size);
return box;
}

Expand Down
23 changes: 12 additions & 11 deletions src/OpenTK.Mathematics/Geometry/Box2i.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,28 +271,29 @@ public Box2i Scaled(Vector2i scale, Vector2i anchor)
}

/// <summary>
/// Inflate this Box2i to encapsulate a given point.
/// Inflates this Box2i by the given size in all directions. A negative size will shrink the box to a maximum of -HalfSize.
/// Use the <see cref="Extend"/> method for the point-encapsulation functionality in earlier API versions.
/// </summary>
/// <param name="point">The point to query.</param>
[Obsolete("Use " + nameof(Extend) + " instead. This function will have it's implementation changed in the future.")]
public void Inflate(Vector2i point)
/// <param name="size">The size to inflate by.</param>
public void Inflate(Vector2i size)
{
_min = Vector2i.ComponentMin(_min, point);
_max = Vector2i.ComponentMax(_max, point);
size = Vector2i.ComponentMax(size, -HalfSize);
_min -= size;
_max += size;
}

/// <summary>
/// Inflate this Box2i to encapsulate a given point.
/// Inflates this Box2i by the given size in all directions. A negative size will shrink the box to a maximum of -HalfSize.
/// Use the <see cref="Extended"/> method for the point-encapsulation functionality in earlier API versions.
/// </summary>
/// <param name="point">The point to query.</param>
/// <param name="size">The size to inflate by.</param>
/// <returns>The inflated box.</returns>
[Pure]
[Obsolete("Use " + nameof(Extended) + " instead. This function will have it's implementation changed in the future.")]
public Box2i Inflated(Vector2i point)
public Box2i Inflated(Vector2i size)
{
// create a local copy of this box
Box2i box = this;
box.Inflate(point);
box.Inflate(size);
return box;
}

Expand Down
23 changes: 12 additions & 11 deletions src/OpenTK.Mathematics/Geometry/Box3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,28 +276,29 @@ public Box3 Scaled(Vector3 scale, Vector3 anchor)
}

/// <summary>
/// Inflate this Box3 to encapsulate a given point.
/// Inflates this Box3 by the given size in all directions. A negative size will shrink the box to a maximum of -HalfSize.
/// Use the <see cref="Extend"/> method for the point-encapsulation functionality in earlier API versions.
/// </summary>
/// <param name="point">The point to query.</param>
[Obsolete("Use " + nameof(Extend) + " instead. This function will have it's implementation changed in the future.")]
public void Inflate(Vector3 point)
/// <param name="size">The size to inflate by.</param>
public void Inflate(Vector3 size)
{
_min = Vector3.ComponentMin(_min, point);
_max = Vector3.ComponentMax(_max, point);
size = Vector3.ComponentMax(size, -HalfSize);
_min -= size;
_max += size;
}

/// <summary>
/// Inflate this Box3 to encapsulate a given point.
/// Inflates this Box3 by the given size in all directions. A negative size will shrink the box to a maximum of -HalfSize.
/// Use the <see cref="Extended"/> method for the point-encapsulation functionality in earlier API versions.
/// </summary>
/// <param name="point">The point to query.</param>
/// <param name="size">The size to inflate by.</param>
/// <returns>The inflated box.</returns>
[Pure]
[Obsolete("Use " + nameof(Extended) + " instead. This function will have it's implementation changed in the future.")]
public Box3 Inflated(Vector3 point)
public Box3 Inflated(Vector3 size)
{
// create a local copy of this box
Box3 box = this;
box.Inflate(point);
box.Inflate(size);
return box;
}

Expand Down
23 changes: 12 additions & 11 deletions src/OpenTK.Mathematics/Geometry/Box3d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,28 +276,29 @@ public Box3d Scaled(Vector3d scale, Vector3d anchor)
}

/// <summary>
/// Inflate this Box3d to encapsulate a given point.
/// Inflates this Box3d by the given size in all directions. A negative size will shrink the box to a maximum of -HalfSize.
/// Use the <see cref="Extend"/> method for the point-encapsulation functionality in earlier API versions.
/// </summary>
/// <param name="point">The point to query.</param>
[Obsolete("Use " + nameof(Extend) + " instead. This function will have it's implementation changed in the future.")]
public void Inflate(Vector3d point)
/// <param name="size">The size to inflate by.</param>
public void Inflate(Vector3d size)
{
_min = Vector3d.ComponentMin(_min, point);
_max = Vector3d.ComponentMax(_max, point);
size = Vector3d.ComponentMax(size, -HalfSize);
_min -= size;
_max += size;
}

/// <summary>
/// Inflate this Box3d to encapsulate a given point.
/// Inflates this Box3d by the given size in all directions. A negative size will shrink the box to a maximum of -HalfSize.
/// Use the <see cref="Extended"/> method for the point-encapsulation functionality in earlier API versions.
/// </summary>
/// <param name="point">The point to query.</param>
/// <param name="size">The size to inflate by.</param>
/// <returns>The inflated box.</returns>
[Pure]
[Obsolete("Use " + nameof(Extended) + " instead. This function will have it's implementation changed in the future.")]
public Box3d Inflated(Vector3d point)
public Box3d Inflated(Vector3d size)
{
// create a local copy of this box
Box3d box = this;
box.Inflate(point);
box.Inflate(size);
return box;
}

Expand Down
23 changes: 12 additions & 11 deletions src/OpenTK.Mathematics/Geometry/Box3i.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,28 +256,29 @@ public Box3i Scaled(Vector3i scale, Vector3i anchor)
}

/// <summary>
/// Inflate this Box3i to encapsulate a given point.
/// Inflates this Box3i by the given size in all directions. A negative size will shrink the box to a maximum of -HalfSize.
/// Use the <see cref="Extend"/> method for the point-encapsulation functionality in earlier API versions.
/// </summary>
/// <param name="point">The point to query.</param>
[Obsolete("Use " + nameof(Extend) + " instead. This function will have it's implementation changed in the future.")]
public void Inflate(Vector3i point)
/// <param name="size">The size to inflate by.</param>
public void Inflate(Vector3i size)
{
_min = Vector3i.ComponentMin(_min, point);
_max = Vector3i.ComponentMax(_max, point);
size = Vector3i.ComponentMax(size, -HalfSize);
_min -= size;
_max += size;
}

/// <summary>
/// Inflate this Box3i to encapsulate a given point.
/// Inflates this Box3i by the given size in all directions. A negative size will shrink the box to a maximum of -HalfSize.
/// Use the <see cref="Extended"/> method for the point-encapsulation functionality in earlier API versions.
/// </summary>
/// <param name="point">The point to query.</param>
/// <param name="size">The size to inflate by.</param>
/// <returns>The inflated box.</returns>
[Pure]
[Obsolete("Use " + nameof(Extended) + " instead. This function will have it's implementation changed in the future.")]
public Box3i Inflated(Vector3i point)
public Box3i Inflated(Vector3i size)
{
// create a local copy of this box
Box3i box = this;
box.Inflate(point);
box.Inflate(size);
return box;
}

Expand Down
28 changes: 16 additions & 12 deletions tests/OpenTK.Tests/Box2Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -157,25 +157,29 @@ module Box2 =
[<Properties(Arbitrary = [|typeof<OpenTKGen>|])>]
module Inflate =
[<Property>]
let ``After inflating a box the point should be either on the edge of the box or the box shouldn't change size`` (b1 : Box2, v1 : Vector2) =
let v2 = b1.Size

b1.Inflate(v1)

Assert.True(b1.DistanceToNearestEdge(v1) = (float32)0 || v2 = b1.Size)

[<Property>]
let ``After inflating the point should be enclosed in the box`` (b1 : Box2, v1 : Vector2) =
Assert.True(b1.Inflated(v1).Contains(v1, true))
let ``Box2.Inflate produces the expected min and max changes`` (b1 : Box2, v1 : Vector2) =
let size = Vector2.ComponentMax(v1, -b1.HalfSize);
let b = Box2(b1.Min - size, b1.Max + size)
Assert.Equal(b, b1.Inflated(v1))

[<Property>]
let ``Box2.Inflate is equivalent to Box2.Inflated`` (b1 : Box2, v1 : Vector2) =
let mutable b = b1

b.Inflate(v1)

Assert.Equal(b, b1.Inflated(v1))

[<Properties(Arbitrary = [|typeof<OpenTKGen>|])>]
module Extend =
[<Property>]
let ``After extending the point should be enclosed in the box`` (b1 : Box2, v1 : Vector2) =
Assert.True(b1.Extended(v1).Contains(v1, true))

[<Property>]
let ``Box2.Extend is equivalent to Box2.Extended`` (b1 : Box2, v1 : Vector2) =
let mutable b = b1
b.Extend(v1)
Assert.Equal(b, b1.Extended(v1))

[<Properties(Arbitrary = [|typeof<OpenTKGen>|])>]
module Center =
[<Property>]
Expand Down
32 changes: 21 additions & 11 deletions tests/OpenTK.Tests/Box3Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -159,24 +159,34 @@ module Box3 =
[<Properties(Arbitrary = [|typeof<OpenTKGen>|])>]
module Inflate =
[<Property>]
let ``After inflating a box the point should be either on the edge of the box or the box shouldn't change size`` (b1 : Box3, v1 : Vector3) =
let v2 = b1.Size

b1.Inflate(v1)
let ``Box3.Inflate produces the expected min and max changes`` (b1 : Box3, v1 : Vector3) =
let size = Vector3.ComponentMax(v1, -b1.HalfSize);
let b = Box3(b1.Min - size, b1.Max + size)
Assert.Equal(b, b1.Inflated(v1))

[<Property>]
let ``Box3.Inflate is equivalent to Box3.Inflated`` (b1 : Box3, v1 : Vector3) =
let mutable b = b1
b.Inflate(v1)
Assert.Equal(b, b1.Inflated(v1))

[<Properties(Arbitrary = [|typeof<OpenTKGen>|])>]
module Extend =
[<Property>]
let ``After extending a box the point should be either on the edge of the box or the box shouldn't change size`` (b1 : Box3, v1 : Vector3) =
let v2 = b1.Size
b1.Extend(v1)
Assert.True(b1.DistanceToNearestEdge(v1) = (float32)0 || v2 = b1.Size)

[<Property>]
let ``After inflating the point should be enclosed in the box`` (b1 : Box3, v1 : Vector3) =
Assert.True(b1.Inflated(v1).Contains(v1, true))
let ``After extending the point should be enclosed in the box`` (b1 : Box3, v1 : Vector3) =
Assert.True(b1.Extended(v1).Contains(v1, true))

[<Property>]
let ``Box3.Inflate is equivalent to Box3.Inflated`` (b1 : Box3, v1 : Vector3) =
let ``Box3.Extend is equivalent to Box3.Extended`` (b1 : Box3, v1 : Vector3) =
let mutable b = b1

b.Inflate(v1)

Assert.Equal(b, b1.Inflated(v1))
b.Extend(v1)
Assert.Equal(b, b1.Extended(v1))

[<Properties(Arbitrary = [|typeof<OpenTKGen>|])>]
module Center =
Expand Down

0 comments on commit 7146451

Please sign in to comment.