From 3abd7b1379a4dadb04416e795749cb2ea361cd43 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Wed, 30 Nov 2022 17:51:20 +0000 Subject: [PATCH] Resolve CA2225, offer methods to complement operators --- VpSharp/src/Cell.cs | 92 ++++++++++++++++++++++++++++++++++++----- VpSharp/src/ColorF.cs | 26 ++++++++++-- VpSharp/src/Vector3d.cs | 49 ++++++++++++++++------ 3 files changed, 141 insertions(+), 26 deletions(-) diff --git a/VpSharp/src/Cell.cs b/VpSharp/src/Cell.cs index b0c65c4..5b54aee 100644 --- a/VpSharp/src/Cell.cs +++ b/VpSharp/src/Cell.cs @@ -49,54 +49,99 @@ public readonly struct Cell : IEquatable, IFormattable public static bool operator !=(Cell left, Cell right) => !left.Equals(right); /// - /// Explicitly converts an instance of to an instance of . + /// Explicitly converts an instance of to an instance of . /// /// The vector to convert. /// /// A cell whose component is equal to , and whose component /// is equal to . /// - public static explicit operator Cell(Vector2 vector) => new((int)vector.X, (int)vector.Y); + public static explicit operator Cell(Vector2 vector) + { + return FromVector2(vector); + } /// - /// Implicitly converts an instance of to an instance of . + /// Implicitly converts an instance of to an instance of . /// /// The cell to convert. /// /// A vector whose component is equal to , and whose /// component is equal to . /// - public static implicit operator Vector2(Cell cell) => new(cell.X, cell.Z); + public static implicit operator Vector2(Cell cell) + { + return cell.ToVector2(); + } /// - /// Explicitly converts an instance of to an instance of . + /// Explicitly converts an instance of to an instance of . /// /// The vector to convert. /// /// A cell whose component is equal to , and whose component /// is equal to . /// - public static explicit operator Cell(Vector3 vector) => new((int)vector.X, (int)vector.Z); + public static explicit operator Cell(Vector3 vector) + { + return FromVector3(vector); + } /// - /// Implicitly converts an instance of to an instance of . + /// Implicitly converts an instance of to an instance of . /// /// The cell to convert. /// /// A vector whose component is equal to , and whose /// component is equal to , and whose component is 0. /// - public static implicit operator Vector3(Cell cell) => new(cell.X, 0, cell.Z); + public static implicit operator Vector3(Cell cell) + { + return cell.ToVector3(); + } /// - /// Explicitly converts an instance of to an instance of . + /// Explicitly converts an instance of to an instance of . /// /// The vector to convert. /// /// A cell whose component is equal to , and whose /// component is equal to . /// - public static explicit operator Cell(Vector3d vector) => new((int)vector.X, (int)vector.Z); + public static explicit operator Cell(Vector3d vector) + { + return FromVector3d(vector); + } + + /// + /// Converts an instance of to a new instance of . + /// + /// The vector to convert. + /// The cell result of the conversion. + public static Cell FromVector2(in Vector2 vector) + { + return new Cell((int)vector.X, (int)vector.Y); + } + + /// + /// Converts an instance of to a new instance of . + /// + /// The vector to convert. + /// The cell result of the conversion. + public static Cell FromVector3(in Vector3 vector) + { + return new Cell((int)vector.X, (int)vector.Y); + } + + /// + /// Converts an instance of to a new instance of . + /// + /// The vector to convert. + /// The cell result of the conversion. + public static Cell FromVector3d(in Vector3d vector) + { + return new Cell((int)vector.X, (int)vector.Y); + } /// /// Returns a value indicating whether this cell and another cell are equal. @@ -150,4 +195,31 @@ public readonly struct Cell : IEquatable, IFormattable builder.Append('>'); return builder.ToString(); } + + /// + /// Converts this cell to a . + /// + /// The vector result of the conversion. + public Vector2 ToVector2() + { + return new Vector2(X, Z); + } + + /// + /// Converts this cell to a . + /// + /// The vector result of the conversion. + public Vector3 ToVector3() + { + return new Vector3(X, 0, Z); + } + + /// + /// Converts this cell to a . + /// + /// The vector result of the conversion. + public Vector3d ToVector3d() + { + return new Vector3d(X, 0, Z); + } } diff --git a/VpSharp/src/ColorF.cs b/VpSharp/src/ColorF.cs index f67ca3c..57d30fc 100644 --- a/VpSharp/src/ColorF.cs +++ b/VpSharp/src/ColorF.cs @@ -1,4 +1,4 @@ -using System.Drawing; +using System.Drawing; using VpSharp.Internal; namespace VpSharp; @@ -71,7 +71,7 @@ public readonly struct ColorF : IEquatable /// The converted color. public static implicit operator ColorF(Color color) { - return FromArgb(color.A / 255.0f, color.R / 255.0f, color.G / 255.0f, color.B / 255.0f); + return FromColor(color); } /// @@ -81,8 +81,7 @@ public readonly struct ColorF : IEquatable /// The converted color. public static explicit operator Color(ColorF color) { - return Color.FromArgb((int) (color.A * 255.0f), (int) (color.R * 255.0f), (int) (color.G * 255.0f), - (int) (color.B * 255.0f)); + return color.ToColor(); } /// @@ -139,6 +138,16 @@ public readonly struct ColorF : IEquatable return new ColorF(a, r, g, b); } + /// + /// Converts an instance of to an instance of . + /// + /// The color to convert. + /// The converted color. + public static ColorF FromColor(Color color) + { + return FromArgb(color.A / 255.0f, color.R / 255.0f, color.G / 255.0f, color.B / 255.0f); + } + /// /// Returns a value indicating whether this color and another color are equal. /// @@ -160,4 +169,13 @@ public readonly struct ColorF : IEquatable { return HashCode.Combine(A, R, G, B); } + + /// + /// Converts this instance of to an instance of . + /// + /// The converted color. + public Color ToColor() + { + return Color.FromArgb((int)(A * 255.0f), (int)(R * 255.0f), (int)(G * 255.0f), (int)(B * 255.0f)); + } } diff --git a/VpSharp/src/Vector3d.cs b/VpSharp/src/Vector3d.cs index 896011d..fbf9242 100644 --- a/VpSharp/src/Vector3d.cs +++ b/VpSharp/src/Vector3d.cs @@ -1,4 +1,4 @@ -using System.Globalization; +using System.Globalization; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -238,22 +238,26 @@ public struct Vector3d : IEquatable, IFormattable public static bool operator !=(Vector3d left, Vector3d right) => !left.Equals(right); /// - /// Implicitly converts a to a new instance of , by implicitly - /// converting the , and fields to - /// . + /// Implicitly converts a to a new instance of , by implicitly converting + /// the , and fields to . /// /// The vector to convert. /// The converted vector. - public static implicit operator Vector3d(Vector3 vector) => new(vector.X, vector.Y, vector.Z); + public static implicit operator Vector3d(in Vector3 vector) + { + return FromVector3(vector); + } /// - /// Explicit converts a to a new instance of , by explicitly - /// converting the , and fields to - /// . + /// Explicit converts a to a new instance of , by explicitly converting the + /// , and fields to . /// /// The vector to convert. /// The converted vector. - public static explicit operator Vector3(Vector3d vector) => new((float) vector.X, (float) vector.Y, (float) vector.Z); + public static explicit operator Vector3(in Vector3d vector) + { + return vector.ToVector3(); + } /// /// Returns a vector whose elements are the absolute values of each of the source vector's elements. @@ -329,9 +333,20 @@ public struct Vector3d : IEquatable, IFormattable /// The dot product. public static double Dot(Vector3d left, Vector3d right) { - return left.X * right.X + - left.Y * right.Y + - left.Z * right.Z; + return (left.X * right.X) + + (left.Y * right.Y) + + (left.Z * right.Z); + } + + /// + /// Converts a to a new instance of , by implicitly converting the + /// , and fields to . + /// + /// The vector to convert. + /// The converted vector. + public static Vector3d FromVector3(in Vector3 vector) + { + return new Vector3d(vector.X, vector.Y, vector.Z); } /// @@ -517,4 +532,14 @@ public struct Vector3d : IEquatable, IFormattable builder.Append('>'); return builder.ToString(); } + + /// + /// Converts this instance to a new instance of , by explicitly converting the , + /// and fields to . + /// + /// The converted vector. + public readonly Vector3 ToVector3() + { + return new Vector3((float)X, (float)Y, (float)Z); + } }