Fix CA2225 violations

This commit is contained in:
Oliver Booth 2022-11-29 17:10:10 +00:00
parent c27334bdc8
commit 398b0c58b1
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
10 changed files with 292 additions and 77 deletions

View File

@ -162,6 +162,27 @@ public readonly struct Circle : IEquatable<Circle>, IComparable<Circle>, ICompar
return left.CompareTo(right) >= 0; return left.CompareTo(right) >= 0;
} }
/// <summary>
/// Explicitly converts a <see cref="Circle" /> to a <see cref="CircleF" />.
/// </summary>
/// <param name="circle">The circle to convert.</param>
/// <returns>The converted circle.</returns>
public static explicit operator Circle(CircleF circle)
{
return Circle.FromCircleF(circle);
}
/// <summary>
/// Converts a <see cref="Circle" /> to a <see cref="CircleF" />.
/// </summary>
/// <param name="circle">The circle to convert.</param>
/// <returns>The converted circle.</returns>
public static Circle FromCircleF(CircleF circle)
{
PointF center = circle.Center;
return new Circle(new Point((int)center.X, (int)center.Y), (int)circle.Radius);
}
/// <summary> /// <summary>
/// Compares this instance to another <see cref="Circle" />. /// Compares this instance to another <see cref="Circle" />.
/// </summary> /// </summary>

View File

@ -174,17 +174,6 @@ public readonly struct CircleF : IEquatable<CircleF>, IComparable<CircleF>, ICom
return left.CompareTo(right) >= 0; return left.CompareTo(right) >= 0;
} }
/// <summary>
/// Explicitly converts a <see cref="Circle" /> to a <see cref="CircleF" />.
/// </summary>
/// <param name="circle">The circle to convert.</param>
/// <returns>The converted circle.</returns>
public static explicit operator Circle(CircleF circle)
{
PointF center = circle.Center;
return new Circle(new Point((int)center.X, (int)center.Y), (int)circle.Radius);
}
/// <summary> /// <summary>
/// Implicitly converts a <see cref="Circle" /> to a <see cref="CircleF" />. /// Implicitly converts a <see cref="Circle" /> to a <see cref="CircleF" />.
/// </summary> /// </summary>
@ -192,7 +181,17 @@ public readonly struct CircleF : IEquatable<CircleF>, IComparable<CircleF>, ICom
/// <returns>The converted circle.</returns> /// <returns>The converted circle.</returns>
public static implicit operator CircleF(Circle circle) public static implicit operator CircleF(Circle circle)
{ {
return new CircleF(circle.Center, circle.Radius); return FromCircle(circle);
}
/// <summary>
/// Converts a <see cref="Circle" /> to a <see cref="CircleF" />.
/// </summary>
/// <param name="circle">The circle to convert.</param>
/// <returns>The converted circle.</returns>
public static CircleF FromCircle(Circle circle)
{
return new Circle(circle.Center, circle.Radius);
} }
/// <summary> /// <summary>

View File

@ -132,10 +132,41 @@ public readonly struct Ellipse : IEquatable<Ellipse>
/// <param name="circle">The circle to convert.</param> /// <param name="circle">The circle to convert.</param>
/// <returns>The converted ellipse.</returns> /// <returns>The converted ellipse.</returns>
public static implicit operator Ellipse(in Circle circle) public static implicit operator Ellipse(in Circle circle)
{
return FromCircle(circle);
}
/// <summary>
/// Explicitly converts an <see cref="EllipseF" /> to an <see cref="Ellipse" />.
/// </summary>
/// <param name="ellipse">The ellipse to convert.</param>
/// <returns>The converted ellipse.</returns>
public static explicit operator Ellipse(in EllipseF ellipse)
{
return FromEllipseF(ellipse);
}
/// <summary>
/// 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 Ellipse FromCircle(in Circle circle)
{ {
return new Ellipse(circle.Center, new Size(circle.Radius, circle.Radius)); return new Ellipse(circle.Center, new Size(circle.Radius, circle.Radius));
} }
/// <summary>
/// Converts an <see cref="EllipseF" /> to an <see cref="Ellipse" />.
/// </summary>
/// <param name="ellipse">The ellipse to convert.</param>
/// <returns>The converted ellipse.</returns>
public static Ellipse FromEllipseF(in EllipseF ellipse)
{
PointF center = ellipse.Center;
return new Ellipse((int)center.X, (int)center.Y, (int)ellipse.HorizontalRadius, (int)ellipse.VerticalRadius);
}
/// <inheritdoc /> /// <inheritdoc />
public override bool Equals(object? obj) public override bool Equals(object? obj)
{ {

View File

@ -161,7 +161,7 @@ public readonly struct EllipseF : IEquatable<EllipseF>
/// <returns>The converted ellipse.</returns> /// <returns>The converted ellipse.</returns>
public static implicit operator EllipseF(in Circle circle) public static implicit operator EllipseF(in Circle circle)
{ {
return new EllipseF(circle.Center, new SizeF(circle.Radius, circle.Radius)); return FromCircle(circle);
} }
/// <summary> /// <summary>
@ -171,7 +171,7 @@ public readonly struct EllipseF : IEquatable<EllipseF>
/// <returns>The converted ellipse.</returns> /// <returns>The converted ellipse.</returns>
public static implicit operator EllipseF(in CircleF circle) public static implicit operator EllipseF(in CircleF circle)
{ {
return new EllipseF(circle.Center, new SizeF(circle.Radius, circle.Radius)); return FromCircleF(circle);
} }
/// <summary> /// <summary>
@ -181,18 +181,37 @@ public readonly struct EllipseF : IEquatable<EllipseF>
/// <returns>The converted ellipse.</returns> /// <returns>The converted ellipse.</returns>
public static implicit operator EllipseF(in Ellipse ellipse) public static implicit operator EllipseF(in Ellipse ellipse)
{ {
return new EllipseF(ellipse.Center, ellipse.Radius); return FromEllipse(ellipse);
} }
/// <summary> /// <summary>
/// Explicitly converts an <see cref="EllipseF" /> to an <see cref="Ellipse" />. /// Converts a <see cref="Circle" /> to an <see cref="EllipseF" />.
/// </summary>
/// <param name="circle">The circle to convert.</param>
/// <returns>The converted ellipse.</returns>
public static EllipseF FromCircle(in Circle circle)
{
return new EllipseF(circle.Center, new SizeF(circle.Radius, circle.Radius));
}
/// <summary>
/// Converts a <see cref="CircleF" /> to an <see cref="EllipseF" />.
/// </summary>
/// <param name="circle">The circle to convert.</param>
/// <returns>The converted ellipse.</returns>
public static EllipseF FromCircleF(in CircleF circle)
{
return new EllipseF(circle.Center, new SizeF(circle.Radius, circle.Radius));
}
/// <summary>
/// Converts an <see cref="Ellipse" /> to an <see cref="EllipseF" />.
/// </summary> /// </summary>
/// <param name="ellipse">The ellipse to convert.</param> /// <param name="ellipse">The ellipse to convert.</param>
/// <returns>The converted ellipse.</returns> /// <returns>The converted ellipse.</returns>
public static explicit operator Ellipse(in EllipseF ellipse) public static EllipseF FromEllipse(in Ellipse ellipse)
{ {
PointF center = ellipse.Center; return new EllipseF(ellipse.Center, ellipse.Radius);
return new Ellipse((int)center.X, (int)center.Y, (int)ellipse.HorizontalRadius, (int)ellipse.VerticalRadius);
} }
/// <inheritdoc /> /// <inheritdoc />

View File

@ -1,4 +1,5 @@
using System.Drawing; using System.Drawing;
using System.Numerics;
namespace X10D.Drawing; namespace X10D.Drawing;
@ -152,6 +153,50 @@ public readonly struct Line : IEquatable<Line>, IComparable<Line>, IComparable
return left.CompareTo(right) >= 0; return left.CompareTo(right) >= 0;
} }
/// <summary>
/// Explicitly converts a <see cref="Line" /> to a <see cref="LineF" />.
/// </summary>
/// <param name="line">The line to convert.</param>
/// <returns>The converted line.</returns>
public static explicit operator Line(in LineF line)
{
return FromLineF(line);
}
/// <summary>
/// Explicitly converts a <see cref="Line3D" /> to a <see cref="Line" />.
/// </summary>
/// <param name="line">The line to convert.</param>
/// <returns>The converted line.</returns>
public static explicit operator Line(in Line3D line)
{
return FromLine3D(line);
}
/// <summary>
/// Converts a <see cref="Line3D" /> to a <see cref="Line" />.
/// </summary>
/// <param name="line">The line to convert.</param>
/// <returns>The converted line.</returns>
public static Line FromLine3D(in Line3D line)
{
Vector3 start = line.Start;
Vector3 end = line.End;
return new Line(new Point((int)start.X, (int)start.Y), new Point((int)end.X, (int)end.Y));
}
/// <summary>
/// Converts a <see cref="Line" /> to a <see cref="LineF" />.
/// </summary>
/// <param name="line">The line to convert.</param>
/// <returns>The converted line.</returns>
public static Line FromLineF(in LineF line)
{
PointF start = line.Start;
PointF end = line.End;
return new Line(new Point((int)start.X, (int)start.Y), new Point((int)end.X, (int)end.Y));
}
/// <summary> /// <summary>
/// Compares this instance to another object. /// Compares this instance to another object.
/// </summary> /// </summary>

View File

@ -158,30 +158,6 @@ public readonly struct Line3D : IEquatable<Line3D>, IComparable<Line3D>, ICompar
return left.CompareTo(right) >= 0; return left.CompareTo(right) >= 0;
} }
/// <summary>
/// Explicitly converts a <see cref="Line3D" /> to a <see cref="Line" />.
/// </summary>
/// <param name="line">The line to convert.</param>
/// <returns>The converted line.</returns>
public static explicit operator Line(in Line3D line)
{
Vector3 start = line.Start;
Vector3 end = line.End;
return new Line(new Point((int)start.X, (int)start.Y), new Point((int)end.X, (int)end.Y));
}
/// <summary>
/// Explicitly converts a <see cref="Line3D" /> to a <see cref="LineF" />.
/// </summary>
/// <param name="line">The line to convert.</param>
/// <returns>The converted line.</returns>
public static explicit operator LineF(in Line3D line)
{
Vector3 start = line.Start;
Vector3 end = line.End;
return new LineF(new PointF(start.X, start.Y), new PointF(end.X, end.Y));
}
/// <summary> /// <summary>
/// Implicitly converts a <see cref="Line" /> to a <see cref="LineF" />. /// Implicitly converts a <see cref="Line" /> to a <see cref="LineF" />.
/// </summary> /// </summary>
@ -189,9 +165,7 @@ public readonly struct Line3D : IEquatable<Line3D>, IComparable<Line3D>, ICompar
/// <returns>The converted line.</returns> /// <returns>The converted line.</returns>
public static implicit operator Line3D(in Line line) public static implicit operator Line3D(in Line line)
{ {
Point start = line.Start; return FromLine(line);
Point end = line.End;
return new Line3D(new Vector3(start.X, start.Y, 0), new Vector3(end.X, end.Y, 0));
} }
/// <summary> /// <summary>
@ -200,6 +174,28 @@ public readonly struct Line3D : IEquatable<Line3D>, IComparable<Line3D>, ICompar
/// <param name="line">The line to convert.</param> /// <param name="line">The line to convert.</param>
/// <returns>The converted line.</returns> /// <returns>The converted line.</returns>
public static implicit operator Line3D(in LineF line) public static implicit operator Line3D(in LineF line)
{
return FromLineF(line);
}
/// <summary>
/// Converts a <see cref="Line" /> to a <see cref="LineF" />.
/// </summary>
/// <param name="line">The line to convert.</param>
/// <returns>The converted line.</returns>
public static Line3D FromLine(in Line line)
{
Point start = line.Start;
Point end = line.End;
return new Line3D(new Vector3(start.X, start.Y, 0), new Vector3(end.X, end.Y, 0));
}
/// <summary>
/// Converts a <see cref="LineF" /> to a <see cref="Line3D" />.
/// </summary>
/// <param name="line">The line to convert.</param>
/// <returns>The converted line.</returns>
public static Line3D FromLineF(in LineF line)
{ {
PointF start = line.Start; PointF start = line.Start;
PointF end = line.End; PointF end = line.End;

View File

@ -164,28 +164,48 @@ public readonly struct LineF : IEquatable<LineF>, IComparable<LineF>, IComparabl
return left.CompareTo(right) >= 0; return left.CompareTo(right) >= 0;
} }
/// <summary>
/// Explicitly converts a <see cref="Line" /> to a <see cref="LineF" />.
/// </summary>
/// <param name="line">The line to convert.</param>
/// <returns>The converted line.</returns>
public static explicit operator Line(in LineF line)
{
PointF start = line.Start;
PointF end = line.End;
return new Line(new Point((int)start.X, (int)start.Y), new Point((int)end.X, (int)end.Y));
}
/// <summary> /// <summary>
/// Implicitly converts a <see cref="Line" /> to a <see cref="LineF" />. /// Implicitly converts a <see cref="Line" /> to a <see cref="LineF" />.
/// </summary> /// </summary>
/// <param name="line">The line to convert.</param> /// <param name="line">The line to convert.</param>
/// <returns>The converted line.</returns> /// <returns>The converted line.</returns>
public static implicit operator LineF(in Line line) public static implicit operator LineF(in Line line)
{
return FromLine(line);
}
/// <summary>
/// Explicitly converts a <see cref="Line3D" /> to a <see cref="LineF" />.
/// </summary>
/// <param name="line">The line to convert.</param>
/// <returns>The converted line.</returns>
public static explicit operator LineF(in Line3D line)
{
return FromLine3D(line);
}
/// <summary>
/// Converts a <see cref="Line" /> to a <see cref="LineF" />.
/// </summary>
/// <param name="line">The line to convert.</param>
/// <returns>The converted line.</returns>
public static LineF FromLine(in Line line)
{ {
return new LineF(line.Start, line.End); return new LineF(line.Start, line.End);
} }
/// <summary>
/// Converts a <see cref="Line3D" /> to a <see cref="LineF" />.
/// </summary>
/// <param name="line">The line to convert.</param>
/// <returns>The converted line.</returns>
public static LineF FromLine3D(in Line3D line)
{
Vector3 start = line.Start;
Vector3 end = line.End;
return new LineF(new PointF(start.X, start.Y), new PointF(end.X, end.Y));
}
/// <summary> /// <summary>
/// Compares this instance to another object. /// Compares this instance to another object.
/// </summary> /// </summary>

View File

@ -1,4 +1,4 @@
using System.Drawing; using System.Drawing;
namespace X10D.Drawing; namespace X10D.Drawing;
@ -133,6 +133,43 @@ public class Polygon : IEquatable<Polygon>
return !left.Equals(right); return !left.Equals(right);
} }
/// <summary>
/// Explicitly converts a <see cref="Polygon" /> to a <see cref="PolygonF" />.
/// </summary>
/// <param name="polygon">The polygon to convert.</param>
/// <returns>The converted polygon.</returns>
public static explicit operator Polygon?(PolygonF? polygon)
{
return polygon is null ? null : FromPolygonF(polygon);
}
/// <summary>
/// Explicitly converts a <see cref="Polygon" /> to a <see cref="PolygonF" />.
/// </summary>
/// <param name="polygon">The polygon to convert.</param>
/// <returns>The converted polygon.</returns>
/// <exception cref="ArgumentNullException"><paramref name="polygon" /> is <see langword="null" />.</exception>
public static Polygon FromPolygonF(PolygonF polygon)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(polygon);
#else
if (polygon is null)
{
throw new ArgumentNullException(nameof(polygon));
}
#endif
var vertices = new List<Point>();
foreach (PointF vertex in polygon.Vertices)
{
vertices.Add(new Point((int)vertex.X, (int)vertex.Y));
}
return new Polygon(vertices);
}
/// <summary> /// <summary>
/// Adds a vertex to this polygon. /// Adds a vertex to this polygon.
/// </summary> /// </summary>

View File

@ -163,20 +163,13 @@ public class PolygonF
} }
/// <summary> /// <summary>
/// Explicitly converts a <see cref="Polygon" /> to a <see cref="PolygonF" />. /// Implicitly converts a <see cref="Polygon" /> to a <see cref="PolygonF" />.
/// </summary> /// </summary>
/// <param name="polygon">The polygon to convert.</param> /// <param name="polygon">The polygon to convert.</param>
/// <returns>The converted polygon.</returns> /// <returns>The converted polygon.</returns>
public static explicit operator Polygon(PolygonF polygon) public static implicit operator PolygonF?(Polygon? polygon)
{ {
var vertices = new List<Point>(); return polygon is null ? null : FromPolygon(polygon);
foreach (PointF vertex in polygon.Vertices)
{
vertices.Add(new Point((int)vertex.X, (int)vertex.Y));
}
return new Polygon(vertices);
} }
/// <summary> /// <summary>
@ -184,8 +177,18 @@ public class PolygonF
/// </summary> /// </summary>
/// <param name="polygon">The polygon to convert.</param> /// <param name="polygon">The polygon to convert.</param>
/// <returns>The converted polygon.</returns> /// <returns>The converted polygon.</returns>
public static implicit operator PolygonF(Polygon polygon) /// <exception cref="ArgumentNullException"><paramref name="polygon" /> is <see langword="null" />.</exception>
public static PolygonF FromPolygon(Polygon polygon)
{ {
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(polygon);
#else
if (polygon is null)
{
throw new ArgumentNullException(nameof(polygon));
}
#endif
var vertices = new List<PointF>(); var vertices = new List<PointF>();
foreach (Point vertex in polygon.Vertices) foreach (Point vertex in polygon.Vertices)

View File

@ -1,4 +1,4 @@
using System.Drawing; using System.Drawing;
using System.Numerics; using System.Numerics;
namespace X10D.Drawing; namespace X10D.Drawing;
@ -135,10 +135,44 @@ public class Polyhedron : IEquatable<Polyhedron>
/// Implicitly converts a <see cref="Polygon" /> to a <see cref="Polyhedron" />. /// Implicitly converts a <see cref="Polygon" /> to a <see cref="Polyhedron" />.
/// </summary> /// </summary>
/// <param name="polygon">The polyhedron to convert.</param> /// <param name="polygon">The polyhedron to convert.</param>
/// <returns>The converted polyhedron.</returns> /// <returns>
public static implicit operator Polyhedron(Polygon polygon) /// The converted polyhedron, or <see langword="null" /> if <paramref name="polygon" /> is <see langword="null" />.
/// </returns>
public static implicit operator Polyhedron?(Polygon? polygon)
{ {
List<Vector3> vertices = new List<Vector3>(); return polygon is null ? null : FromPolygon(polygon);
}
/// <summary>
/// Implicitly converts a <see cref="PolygonF" /> to a <see cref="Polyhedron" />.
/// </summary>
/// <param name="polygon">The polyhedron to convert.</param>
/// <returns>
/// The converted polyhedron, or <see langword="null" /> if <paramref name="polygon" /> is <see langword="null" />.
/// </returns>
public static implicit operator Polyhedron?(PolygonF? polygon)
{
return polygon is null ? null : FromPolygonF(polygon);
}
/// <summary>
/// Converts a <see cref="Polygon" /> to a <see cref="Polyhedron" />.
/// </summary>
/// <param name="polygon">The polyhedron to convert.</param>
/// <returns>The converted polyhedron.</returns>
/// <exception cref="ArgumentNullException"><paramref name="polygon" /> is <see langword="null" />.</exception>
public static Polyhedron FromPolygon(Polygon polygon)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(polygon);
#else
if (polygon is null)
{
throw new ArgumentNullException(nameof(polygon));
}
#endif
var vertices = new List<Vector3>();
foreach (Point vertex in polygon.Vertices) foreach (Point vertex in polygon.Vertices)
{ {
@ -149,13 +183,23 @@ public class Polyhedron : IEquatable<Polyhedron>
} }
/// <summary> /// <summary>
/// Implicitly converts a <see cref="PolygonF" /> to a <see cref="Polyhedron" />. /// Converts a <see cref="PolygonF" /> to a <see cref="Polyhedron" />.
/// </summary> /// </summary>
/// <param name="polygon">The polyhedron to convert.</param> /// <param name="polygon">The polyhedron to convert.</param>
/// <returns>The converted polyhedron.</returns> /// <returns>The converted polyhedron.</returns>
public static implicit operator Polyhedron(PolygonF polygon) /// <exception cref="ArgumentNullException"><paramref name="polygon" /> is <see langword="null" />.</exception>
public static Polyhedron FromPolygonF(PolygonF polygon)
{ {
List<Vector3> vertices = new List<Vector3>(); #if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(polygon);
#else
if (polygon is null)
{
throw new ArgumentNullException(nameof(polygon));
}
#endif
var vertices = new List<Vector3>();
foreach (PointF vertex in polygon.Vertices) foreach (PointF vertex in polygon.Vertices)
{ {