Skip to content

Commit df35bd2

Browse files
authored
πŸ› Fix Rectangle.FromSides creation (#380)
* πŸ› Fix Rectangle.FromSides creation * βͺ Revert accidental change made by Rider
1 parent 5b4622c commit df35bd2

3 files changed

Lines changed: 83 additions & 9 deletions

File tree

β€ŽBearded.Utilities.Tests/Bearded.Utilities.Tests.csprojβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<LangVersion>10</LangVersion>
44
<Nullable>enable</Nullable>
55
<WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors>
6-
<TargetFrameworks>net5.0;net6.0;netcoreapp3.1</TargetFrameworks>
6+
<TargetFrameworks>net6.0;netcoreapp3.1</TargetFrameworks>
77
</PropertyGroup>
88
<ItemGroup>
99
<PackageReference Include="FluentAssertions" Version="6.12.0" />
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
ο»Ώusing System;
2+
using Bearded.Utilities.Geometry;
3+
using Bearded.Utilities.Tests.Generators;
4+
using FluentAssertions;
5+
using FsCheck.Xunit;
6+
using OpenTK.Mathematics;
7+
8+
namespace Bearded.Utilities.Tests.Geometry;
9+
10+
public sealed class RectangleTest
11+
{
12+
private const float epsilon = 0.01f;
13+
14+
[Property(Arbitrary = new[] { typeof(FloatGenerators.ForArithmetic) })]
15+
public void RectangleMadeFromXyWidthHeightHasCorrectDimensions(float x, float y, float w, float h)
16+
{
17+
if (w < 0) w *= -1;
18+
if (h < 0) h *= -1;
19+
20+
var rectangle = new Rectangle(x, y, w, h);
21+
22+
// from input
23+
rectangle.Left.Should().BeApproximately(x, epsilon);
24+
rectangle.Top.Should().BeApproximately(y, epsilon);
25+
rectangle.Width.Should().BeApproximately(w, epsilon);
26+
rectangle.Height.Should().BeApproximately(h, epsilon);
27+
28+
// derived
29+
rectangle.Right.Should().BeApproximately(x + w, epsilon);
30+
rectangle.Bottom.Should().BeApproximately(y + h, epsilon);
31+
}
32+
33+
[Property(Arbitrary = new[] { typeof(FloatGenerators.ForArithmetic) })]
34+
public void RectangleMadeFromSidesHasCorrectDimensions(float x1, float x2, float y1, float y2)
35+
{
36+
var top = Math.Min(y1, y2);
37+
var right = Math.Max(x1, x2);
38+
var bottom = Math.Max(y1, y2);
39+
var left = Math.Min(x1, x2);
40+
41+
var rectangle = Rectangle.WithSides(top, right, bottom, left);
42+
43+
// from input
44+
rectangle.Top.Should().BeApproximately(top, epsilon);
45+
rectangle.Right.Should().BeApproximately(right, epsilon);
46+
rectangle.Bottom.Should().BeApproximately(bottom, epsilon);
47+
rectangle.Left.Should().BeApproximately(left, epsilon);
48+
49+
// derived
50+
rectangle.Width.Should().BeApproximately(right - left, epsilon);
51+
rectangle.Height.Should().BeApproximately(bottom - top, epsilon);
52+
}
53+
54+
[Property(Arbitrary = new[] { typeof(FloatGenerators.ForArithmetic) })]
55+
public void RectangleMadeFromCornersHasCorrectDimensions(float x1, float x2, float y1, float y2)
56+
{
57+
var top = Math.Min(y1, y2);
58+
var right = Math.Max(x1, x2);
59+
var bottom = Math.Max(y1, y2);
60+
var left = Math.Min(x1, x2);
61+
62+
var rectangle = Rectangle.WithCorners(new Vector2(left, top), new Vector2(right, bottom));
63+
64+
// from input
65+
rectangle.Top.Should().BeApproximately(top, epsilon);
66+
rectangle.Right.Should().BeApproximately(right, epsilon);
67+
rectangle.Bottom.Should().BeApproximately(bottom, epsilon);
68+
rectangle.Left.Should().BeApproximately(left, epsilon);
69+
70+
// derived
71+
rectangle.Width.Should().BeApproximately(right - left, epsilon);
72+
rectangle.Height.Should().BeApproximately(bottom - top, epsilon);
73+
}
74+
}

β€ŽBearded.Utilities/Geometry/Rectangle.csβ€Ž

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ namespace Bearded.Utilities.Geometry;
1919
public float Right => Left + Width;
2020
public float Bottom => Top + Height;
2121

22-
public Vector2 TopLeft => new Vector2(Left, Top);
23-
public Vector2 TopRight => new Vector2(Right, Top);
24-
public Vector2 BottomLeft => new Vector2(Left, Bottom);
25-
public Vector2 BottomRight => new Vector2(Right, Bottom);
22+
public Vector2 TopLeft => new(Left, Top);
23+
public Vector2 TopRight => new(Right, Top);
24+
public Vector2 BottomLeft => new(Left, Bottom);
25+
public Vector2 BottomRight => new(Right, Bottom);
2626

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

2929
public float Area => Width * Height;
3030

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

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

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

6464
public bool Equals(Rectangle other)
6565
// ReSharper disable CompareOfFloatsByEqualityOperator

0 commit comments

Comments
Β (0)