refactor: made equality operators consistent in primitives & added missing ones
This commit is contained in:
parent
fa1614f238
commit
da5f31f9d7
@ -38,6 +38,9 @@ public readonly struct AABB(Vector2D lowerBoundary, Vector2D upperBoundary)
|
||||
/// </summary>
|
||||
public readonly Vector2D SizeHalf => Size * .5f;
|
||||
|
||||
public static bool operator ==(AABB left, AABB right) => left.UpperBoundary == right.UpperBoundary && left.LowerBoundary == right.LowerBoundary;
|
||||
public static bool operator !=(AABB left, AABB right) => left.UpperBoundary != right.UpperBoundary || left.LowerBoundary != right.LowerBoundary;
|
||||
|
||||
/// <summary>
|
||||
/// Creates an <see cref="AABB"/> from a collection of <see cref="Vector2D"/>s.
|
||||
/// </summary>
|
||||
@ -78,7 +81,7 @@ public readonly struct AABB(Vector2D lowerBoundary, Vector2D upperBoundary)
|
||||
/// </summary>
|
||||
/// <param name="obj">The object to compare with the current <see cref="AABB"/>.</param>
|
||||
/// <returns><see cref="true"/> if the specified object is equal to the current <see cref="AABB"/>; otherwise, <see cref="false"/>.</returns>
|
||||
public override bool Equals(object? obj) => obj is AABB aabb && LowerBoundary.Equals(aabb.LowerBoundary) && UpperBoundary.Equals(aabb.UpperBoundary);
|
||||
public override bool Equals(object? obj) => obj is AABB aabb && this == aabb;
|
||||
|
||||
/// <summary>
|
||||
/// Generates a hash code for the <see cref="AABB"/>.
|
||||
|
@ -38,6 +38,9 @@ public readonly struct Circle(Vector2D center, float radius)
|
||||
/// </summary>
|
||||
public static readonly Circle UnitCircle = new(Vector2D.Zero, 1f);
|
||||
|
||||
public static bool operator ==(Circle left, Circle right) => left.Center == right.Center && left.Radius == right.Radius;
|
||||
public static bool operator !=(Circle left, Circle right) => left.Center != right.Center || left.Radius != right.Radius;
|
||||
|
||||
/// <summary>
|
||||
/// Sets the center of the <see cref="Circle"/>.
|
||||
/// </summary>
|
||||
@ -83,7 +86,7 @@ public readonly struct Circle(Vector2D center, float radius)
|
||||
/// </summary>
|
||||
/// <param name="obj">The object to compare with the current <see cref="Circle"/>.</param>
|
||||
/// <returns><see cref="true"/> if the specified object is equal to the current <see cref="Circle"/>; otherwise, <see cref="false"/>.</returns>
|
||||
public override bool Equals(object? obj) => obj is Circle circle && Center.Equals(circle.Center) && Radius.Equals(circle.Radius);
|
||||
public override bool Equals(object? obj) => obj is Circle circle && this == circle;
|
||||
|
||||
/// <summary>
|
||||
/// Generates a hash code for the <see cref="Circle"/>.
|
||||
|
@ -34,8 +34,8 @@ public readonly struct ColorHSV(float hue, float saturation, float value)
|
||||
public static ColorHSV operator *(ColorHSV color, float value) => new((color.Hue * value).Clamp(0f, 1f), (color.Saturation * value).Clamp(0f, 1f), (color.Value * value).Clamp(0f, 1f));
|
||||
public static ColorHSV operator *(float value, ColorHSV color) => new((color.Hue * value).Clamp(0f, 1f), (color.Saturation * value).Clamp(0f, 1f), (color.Value * value).Clamp(0f, 1f));
|
||||
public static ColorHSV operator /(ColorHSV color, float value) => new((color.Hue / value).Clamp(0f, 1f), (color.Saturation / value).Clamp(0f, 1f), (color.Value / value).Clamp(0f, 1f));
|
||||
public static bool operator ==(ColorHSV left, ColorHSV right) => left.Hue.ApproximatelyEquals(right.Hue) && left.Saturation.ApproximatelyEquals(right.Saturation) && left.Value.ApproximatelyEquals(right.Value);
|
||||
public static bool operator !=(ColorHSV left, ColorHSV right) => !left.Hue.ApproximatelyEquals(right.Hue) || !left.Saturation.ApproximatelyEquals(right.Saturation) || !left.Value.ApproximatelyEquals(right.Value);
|
||||
public static bool operator ==(ColorHSV left, ColorHSV right) => left.Hue == right.Hue && left.Saturation == right.Saturation && left.Value == right.Value;
|
||||
public static bool operator !=(ColorHSV left, ColorHSV right) => left.Hue != right.Hue || left.Saturation != right.Saturation || left.Value != right.Value;
|
||||
|
||||
public static implicit operator ColorHSV(ColorRGBA rgba) => (ColorRGB)rgba;
|
||||
public static implicit operator ColorHSV(ColorRGB rgb)
|
||||
|
@ -43,6 +43,9 @@ public readonly struct Line2D(Vector2D from, Vector2D to)
|
||||
/// </summary>
|
||||
public readonly float LengthSquared => From.FromTo(To).LengthSquared();
|
||||
|
||||
public static bool operator ==(Line2D left, Line2D right) => left.From == right.From && left.To == right.To;
|
||||
public static bool operator !=(Line2D left, Line2D right) => left.From != right.From || left.To != right.To;
|
||||
|
||||
/// <summary>
|
||||
/// The equation of the <see cref="Line2D"/> defined by this <see cref="Line2D"/> segment.
|
||||
/// </summary>
|
||||
@ -192,7 +195,7 @@ public readonly struct Line2D(Vector2D from, Vector2D to)
|
||||
/// </summary>
|
||||
/// <param name="obj">The object to compare with the current <see cref="Line2D"/>.</param>
|
||||
/// <returns><see cref="true"/> if the specified object is equal to the current <see cref="Line2D"/>; otherwise, <see cref="false"/>.</returns>
|
||||
public override bool Equals(object? obj) => obj is Line2D line2D && From.Equals(line2D.From) && To.Equals(line2D.To);
|
||||
public override bool Equals(object? obj) => obj is Line2D line2D && this == line2D;
|
||||
|
||||
/// <summary>
|
||||
/// Generates a hash code for the <see cref="Line2D"/>.
|
||||
|
@ -21,6 +21,9 @@ public readonly struct Line2DEquation(float slope, float offsetY)
|
||||
/// </summary>
|
||||
public readonly float OffsetY = offsetY;
|
||||
|
||||
public static bool operator ==(Line2DEquation left, Line2DEquation right) => left.Slope == right.Slope && left.OffsetY == right.OffsetY;
|
||||
public static bool operator !=(Line2DEquation left, Line2DEquation right) => left.Slope != right.Slope || left.OffsetY != right.OffsetY;
|
||||
|
||||
/// <summary>
|
||||
/// Resolves the Y coordinate for a given X coordinate using the <see cref="Line2DEquation"/>.
|
||||
/// </summary>
|
||||
@ -44,7 +47,7 @@ public readonly struct Line2DEquation(float slope, float offsetY)
|
||||
/// </summary>
|
||||
/// <param name="obj">The object to compare with the current <see cref="Line2DEquation"/>.</param>
|
||||
/// <returns><see cref="true"/> if the specified object is equal to the current <see cref="Line2DEquation"/>; otherwise, <see cref="false"/>.</returns>
|
||||
public override bool Equals(object? obj) => obj is Line2DEquation lineEquation && Slope.Equals(lineEquation.Slope) && OffsetY.Equals(lineEquation.OffsetY);
|
||||
public override bool Equals(object? obj) => obj is Line2DEquation lineEquation && this == lineEquation;
|
||||
|
||||
/// <summary>
|
||||
/// Generates a hash code for the <see cref="Line2DEquation"/>.
|
||||
|
@ -21,6 +21,9 @@ public readonly struct Projection1D(float min, float max)
|
||||
/// </summary>
|
||||
public readonly float Max = max;
|
||||
|
||||
public static bool operator ==(Projection1D left, Projection1D right) => left.Min == right.Min && left.Max == right.Max;
|
||||
public static bool operator !=(Projection1D left, Projection1D right) => left.Min != right.Min || left.Max != right.Max;
|
||||
|
||||
/// <summary>
|
||||
/// Checks if two projections overlap.
|
||||
/// </summary>
|
||||
@ -86,7 +89,7 @@ public readonly struct Projection1D(float min, float max)
|
||||
/// </summary>
|
||||
/// <param name="obj">The object to compare with the current <see cref="Projection1D"/>.</param>
|
||||
/// <returns><see cref="true"/> if the specified object is equal to the current <see cref="Projection1D"/>; otherwise, <see cref="false"/>.</returns>
|
||||
public override bool Equals(object? obj) => obj is Projection1D projection1D && Min.Equals(projection1D.Min) && Max.Equals(projection1D.Max);
|
||||
public override bool Equals(object? obj) => obj is Projection1D projection1D && this == projection1D;
|
||||
|
||||
/// <summary>
|
||||
/// Generates a hash code for the <see cref="Projection1D"/>.
|
||||
|
@ -22,8 +22,8 @@ public readonly struct Ray2D(Vector2D Origin, Vector2D Direction)
|
||||
/// </summary>
|
||||
public readonly Ray2D Reversed => new(Origin, -Direction);
|
||||
|
||||
public static bool operator ==(Ray2D left, Ray2D right) => left.Origin == right.Origin;
|
||||
public static bool operator !=(Ray2D left, Ray2D right) => left.Origin != right.Origin;
|
||||
public static bool operator ==(Ray2D left, Ray2D right) => left.Origin == right.Origin && left.Direction == right.Direction;
|
||||
public static bool operator !=(Ray2D left, Ray2D right) => left.Origin != right.Origin || left.Direction != right.Direction;
|
||||
public static implicit operator Ray2D(Line2D line) => new(line.From, line.From.FromTo(line.To).Normalized);
|
||||
|
||||
/// <summary>
|
||||
@ -71,7 +71,7 @@ public readonly struct Ray2D(Vector2D Origin, Vector2D Direction)
|
||||
/// </summary>
|
||||
/// <param name="obj">The object to compare with the current <see cref="Ray2D"/>.</param>
|
||||
/// <returns><see cref="true"/> if the specified object is equal to the current <see cref="Ray2D"/>; otherwise, <see cref="false"/>.</returns>
|
||||
public override bool Equals(object? obj) => obj is Ray2D ray2D && Origin.Equals(ray2D.Origin) && Direction.Equals(ray2D.Direction);
|
||||
public override bool Equals(object? obj) => obj is Ray2D ray2D && this == ray2D;
|
||||
|
||||
/// <summary>
|
||||
/// Generates a hash code for the <see cref="Ray2D"/>.
|
||||
|
@ -7,6 +7,9 @@ public readonly struct Triangle(Vector2D A, Vector2D B, Vector2D C)
|
||||
public readonly Vector2D B { get; init; } = B;
|
||||
public readonly Vector2D C { get; init; } = C;
|
||||
|
||||
public static bool operator ==(Triangle left, Triangle right) => left.A == right.A && left.B == right.B && left.C == right.C;
|
||||
public static bool operator !=(Triangle left, Triangle right) => left.A != right.A || left.B != right.B || left.C != right.C;
|
||||
|
||||
public readonly float Area
|
||||
=> .5f * Math.Abs(
|
||||
A.X * (B.Y - C.Y) +
|
||||
@ -50,7 +53,7 @@ public readonly struct Triangle(Vector2D A, Vector2D B, Vector2D C)
|
||||
/// </summary>
|
||||
/// <param name="obj">The object to compare with the current <see cref="Triangle"/>.</param>
|
||||
/// <returns><see cref="true"/> if the specified object is equal to the current <see cref="Triangle"/>; otherwise, <see cref="false"/>.</returns>
|
||||
public override bool Equals(object? obj) => obj is Triangle triangle && A.Equals(triangle.A) && B.Equals(triangle.B) && C.Equals(triangle.C);
|
||||
public override bool Equals(object? obj) => obj is Triangle triangle && this == triangle;
|
||||
|
||||
/// <summary>
|
||||
/// Generates a hash code for the <see cref="Triangle"/>.
|
||||
|
Loading…
x
Reference in New Issue
Block a user