From b0cce087b356c052eb2bee50a650b8e61807276c Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Wed, 1 Jun 2022 17:05:25 +0100 Subject: [PATCH] Add additional ctor overloads for Ellipse/F --- X10D.Tests/src/Drawing/EllipseFTests.cs | 24 ++++++- X10D.Tests/src/Drawing/EllipseTests.cs | 15 ++++- .../src/Numerics/Vector3IntExtensions.cs | 1 - X10D/src/Drawing/Ellipse.cs | 47 ++++++++++---- X10D/src/Drawing/EllipseF.cs | 64 ++++++++++++++++--- 5 files changed, 129 insertions(+), 22 deletions(-) diff --git a/X10D.Tests/src/Drawing/EllipseFTests.cs b/X10D.Tests/src/Drawing/EllipseFTests.cs index 78a7514..750e900 100644 --- a/X10D.Tests/src/Drawing/EllipseFTests.cs +++ b/X10D.Tests/src/Drawing/EllipseFTests.cs @@ -1,4 +1,6 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Drawing; +using System.Numerics; +using Microsoft.VisualStudio.TestTools.UnitTesting; using X10D.Drawing; namespace X10D.Tests.Drawing; @@ -20,6 +22,26 @@ public class EllipseFTests Assert.AreEqual(2 * MathF.PI, unitEllipse.ApproximateCircumference, 1e-6f); } + [TestMethod] + public void Constructor_ShouldGiveCorrectEllipse() + { + var ellipse = new EllipseF(PointF.Empty, new SizeF(2, 1)); + Assert.AreEqual(new PointF(0, 0), ellipse.Center); + Assert.AreEqual(new SizeF(2, 1), ellipse.Radius); + + ellipse = new EllipseF(0, 0, 2, 1); + Assert.AreEqual(new PointF(0, 0), ellipse.Center); + Assert.AreEqual(new SizeF(2, 1), ellipse.Radius); + + ellipse = new EllipseF(PointF.Empty, new Vector2(2, 1)); + Assert.AreEqual(new PointF(0, 0), ellipse.Center); + Assert.AreEqual(new SizeF(2, 1), ellipse.Radius); + + ellipse = new EllipseF(Vector2.Zero, new Vector2(2, 1)); + Assert.AreEqual(new PointF(0, 0), ellipse.Center); + Assert.AreEqual(new SizeF(2, 1), ellipse.Radius); + } + [TestMethod] public void Equals_ShouldBeTrue_GivenTwoUnitEllipses() { diff --git a/X10D.Tests/src/Drawing/EllipseTests.cs b/X10D.Tests/src/Drawing/EllipseTests.cs index 0c62cdc..4ee2132 100644 --- a/X10D.Tests/src/Drawing/EllipseTests.cs +++ b/X10D.Tests/src/Drawing/EllipseTests.cs @@ -1,4 +1,5 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Drawing; +using Microsoft.VisualStudio.TestTools.UnitTesting; using X10D.Drawing; namespace X10D.Tests.Drawing; @@ -20,6 +21,18 @@ public class EllipseTests Assert.AreEqual(2 * MathF.PI, unitEllipse.ApproximateCircumference, 1e-6f); } + [TestMethod] + public void Constructor_ShouldGiveCorrectEllipse() + { + var ellipse = new Ellipse(Point.Empty, new Size(2, 1)); + Assert.AreEqual(new Point(0, 0), ellipse.Center); + Assert.AreEqual(new Size(2, 1), ellipse.Radius); + + ellipse = new Ellipse(0, 0, 2, 1); + Assert.AreEqual(new Point(0, 0), ellipse.Center); + Assert.AreEqual(new Size(2, 1), ellipse.Radius); + } + [TestMethod] public void Equals_ShouldBeTrue_GivenTwoUnitEllipses() { diff --git a/X10D.Unity/src/Numerics/Vector3IntExtensions.cs b/X10D.Unity/src/Numerics/Vector3IntExtensions.cs index 48697aa..863068c 100644 --- a/X10D.Unity/src/Numerics/Vector3IntExtensions.cs +++ b/X10D.Unity/src/Numerics/Vector3IntExtensions.cs @@ -1,5 +1,4 @@ using System.Diagnostics.Contracts; -using System.Drawing; using System.Runtime.CompilerServices; using UnityEngine; diff --git a/X10D/src/Drawing/Ellipse.cs b/X10D/src/Drawing/Ellipse.cs index 950265a..911ea58 100644 --- a/X10D/src/Drawing/Ellipse.cs +++ b/X10D/src/Drawing/Ellipse.cs @@ -15,24 +15,30 @@ public readonly struct Ellipse : IEquatable /// /// The unit ellipse. That is, an ellipse whose center point is (0, 0) and whose two radii are 1. /// - public static readonly Ellipse Unit = new(Point.Empty, 1, 1); + public static readonly Ellipse Unit = new(0, 0, 1, 1); + + /// + /// Initializes a new instance of the struct. + /// + /// The X coordinate of the center point. + /// The Y coordinate of the center point. + /// The horizontal radius of the ellipse. + /// The vertical radius of the ellipse. + public Ellipse(int centerX, int centerY, int horizontalRadius, int verticalRadius) + : this(new Point(centerX, centerY), new Size(horizontalRadius, verticalRadius)) + { + } /// /// Initializes a new instance of the struct. /// /// The center point of the ellipse. - /// The horizontal radius of the ellipse. - /// The vertical radius of the ellipse. - public Ellipse(Point center, int horizontalRadius, int verticalRadius) + /// The radius of the ellipse. + public Ellipse(Point center, Size radius) { Center = center; - HorizontalRadius = horizontalRadius; - VerticalRadius = verticalRadius; - } - - public static implicit operator Ellipse(Circle circle) - { - return new Ellipse(circle.Center, circle.Radius, circle.Radius); + HorizontalRadius = radius.Width; + VerticalRadius = radius.Height; } /// @@ -77,6 +83,15 @@ public readonly struct Ellipse : IEquatable /// The horizontal radius. public int HorizontalRadius { get; } + /// + /// Gets the radius of the ellipse. + /// + /// The radius. + public Size Radius + { + get => new(HorizontalRadius, VerticalRadius); + } + /// /// Gets the vertical radius of the ellipse. /// @@ -111,6 +126,16 @@ public readonly struct Ellipse : IEquatable return !left.Equals(right); } + /// + /// Implicitly converts a to an . + /// + /// The circle to convert. + /// The converted ellipse. + public static implicit operator Ellipse(Circle circle) + { + return new Ellipse(circle.Center, new Size(circle.Radius, circle.Radius)); + } + /// public override bool Equals(object? obj) { diff --git a/X10D/src/Drawing/EllipseF.cs b/X10D/src/Drawing/EllipseF.cs index c07cd2d..d3a9d2d 100644 --- a/X10D/src/Drawing/EllipseF.cs +++ b/X10D/src/Drawing/EllipseF.cs @@ -1,4 +1,6 @@ using System.Drawing; +using System.Numerics; +using X10D.Numerics; namespace X10D.Drawing; @@ -15,21 +17,58 @@ public readonly struct EllipseF : IEquatable /// /// The unit ellipse. That is, an ellipse whose center point is (0, 0) and whose two radii are 1. /// - public static readonly EllipseF Unit = new(PointF.Empty, 1.0f, 1.0f); + public static readonly EllipseF Unit = new(0.0f, 0.0f, 1.0f, 1.0f); /// /// Initializes a new instance of the struct. /// - /// The center point of the ellipse. + /// The X coordinate of the center point. + /// The Y coordinate of the center point. /// The horizontal radius of the ellipse. /// The vertical radius of the ellipse. - public EllipseF(PointF center, float horizontalRadius, float verticalRadius) + public EllipseF(float centerX, float centerY, float horizontalRadius, float verticalRadius) { - Center = center; + Center = new PointF(centerX, centerY); HorizontalRadius = horizontalRadius; VerticalRadius = verticalRadius; } + /// + /// Initializes a new instance of the struct. + /// + /// The center point of the ellipse. + /// The radius of the ellipse. + public EllipseF(PointF center, SizeF radius) + { + Center = center; + HorizontalRadius = radius.Width; + VerticalRadius = radius.Height; + } + + /// + /// Initializes a new instance of the struct. + /// + /// The center point of the ellipse. + /// The radius of the ellipse. + public EllipseF(PointF center, Vector2 radius) + { + Center = center; + HorizontalRadius = radius.X; + VerticalRadius = radius.Y; + } + + /// + /// Initializes a new instance of the struct. + /// + /// The center point of the ellipse. + /// The radius of the ellipse. + public EllipseF(Vector2 center, Vector2 radius) + { + Center = center.ToPointF(); + HorizontalRadius = radius.X; + VerticalRadius = radius.Y; + } + /// /// Gets the area of the ellipse. /// @@ -72,6 +111,15 @@ public readonly struct EllipseF : IEquatable /// The horizontal radius. public float HorizontalRadius { get; } + /// + /// Gets the radius of the ellipse. + /// + /// The radius. + public SizeF Radius + { + get => new(HorizontalRadius, VerticalRadius); + } + /// /// Gets the vertical radius of the ellipse. /// @@ -113,7 +161,7 @@ public readonly struct EllipseF : IEquatable /// The converted ellipse. public static implicit operator EllipseF(Circle circle) { - return new EllipseF(circle.Center, circle.Radius, circle.Radius); + return new EllipseF(circle.Center, new SizeF(circle.Radius, circle.Radius)); } /// @@ -123,7 +171,7 @@ public readonly struct EllipseF : IEquatable /// The converted ellipse. public static implicit operator EllipseF(CircleF circle) { - return new EllipseF(circle.Center, circle.Radius, circle.Radius); + return new EllipseF(circle.Center, new SizeF(circle.Radius, circle.Radius)); } /// @@ -133,7 +181,7 @@ public readonly struct EllipseF : IEquatable /// The converted ellipse. public static implicit operator EllipseF(Ellipse ellipse) { - return new EllipseF(ellipse.Center, ellipse.HorizontalRadius, ellipse.VerticalRadius); + return new EllipseF(ellipse.Center, ellipse.Radius); } /// @@ -144,7 +192,7 @@ public readonly struct EllipseF : IEquatable public static explicit operator Ellipse(EllipseF ellipse) { PointF center = ellipse.Center; - return new Ellipse(new Point((int)center.X, (int)center.Y), (int)ellipse.HorizontalRadius, (int)ellipse.VerticalRadius); + return new Ellipse((int)center.X, (int)center.Y, (int)ellipse.HorizontalRadius, (int)ellipse.VerticalRadius); } ///