From 398b0c58b1764b75f9d4e70842eac25b94224db7 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Tue, 29 Nov 2022 17:10:10 +0000 Subject: [PATCH] Fix CA2225 violations --- X10D/src/Drawing/Circle.cs | 21 ++++++++++++ X10D/src/Drawing/CircleF.cs | 23 +++++++------- X10D/src/Drawing/Ellipse.cs | 31 ++++++++++++++++++ X10D/src/Drawing/EllipseF.cs | 33 +++++++++++++++---- X10D/src/Drawing/Line.cs | 45 ++++++++++++++++++++++++++ X10D/src/Drawing/Line3D.cs | 50 ++++++++++++++--------------- X10D/src/Drawing/LineF.cs | 44 +++++++++++++++++++------- X10D/src/Drawing/Polygon.cs | 39 ++++++++++++++++++++++- X10D/src/Drawing/PolygonF.cs | 25 ++++++++------- X10D/src/Drawing/Polyhedron.cs | 58 ++++++++++++++++++++++++++++++---- 10 files changed, 292 insertions(+), 77 deletions(-) diff --git a/X10D/src/Drawing/Circle.cs b/X10D/src/Drawing/Circle.cs index ba7aa9c..1ba36c6 100644 --- a/X10D/src/Drawing/Circle.cs +++ b/X10D/src/Drawing/Circle.cs @@ -162,6 +162,27 @@ public readonly struct Circle : IEquatable, IComparable, ICompar return left.CompareTo(right) >= 0; } + /// + /// Explicitly converts a to a . + /// + /// The circle to convert. + /// The converted circle. + public static explicit operator Circle(CircleF circle) + { + return Circle.FromCircleF(circle); + } + + /// + /// Converts a to a . + /// + /// The circle to convert. + /// The converted circle. + public static Circle FromCircleF(CircleF circle) + { + PointF center = circle.Center; + return new Circle(new Point((int)center.X, (int)center.Y), (int)circle.Radius); + } + /// /// Compares this instance to another . /// diff --git a/X10D/src/Drawing/CircleF.cs b/X10D/src/Drawing/CircleF.cs index 5281320..da0759c 100644 --- a/X10D/src/Drawing/CircleF.cs +++ b/X10D/src/Drawing/CircleF.cs @@ -174,17 +174,6 @@ public readonly struct CircleF : IEquatable, IComparable, ICom return left.CompareTo(right) >= 0; } - /// - /// Explicitly converts a to a . - /// - /// The circle to convert. - /// The converted circle. - 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); - } - /// /// Implicitly converts a to a . /// @@ -192,7 +181,17 @@ public readonly struct CircleF : IEquatable, IComparable, ICom /// The converted circle. public static implicit operator CircleF(Circle circle) { - return new CircleF(circle.Center, circle.Radius); + return FromCircle(circle); + } + + /// + /// Converts a to a . + /// + /// The circle to convert. + /// The converted circle. + public static CircleF FromCircle(Circle circle) + { + return new Circle(circle.Center, circle.Radius); } /// diff --git a/X10D/src/Drawing/Ellipse.cs b/X10D/src/Drawing/Ellipse.cs index e53cc62..736b3aa 100644 --- a/X10D/src/Drawing/Ellipse.cs +++ b/X10D/src/Drawing/Ellipse.cs @@ -132,10 +132,41 @@ public readonly struct Ellipse : IEquatable /// The circle to convert. /// The converted ellipse. public static implicit operator Ellipse(in Circle circle) + { + return FromCircle(circle); + } + + /// + /// Explicitly converts an to an . + /// + /// The ellipse to convert. + /// The converted ellipse. + public static explicit operator Ellipse(in EllipseF ellipse) + { + return FromEllipseF(ellipse); + } + + /// + /// Converts a to an . + /// + /// The circle to convert. + /// The converted ellipse. + public static Ellipse FromCircle(in Circle circle) { return new Ellipse(circle.Center, new Size(circle.Radius, circle.Radius)); } + /// + /// Converts an to an . + /// + /// The ellipse to convert. + /// The converted ellipse. + 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); + } + /// public override bool Equals(object? obj) { diff --git a/X10D/src/Drawing/EllipseF.cs b/X10D/src/Drawing/EllipseF.cs index 0f21acf..310f17f 100644 --- a/X10D/src/Drawing/EllipseF.cs +++ b/X10D/src/Drawing/EllipseF.cs @@ -161,7 +161,7 @@ public readonly struct EllipseF : IEquatable /// The converted ellipse. public static implicit operator EllipseF(in Circle circle) { - return new EllipseF(circle.Center, new SizeF(circle.Radius, circle.Radius)); + return FromCircle(circle); } /// @@ -171,7 +171,7 @@ public readonly struct EllipseF : IEquatable /// The converted ellipse. public static implicit operator EllipseF(in CircleF circle) { - return new EllipseF(circle.Center, new SizeF(circle.Radius, circle.Radius)); + return FromCircleF(circle); } /// @@ -181,18 +181,37 @@ public readonly struct EllipseF : IEquatable /// The converted ellipse. public static implicit operator EllipseF(in Ellipse ellipse) { - return new EllipseF(ellipse.Center, ellipse.Radius); + return FromEllipse(ellipse); } /// - /// Explicitly converts an to an . + /// Converts a to an . + /// + /// The circle to convert. + /// The converted ellipse. + public static EllipseF FromCircle(in Circle circle) + { + return new EllipseF(circle.Center, new SizeF(circle.Radius, circle.Radius)); + } + + /// + /// Converts a to an . + /// + /// The circle to convert. + /// The converted ellipse. + public static EllipseF FromCircleF(in CircleF circle) + { + return new EllipseF(circle.Center, new SizeF(circle.Radius, circle.Radius)); + } + + /// + /// Converts an to an . /// /// The ellipse to convert. /// The converted ellipse. - public static explicit operator Ellipse(in EllipseF ellipse) + public static EllipseF FromEllipse(in Ellipse ellipse) { - PointF center = ellipse.Center; - return new Ellipse((int)center.X, (int)center.Y, (int)ellipse.HorizontalRadius, (int)ellipse.VerticalRadius); + return new EllipseF(ellipse.Center, ellipse.Radius); } /// diff --git a/X10D/src/Drawing/Line.cs b/X10D/src/Drawing/Line.cs index 593c670..c174b9f 100644 --- a/X10D/src/Drawing/Line.cs +++ b/X10D/src/Drawing/Line.cs @@ -1,4 +1,5 @@ using System.Drawing; +using System.Numerics; namespace X10D.Drawing; @@ -152,6 +153,50 @@ public readonly struct Line : IEquatable, IComparable, IComparable return left.CompareTo(right) >= 0; } + /// + /// Explicitly converts a to a . + /// + /// The line to convert. + /// The converted line. + public static explicit operator Line(in LineF line) + { + return FromLineF(line); + } + + /// + /// Explicitly converts a to a . + /// + /// The line to convert. + /// The converted line. + public static explicit operator Line(in Line3D line) + { + return FromLine3D(line); + } + + /// + /// Converts a to a . + /// + /// The line to convert. + /// The converted line. + 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)); + } + + /// + /// Converts a to a . + /// + /// The line to convert. + /// The converted line. + 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)); + } + /// /// Compares this instance to another object. /// diff --git a/X10D/src/Drawing/Line3D.cs b/X10D/src/Drawing/Line3D.cs index 2f1cf04..f2b3bc5 100644 --- a/X10D/src/Drawing/Line3D.cs +++ b/X10D/src/Drawing/Line3D.cs @@ -158,30 +158,6 @@ public readonly struct Line3D : IEquatable, IComparable, ICompar return left.CompareTo(right) >= 0; } - /// - /// Explicitly converts a to a . - /// - /// The line to convert. - /// The converted line. - 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)); - } - - /// - /// Explicitly converts a to a . - /// - /// The line to convert. - /// The converted line. - 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)); - } - /// /// Implicitly converts a to a . /// @@ -189,9 +165,7 @@ public readonly struct Line3D : IEquatable, IComparable, ICompar /// The converted line. public static implicit operator Line3D(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)); + return FromLine(line); } /// @@ -200,6 +174,28 @@ public readonly struct Line3D : IEquatable, IComparable, ICompar /// The line to convert. /// The converted line. public static implicit operator Line3D(in LineF line) + { + return FromLineF(line); + } + + /// + /// Converts a to a . + /// + /// The line to convert. + /// The converted line. + 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)); + } + + /// + /// Converts a to a . + /// + /// The line to convert. + /// The converted line. + public static Line3D FromLineF(in LineF line) { PointF start = line.Start; PointF end = line.End; diff --git a/X10D/src/Drawing/LineF.cs b/X10D/src/Drawing/LineF.cs index 9c0a62d..8e1b172 100644 --- a/X10D/src/Drawing/LineF.cs +++ b/X10D/src/Drawing/LineF.cs @@ -164,28 +164,48 @@ public readonly struct LineF : IEquatable, IComparable, IComparabl return left.CompareTo(right) >= 0; } - /// - /// Explicitly converts a to a . - /// - /// The line to convert. - /// The converted line. - 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)); - } - /// /// Implicitly converts a to a . /// /// The line to convert. /// The converted line. public static implicit operator LineF(in Line line) + { + return FromLine(line); + } + + /// + /// Explicitly converts a to a . + /// + /// The line to convert. + /// The converted line. + public static explicit operator LineF(in Line3D line) + { + return FromLine3D(line); + } + + /// + /// Converts a to a . + /// + /// The line to convert. + /// The converted line. + public static LineF FromLine(in Line line) { return new LineF(line.Start, line.End); } + /// + /// Converts a to a . + /// + /// The line to convert. + /// The converted line. + 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)); + } + /// /// Compares this instance to another object. /// diff --git a/X10D/src/Drawing/Polygon.cs b/X10D/src/Drawing/Polygon.cs index d9b4450..7de943b 100644 --- a/X10D/src/Drawing/Polygon.cs +++ b/X10D/src/Drawing/Polygon.cs @@ -1,4 +1,4 @@ -using System.Drawing; +using System.Drawing; namespace X10D.Drawing; @@ -133,6 +133,43 @@ public class Polygon : IEquatable return !left.Equals(right); } + /// + /// Explicitly converts a to a . + /// + /// The polygon to convert. + /// The converted polygon. + public static explicit operator Polygon?(PolygonF? polygon) + { + return polygon is null ? null : FromPolygonF(polygon); + } + + /// + /// Explicitly converts a to a . + /// + /// The polygon to convert. + /// The converted polygon. + /// is . + 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(); + + foreach (PointF vertex in polygon.Vertices) + { + vertices.Add(new Point((int)vertex.X, (int)vertex.Y)); + } + + return new Polygon(vertices); + } + /// /// Adds a vertex to this polygon. /// diff --git a/X10D/src/Drawing/PolygonF.cs b/X10D/src/Drawing/PolygonF.cs index 5032bb8..15243e8 100644 --- a/X10D/src/Drawing/PolygonF.cs +++ b/X10D/src/Drawing/PolygonF.cs @@ -163,20 +163,13 @@ public class PolygonF } /// - /// Explicitly converts a to a . + /// Implicitly converts a to a . /// /// The polygon to convert. /// The converted polygon. - public static explicit operator Polygon(PolygonF polygon) + public static implicit operator PolygonF?(Polygon? polygon) { - var vertices = new List(); - - foreach (PointF vertex in polygon.Vertices) - { - vertices.Add(new Point((int)vertex.X, (int)vertex.Y)); - } - - return new Polygon(vertices); + return polygon is null ? null : FromPolygon(polygon); } /// @@ -184,8 +177,18 @@ public class PolygonF /// /// The polygon to convert. /// The converted polygon. - public static implicit operator PolygonF(Polygon polygon) + /// is . + 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(); foreach (Point vertex in polygon.Vertices) diff --git a/X10D/src/Drawing/Polyhedron.cs b/X10D/src/Drawing/Polyhedron.cs index a839a06..4a28394 100644 --- a/X10D/src/Drawing/Polyhedron.cs +++ b/X10D/src/Drawing/Polyhedron.cs @@ -1,4 +1,4 @@ -using System.Drawing; +using System.Drawing; using System.Numerics; namespace X10D.Drawing; @@ -135,10 +135,44 @@ public class Polyhedron : IEquatable /// Implicitly converts a to a . /// /// The polyhedron to convert. - /// The converted polyhedron. - public static implicit operator Polyhedron(Polygon polygon) + /// + /// The converted polyhedron, or if is . + /// + public static implicit operator Polyhedron?(Polygon? polygon) { - List vertices = new List(); + return polygon is null ? null : FromPolygon(polygon); + } + + /// + /// Implicitly converts a to a . + /// + /// The polyhedron to convert. + /// + /// The converted polyhedron, or if is . + /// + public static implicit operator Polyhedron?(PolygonF? polygon) + { + return polygon is null ? null : FromPolygonF(polygon); + } + + /// + /// Converts a to a . + /// + /// The polyhedron to convert. + /// The converted polyhedron. + /// is . + 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(); foreach (Point vertex in polygon.Vertices) { @@ -149,13 +183,23 @@ public class Polyhedron : IEquatable } /// - /// Implicitly converts a to a . + /// Converts a to a . /// /// The polyhedron to convert. /// The converted polyhedron. - public static implicit operator Polyhedron(PolygonF polygon) + /// is . + public static Polyhedron FromPolygonF(PolygonF polygon) { - List vertices = new List(); +#if NET6_0_OR_GREATER + ArgumentNullException.ThrowIfNull(polygon); +#else + if (polygon is null) + { + throw new ArgumentNullException(nameof(polygon)); + } +#endif + + var vertices = new List(); foreach (PointF vertex in polygon.Vertices) {