Resolve CA2225, offer methods to complement operators

This commit is contained in:
Oliver Booth 2022-11-30 17:51:20 +00:00
parent 9336511059
commit 3abd7b1379
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
3 changed files with 141 additions and 26 deletions

View File

@ -49,54 +49,99 @@ public readonly struct Cell : IEquatable<Cell>, IFormattable
public static bool operator !=(Cell left, Cell right) => !left.Equals(right);
/// <summary>
/// Explicitly converts an instance of <see cref="Vector2" /> to an instance of <see cref="Cell" />.
/// Explicitly converts an instance of <see cref="Vector2" /> to an instance of <see cref="Cell" />.
/// </summary>
/// <param name="vector">The vector to convert.</param>
/// <returns>
/// A cell whose <see cref="X" /> component is equal to <see cref="Vector2.X" />, and whose <see cref="Z" /> component
/// is equal to <see cref="Vector2.Y" />.
/// </returns>
public static explicit operator Cell(Vector2 vector) => new((int)vector.X, (int)vector.Y);
public static explicit operator Cell(Vector2 vector)
{
return FromVector2(vector);
}
/// <summary>
/// Implicitly converts an instance of <see cref="Cell" /> to an instance of <see cref="Vector2" />.
/// Implicitly converts an instance of <see cref="Cell" /> to an instance of <see cref="Vector2" />.
/// </summary>
/// <param name="cell">The cell to convert.</param>
/// <returns>
/// A vector whose <see cref="X" /> component is equal to <see cref="Cell.X" />, and whose <see cref="Vector2.Y" />
/// component is equal to <see cref="Z" />.
/// </returns>
public static implicit operator Vector2(Cell cell) => new(cell.X, cell.Z);
public static implicit operator Vector2(Cell cell)
{
return cell.ToVector2();
}
/// <summary>
/// Explicitly converts an instance of <see cref="Vector3" /> to an instance of <see cref="Cell" />.
/// Explicitly converts an instance of <see cref="Vector3" /> to an instance of <see cref="Cell" />.
/// </summary>
/// <param name="vector">The vector to convert.</param>
/// <returns>
/// A cell whose <see cref="X" /> component is equal to <see cref="Vector3.X" />, and whose <see cref="Z" /> component
/// is equal to <see cref="Vector3.Z" />.
/// </returns>
public static explicit operator Cell(Vector3 vector) => new((int)vector.X, (int)vector.Z);
public static explicit operator Cell(Vector3 vector)
{
return FromVector3(vector);
}
/// <summary>
/// Implicitly converts an instance of <see cref="Cell" /> to an instance of <see cref="Vector3" />.
/// Implicitly converts an instance of <see cref="Cell" /> to an instance of <see cref="Vector3" />.
/// </summary>
/// <param name="cell">The cell to convert.</param>
/// <returns>
/// A vector whose <see cref="X" /> component is equal to <see cref="Cell.X" />, and whose <see cref="Vector3.Z" />
/// component is equal to <see cref="Z" />, and whose <see cref="Vector3.Y" /> component is 0.
/// </returns>
public static implicit operator Vector3(Cell cell) => new(cell.X, 0, cell.Z);
public static implicit operator Vector3(Cell cell)
{
return cell.ToVector3();
}
/// <summary>
/// Explicitly converts an instance of <see cref="Vector3d" /> to an instance of <see cref="Cell" />.
/// Explicitly converts an instance of <see cref="Vector3d" /> to an instance of <see cref="Cell" />.
/// </summary>
/// <param name="vector">The vector to convert.</param>
/// <returns>
/// A cell whose <see cref="X" /> component is equal to <see cref="Vector3d.X" />, and whose <see cref="Z" />
/// component is equal to <see cref="Vector3d.Z" />.
/// </returns>
public static explicit operator Cell(Vector3d vector) => new((int)vector.X, (int)vector.Z);
public static explicit operator Cell(Vector3d vector)
{
return FromVector3d(vector);
}
/// <summary>
/// Converts an instance of <see cref="Vector2" /> to a new instance of <see cref="Cell" />.
/// </summary>
/// <param name="vector">The vector to convert.</param>
/// <returns>The cell result of the conversion.</returns>
public static Cell FromVector2(in Vector2 vector)
{
return new Cell((int)vector.X, (int)vector.Y);
}
/// <summary>
/// Converts an instance of <see cref="Vector3" /> to a new instance of <see cref="Cell" />.
/// </summary>
/// <param name="vector">The vector to convert.</param>
/// <returns>The cell result of the conversion.</returns>
public static Cell FromVector3(in Vector3 vector)
{
return new Cell((int)vector.X, (int)vector.Y);
}
/// <summary>
/// Converts an instance of <see cref="Vector3d" /> to a new instance of <see cref="Cell" />.
/// </summary>
/// <param name="vector">The vector to convert.</param>
/// <returns>The cell result of the conversion.</returns>
public static Cell FromVector3d(in Vector3d vector)
{
return new Cell((int)vector.X, (int)vector.Y);
}
/// <summary>
/// Returns a value indicating whether this cell and another cell are equal.
@ -150,4 +195,31 @@ public readonly struct Cell : IEquatable<Cell>, IFormattable
builder.Append('>');
return builder.ToString();
}
/// <summary>
/// Converts this cell to a <see cref="Vector2" />.
/// </summary>
/// <returns>The vector result of the conversion.</returns>
public Vector2 ToVector2()
{
return new Vector2(X, Z);
}
/// <summary>
/// Converts this cell to a <see cref="Vector2" />.
/// </summary>
/// <returns>The vector result of the conversion.</returns>
public Vector3 ToVector3()
{
return new Vector3(X, 0, Z);
}
/// <summary>
/// Converts this cell to a <see cref="Vector2" />.
/// </summary>
/// <returns>The vector result of the conversion.</returns>
public Vector3d ToVector3d()
{
return new Vector3d(X, 0, Z);
}
}

View File

@ -1,4 +1,4 @@
using System.Drawing;
using System.Drawing;
using VpSharp.Internal;
namespace VpSharp;
@ -71,7 +71,7 @@ public readonly struct ColorF : IEquatable<ColorF>
/// <returns>The converted color.</returns>
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);
}
/// <summary>
@ -81,8 +81,7 @@ public readonly struct ColorF : IEquatable<ColorF>
/// <returns>The converted color.</returns>
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();
}
/// <summary>
@ -139,6 +138,16 @@ public readonly struct ColorF : IEquatable<ColorF>
return new ColorF(a, r, g, b);
}
/// <summary>
/// Converts an instance of <see cref="Color" /> to an instance of <see cref="ColorF" />.
/// </summary>
/// <param name="color">The color to convert.</param>
/// <returns>The converted color.</returns>
public static ColorF FromColor(Color color)
{
return FromArgb(color.A / 255.0f, color.R / 255.0f, color.G / 255.0f, color.B / 255.0f);
}
/// <summary>
/// Returns a value indicating whether this color and another color are equal.
/// </summary>
@ -160,4 +169,13 @@ public readonly struct ColorF : IEquatable<ColorF>
{
return HashCode.Combine(A, R, G, B);
}
/// <summary>
/// Converts this instance of <see cref="ColorF" /> to an instance of <see cref="Color" />.
/// </summary>
/// <returns>The converted color.</returns>
public Color ToColor()
{
return Color.FromArgb((int)(A * 255.0f), (int)(R * 255.0f), (int)(G * 255.0f), (int)(B * 255.0f));
}
}

View File

@ -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<Vector3d>, IFormattable
public static bool operator !=(Vector3d left, Vector3d right) => !left.Equals(right);
/// <summary>
/// Implicitly converts a <see cref="Vector3" /> to a new instance of <see cref="Vector3d" />, by implicitly
/// converting the <see cref="Vector3.X" />, <see cref="Vector3.Y" /> and <see cref="Vector3.Z" /> fields to
/// <see cref="double" />.
/// Implicitly converts a <see cref="Vector3" /> to a new instance of <see cref="Vector3d" />, by implicitly converting
/// the <see cref="Vector3.X" />, <see cref="Vector3.Y" /> and <see cref="Vector3.Z" /> fields to <see cref="double" />.
/// </summary>
/// <param name="vector">The vector to convert.</param>
/// <returns>The converted vector.</returns>
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);
}
/// <summary>
/// Explicit converts a <see cref="Vector3" /> to a new instance of <see cref="Vector3d" />, by explicitly
/// converting the <see cref="Vector3.X" />, <see cref="Vector3.Y" /> and <see cref="Vector3.Z" /> fields to
/// <see cref="float" />.
/// Explicit converts a <see cref="Vector3d" /> to a new instance of <see cref="Vector3" />, by explicitly converting the
/// <see cref="X" />, <see cref="Y" /> and <see cref="Z" /> fields to <see cref="float" />.
/// </summary>
/// <param name="vector">The vector to convert.</param>
/// <returns>The converted vector.</returns>
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();
}
/// <summary>
/// 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<Vector3d>, IFormattable
/// <returns>The dot product.</returns>
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);
}
/// <summary>
/// Converts a <see cref="Vector3" /> to a new instance of <see cref="Vector3d" />, by implicitly converting the
/// <see cref="Vector3.X" />, <see cref="Vector3.Y" /> and <see cref="Vector3.Z" /> fields to <see cref="double" />.
/// </summary>
/// <param name="vector">The vector to convert.</param>
/// <returns>The converted vector.</returns>
public static Vector3d FromVector3(in Vector3 vector)
{
return new Vector3d(vector.X, vector.Y, vector.Z);
}
/// <summary>
@ -517,4 +532,14 @@ public struct Vector3d : IEquatable<Vector3d>, IFormattable
builder.Append('>');
return builder.ToString();
}
/// <summary>
/// Converts this instance to a new instance of <see cref="Vector3" />, by explicitly converting the <see cref="X" />,
/// <see cref="Y" /> and <see cref="Z" /> fields to <see cref="float" />.
/// </summary>
/// <returns>The converted vector.</returns>
public readonly Vector3 ToVector3()
{
return new Vector3((float)X, (float)Y, (float)Z);
}
}