1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-23 00:38:47 +00:00

Add additional ctor overloads for Ellipse/F

This commit is contained in:
Oliver Booth 2022-06-01 17:05:25 +01:00
parent ea56f2be48
commit b0cce087b3
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
5 changed files with 129 additions and 22 deletions

View File

@ -1,4 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Drawing;
using System.Numerics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using X10D.Drawing; using X10D.Drawing;
namespace X10D.Tests.Drawing; namespace X10D.Tests.Drawing;
@ -20,6 +22,26 @@ public class EllipseFTests
Assert.AreEqual(2 * MathF.PI, unitEllipse.ApproximateCircumference, 1e-6f); 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] [TestMethod]
public void Equals_ShouldBeTrue_GivenTwoUnitEllipses() public void Equals_ShouldBeTrue_GivenTwoUnitEllipses()
{ {

View File

@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Drawing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using X10D.Drawing; using X10D.Drawing;
namespace X10D.Tests.Drawing; namespace X10D.Tests.Drawing;
@ -20,6 +21,18 @@ public class EllipseTests
Assert.AreEqual(2 * MathF.PI, unitEllipse.ApproximateCircumference, 1e-6f); 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] [TestMethod]
public void Equals_ShouldBeTrue_GivenTwoUnitEllipses() public void Equals_ShouldBeTrue_GivenTwoUnitEllipses()
{ {

View File

@ -1,5 +1,4 @@
using System.Diagnostics.Contracts; using System.Diagnostics.Contracts;
using System.Drawing;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using UnityEngine; using UnityEngine;

View File

@ -15,24 +15,30 @@ public readonly struct Ellipse : IEquatable<Ellipse>
/// <summary> /// <summary>
/// The unit ellipse. That is, an ellipse whose center point is (0, 0) and whose two radii are 1. /// The unit ellipse. That is, an ellipse whose center point is (0, 0) and whose two radii are 1.
/// </summary> /// </summary>
public static readonly Ellipse Unit = new(Point.Empty, 1, 1); public static readonly Ellipse Unit = new(0, 0, 1, 1);
/// <summary>
/// Initializes a new instance of the <see cref="Ellipse" /> struct.
/// </summary>
/// <param name="centerX">The X coordinate of the center point.</param>
/// <param name="centerY">The Y coordinate of the center point.</param>
/// <param name="horizontalRadius">The horizontal radius of the ellipse.</param>
/// <param name="verticalRadius">The vertical radius of the ellipse.</param>
public Ellipse(int centerX, int centerY, int horizontalRadius, int verticalRadius)
: this(new Point(centerX, centerY), new Size(horizontalRadius, verticalRadius))
{
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Ellipse" /> struct. /// Initializes a new instance of the <see cref="Ellipse" /> struct.
/// </summary> /// </summary>
/// <param name="center">The center point of the ellipse.</param> /// <param name="center">The center point of the ellipse.</param>
/// <param name="horizontalRadius">The horizontal radius of the ellipse.</param> /// <param name="radius">The radius of the ellipse.</param>
/// <param name="verticalRadius">The vertical radius of the ellipse.</param> public Ellipse(Point center, Size radius)
public Ellipse(Point center, int horizontalRadius, int verticalRadius)
{ {
Center = center; Center = center;
HorizontalRadius = horizontalRadius; HorizontalRadius = radius.Width;
VerticalRadius = verticalRadius; VerticalRadius = radius.Height;
}
public static implicit operator Ellipse(Circle circle)
{
return new Ellipse(circle.Center, circle.Radius, circle.Radius);
} }
/// <summary> /// <summary>
@ -77,6 +83,15 @@ public readonly struct Ellipse : IEquatable<Ellipse>
/// <value>The horizontal radius.</value> /// <value>The horizontal radius.</value>
public int HorizontalRadius { get; } public int HorizontalRadius { get; }
/// <summary>
/// Gets the radius of the ellipse.
/// </summary>
/// <value>The radius.</value>
public Size Radius
{
get => new(HorizontalRadius, VerticalRadius);
}
/// <summary> /// <summary>
/// Gets the vertical radius of the ellipse. /// Gets the vertical radius of the ellipse.
/// </summary> /// </summary>
@ -111,6 +126,16 @@ public readonly struct Ellipse : IEquatable<Ellipse>
return !left.Equals(right); return !left.Equals(right);
} }
/// <summary>
/// Implicitly converts a <see cref="Circle" /> to an <see cref="Ellipse" />.
/// </summary>
/// <param name="circle">The circle to convert.</param>
/// <returns>The converted ellipse.</returns>
public static implicit operator Ellipse(Circle circle)
{
return new Ellipse(circle.Center, new Size(circle.Radius, circle.Radius));
}
/// <inheritdoc /> /// <inheritdoc />
public override bool Equals(object? obj) public override bool Equals(object? obj)
{ {

View File

@ -1,4 +1,6 @@
using System.Drawing; using System.Drawing;
using System.Numerics;
using X10D.Numerics;
namespace X10D.Drawing; namespace X10D.Drawing;
@ -15,21 +17,58 @@ public readonly struct EllipseF : IEquatable<EllipseF>
/// <summary> /// <summary>
/// The unit ellipse. That is, an ellipse whose center point is (0, 0) and whose two radii are 1. /// The unit ellipse. That is, an ellipse whose center point is (0, 0) and whose two radii are 1.
/// </summary> /// </summary>
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);
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="EllipseF" /> struct. /// Initializes a new instance of the <see cref="EllipseF" /> struct.
/// </summary> /// </summary>
/// <param name="center">The center point of the ellipse.</param> /// <param name="centerX">The X coordinate of the center point.</param>
/// <param name="centerY">The Y coordinate of the center point.</param>
/// <param name="horizontalRadius">The horizontal radius of the ellipse.</param> /// <param name="horizontalRadius">The horizontal radius of the ellipse.</param>
/// <param name="verticalRadius">The vertical radius of the ellipse.</param> /// <param name="verticalRadius">The vertical radius of the ellipse.</param>
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; HorizontalRadius = horizontalRadius;
VerticalRadius = verticalRadius; VerticalRadius = verticalRadius;
} }
/// <summary>
/// Initializes a new instance of the <see cref="Ellipse" /> struct.
/// </summary>
/// <param name="center">The center point of the ellipse.</param>
/// <param name="radius">The radius of the ellipse.</param>
public EllipseF(PointF center, SizeF radius)
{
Center = center;
HorizontalRadius = radius.Width;
VerticalRadius = radius.Height;
}
/// <summary>
/// Initializes a new instance of the <see cref="Ellipse" /> struct.
/// </summary>
/// <param name="center">The center point of the ellipse.</param>
/// <param name="radius">The radius of the ellipse.</param>
public EllipseF(PointF center, Vector2 radius)
{
Center = center;
HorizontalRadius = radius.X;
VerticalRadius = radius.Y;
}
/// <summary>
/// Initializes a new instance of the <see cref="Ellipse" /> struct.
/// </summary>
/// <param name="center">The center point of the ellipse.</param>
/// <param name="radius">The radius of the ellipse.</param>
public EllipseF(Vector2 center, Vector2 radius)
{
Center = center.ToPointF();
HorizontalRadius = radius.X;
VerticalRadius = radius.Y;
}
/// <summary> /// <summary>
/// Gets the area of the ellipse. /// Gets the area of the ellipse.
/// </summary> /// </summary>
@ -72,6 +111,15 @@ public readonly struct EllipseF : IEquatable<EllipseF>
/// <value>The horizontal radius.</value> /// <value>The horizontal radius.</value>
public float HorizontalRadius { get; } public float HorizontalRadius { get; }
/// <summary>
/// Gets the radius of the ellipse.
/// </summary>
/// <value>The radius.</value>
public SizeF Radius
{
get => new(HorizontalRadius, VerticalRadius);
}
/// <summary> /// <summary>
/// Gets the vertical radius of the ellipse. /// Gets the vertical radius of the ellipse.
/// </summary> /// </summary>
@ -113,7 +161,7 @@ public readonly struct EllipseF : IEquatable<EllipseF>
/// <returns>The converted ellipse.</returns> /// <returns>The converted ellipse.</returns>
public static implicit operator EllipseF(Circle circle) 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));
} }
/// <summary> /// <summary>
@ -123,7 +171,7 @@ public readonly struct EllipseF : IEquatable<EllipseF>
/// <returns>The converted ellipse.</returns> /// <returns>The converted ellipse.</returns>
public static implicit operator EllipseF(CircleF circle) 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));
} }
/// <summary> /// <summary>
@ -133,7 +181,7 @@ public readonly struct EllipseF : IEquatable<EllipseF>
/// <returns>The converted ellipse.</returns> /// <returns>The converted ellipse.</returns>
public static implicit operator EllipseF(Ellipse ellipse) public static implicit operator EllipseF(Ellipse ellipse)
{ {
return new EllipseF(ellipse.Center, ellipse.HorizontalRadius, ellipse.VerticalRadius); return new EllipseF(ellipse.Center, ellipse.Radius);
} }
/// <summary> /// <summary>
@ -144,7 +192,7 @@ public readonly struct EllipseF : IEquatable<EllipseF>
public static explicit operator Ellipse(EllipseF ellipse) public static explicit operator Ellipse(EllipseF ellipse)
{ {
PointF center = ellipse.Center; 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);
} }
/// <inheritdoc /> /// <inheritdoc />