Skip to content

Commit

Permalink
🐛 Fix Rectangle.FromSides creation
Browse files Browse the repository at this point in the history
  • Loading branch information
tomrijnbeek committed Mar 4, 2024
1 parent 5b4622c commit 088d129
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Bearded.Utilities.Testing\Bearded.Utilities.Testing.csproj" />
<ProjectReference Include="..\Bearded.Utilities\Bearded.Utilities.csproj" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion Bearded.Utilities.Tests/Bearded.Utilities.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<LangVersion>10</LangVersion>
<Nullable>enable</Nullable>
<WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors>
<TargetFrameworks>net5.0;net6.0;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks>net6.0;netcoreapp3.1</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
Expand Down
74 changes: 74 additions & 0 deletions Bearded.Utilities.Tests/Geometry/RectangleTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using Bearded.Utilities.Geometry;
using Bearded.Utilities.Tests.Generators;
using FluentAssertions;
using FsCheck.Xunit;
using OpenTK.Mathematics;

namespace Bearded.Utilities.Tests.Geometry;

public sealed class RectangleTest
{
private const float epsilon = 0.01f;

[Property(Arbitrary = new[] { typeof(FloatGenerators.ForArithmetic) })]
public void RectangleMadeFromXyWidthHeightHasCorrectDimensions(float x, float y, float w, float h)
{
if (w < 0) w *= -1;
if (h < 0) h *= -1;

var rectangle = new Rectangle(x, y, w, h);

// from input
rectangle.Left.Should().BeApproximately(x, epsilon);
rectangle.Top.Should().BeApproximately(y, epsilon);
rectangle.Width.Should().BeApproximately(w, epsilon);
rectangle.Height.Should().BeApproximately(h, epsilon);

// derived
rectangle.Right.Should().BeApproximately(x + w, epsilon);
rectangle.Bottom.Should().BeApproximately(y + h, epsilon);
}

[Property(Arbitrary = new[] { typeof(FloatGenerators.ForArithmetic) })]
public void RectangleMadeFromSidesHasCorrectDimensions(float x1, float x2, float y1, float y2)
{
var top = Math.Min(y1, y2);
var right = Math.Max(x1, x2);
var bottom = Math.Max(y1, y2);
var left = Math.Min(x1, x2);

var rectangle = Rectangle.WithSides(top, right, bottom, left);

// from input
rectangle.Top.Should().BeApproximately(top, epsilon);
rectangle.Right.Should().BeApproximately(right, epsilon);
rectangle.Bottom.Should().BeApproximately(bottom, epsilon);
rectangle.Left.Should().BeApproximately(left, epsilon);

// derived
rectangle.Width.Should().BeApproximately(right - left, epsilon);
rectangle.Height.Should().BeApproximately(bottom - top, epsilon);
}

[Property(Arbitrary = new[] { typeof(FloatGenerators.ForArithmetic) })]
public void RectangleMadeFromCornersHasCorrectDimensions(float x1, float x2, float y1, float y2)
{
var top = Math.Min(y1, y2);
var right = Math.Max(x1, x2);
var bottom = Math.Max(y1, y2);
var left = Math.Min(x1, x2);

var rectangle = Rectangle.WithCorners(new Vector2(left, top), new Vector2(right, bottom));

// from input
rectangle.Top.Should().BeApproximately(top, epsilon);
rectangle.Right.Should().BeApproximately(right, epsilon);
rectangle.Bottom.Should().BeApproximately(bottom, epsilon);
rectangle.Left.Should().BeApproximately(left, epsilon);

// derived
rectangle.Width.Should().BeApproximately(right - left, epsilon);
rectangle.Height.Should().BeApproximately(bottom - top, epsilon);
}
}
16 changes: 8 additions & 8 deletions Bearded.Utilities/Geometry/Rectangle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ namespace Bearded.Utilities.Geometry;
public float Right => Left + Width;
public float Bottom => Top + Height;

public Vector2 TopLeft => new Vector2(Left, Top);
public Vector2 TopRight => new Vector2(Right, Top);
public Vector2 BottomLeft => new Vector2(Left, Bottom);
public Vector2 BottomRight => new Vector2(Right, Bottom);
public Vector2 TopLeft => new(Left, Top);
public Vector2 TopRight => new(Right, Top);
public Vector2 BottomLeft => new(Left, Bottom);
public Vector2 BottomRight => new(Right, Bottom);

public Vector2 Center => new Vector2(Left + Width * 0.5f, Top + Height * 0.5f);
public Vector2 Center => new(Left + Width * 0.5f, Top + Height * 0.5f);

public float Area => Width * Height;

Expand Down Expand Up @@ -56,10 +56,10 @@ public bool Intersects(Rectangle other)
|| other.Top > Bottom || other.Bottom < Top);

public static Rectangle WithCorners(Vector2 upperLeft, Vector2 bottomRight)
=> new Rectangle(upperLeft.X, upperLeft.Y, bottomRight.X - upperLeft.X, bottomRight.Y - upperLeft.Y);
=> new(upperLeft.X, upperLeft.Y, bottomRight.X - upperLeft.X, bottomRight.Y - upperLeft.Y);

public static Rectangle WithSides(float top, float right, float bottom, float left)
=> new Rectangle(top, left, bottom - top, right - left);
public static Rectangle WithSides(float top, float right, float bottom, float left) =>
new(left, top, right - left, bottom - top);

public bool Equals(Rectangle other)
// ReSharper disable CompareOfFloatsByEqualityOperator
Expand Down

0 comments on commit 088d129

Please sign in to comment.