From da5f31f9d7bd0e2cd11658f567cc7376ae8e26a3 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Fri, 27 Jun 2025 12:00:46 +0300 Subject: [PATCH] refactor: made equality operators consistent in primitives & added missing ones --- Engine.Core/Primitives/AABB.cs | 5 ++++- Engine.Core/Primitives/Circle.cs | 5 ++++- Engine.Core/Primitives/ColorHSV.cs | 4 ++-- Engine.Core/Primitives/Line2D.cs | 5 ++++- Engine.Core/Primitives/Line2DEquation.cs | 5 ++++- Engine.Core/Primitives/Projection1D.cs | 5 ++++- Engine.Core/Primitives/Ray2D.cs | 6 +++--- Engine.Core/Primitives/Triangle.cs | 5 ++++- 8 files changed, 29 insertions(+), 11 deletions(-) diff --git a/Engine.Core/Primitives/AABB.cs b/Engine.Core/Primitives/AABB.cs index 7a04cb7..142e885 100644 --- a/Engine.Core/Primitives/AABB.cs +++ b/Engine.Core/Primitives/AABB.cs @@ -38,6 +38,9 @@ public readonly struct AABB(Vector2D lowerBoundary, Vector2D upperBoundary) /// 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; + /// /// Creates an from a collection of s. /// @@ -78,7 +81,7 @@ public readonly struct AABB(Vector2D lowerBoundary, Vector2D upperBoundary) /// /// The object to compare with the current . /// if the specified object is equal to the current ; otherwise, . - 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; /// /// Generates a hash code for the . diff --git a/Engine.Core/Primitives/Circle.cs b/Engine.Core/Primitives/Circle.cs index 2d63ec3..9c3f2ae 100644 --- a/Engine.Core/Primitives/Circle.cs +++ b/Engine.Core/Primitives/Circle.cs @@ -38,6 +38,9 @@ public readonly struct Circle(Vector2D center, float radius) /// 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; + /// /// Sets the center of the . /// @@ -83,7 +86,7 @@ public readonly struct Circle(Vector2D center, float radius) /// /// The object to compare with the current . /// if the specified object is equal to the current ; otherwise, . - 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; /// /// Generates a hash code for the . diff --git a/Engine.Core/Primitives/ColorHSV.cs b/Engine.Core/Primitives/ColorHSV.cs index 35c81c7..cec3064 100644 --- a/Engine.Core/Primitives/ColorHSV.cs +++ b/Engine.Core/Primitives/ColorHSV.cs @@ -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) diff --git a/Engine.Core/Primitives/Line2D.cs b/Engine.Core/Primitives/Line2D.cs index d4ddbb4..ee4e272 100644 --- a/Engine.Core/Primitives/Line2D.cs +++ b/Engine.Core/Primitives/Line2D.cs @@ -43,6 +43,9 @@ public readonly struct Line2D(Vector2D from, Vector2D to) /// 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; + /// /// The equation of the defined by this segment. /// @@ -192,7 +195,7 @@ public readonly struct Line2D(Vector2D from, Vector2D to) /// /// The object to compare with the current . /// if the specified object is equal to the current ; otherwise, . - 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; /// /// Generates a hash code for the . diff --git a/Engine.Core/Primitives/Line2DEquation.cs b/Engine.Core/Primitives/Line2DEquation.cs index 5b227f5..19033bf 100644 --- a/Engine.Core/Primitives/Line2DEquation.cs +++ b/Engine.Core/Primitives/Line2DEquation.cs @@ -21,6 +21,9 @@ public readonly struct Line2DEquation(float slope, float offsetY) /// 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; + /// /// Resolves the Y coordinate for a given X coordinate using the . /// @@ -44,7 +47,7 @@ public readonly struct Line2DEquation(float slope, float offsetY) /// /// The object to compare with the current . /// if the specified object is equal to the current ; otherwise, . - 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; /// /// Generates a hash code for the . diff --git a/Engine.Core/Primitives/Projection1D.cs b/Engine.Core/Primitives/Projection1D.cs index d144125..599d53a 100644 --- a/Engine.Core/Primitives/Projection1D.cs +++ b/Engine.Core/Primitives/Projection1D.cs @@ -21,6 +21,9 @@ public readonly struct Projection1D(float min, float max) /// 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; + /// /// Checks if two projections overlap. /// @@ -86,7 +89,7 @@ public readonly struct Projection1D(float min, float max) /// /// The object to compare with the current . /// if the specified object is equal to the current ; otherwise, . - 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; /// /// Generates a hash code for the . diff --git a/Engine.Core/Primitives/Ray2D.cs b/Engine.Core/Primitives/Ray2D.cs index ed57996..f536179 100644 --- a/Engine.Core/Primitives/Ray2D.cs +++ b/Engine.Core/Primitives/Ray2D.cs @@ -22,8 +22,8 @@ public readonly struct Ray2D(Vector2D Origin, Vector2D Direction) /// 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); /// @@ -71,7 +71,7 @@ public readonly struct Ray2D(Vector2D Origin, Vector2D Direction) /// /// The object to compare with the current . /// if the specified object is equal to the current ; otherwise, . - 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; /// /// Generates a hash code for the . diff --git a/Engine.Core/Primitives/Triangle.cs b/Engine.Core/Primitives/Triangle.cs index 064b047..087d977 100644 --- a/Engine.Core/Primitives/Triangle.cs +++ b/Engine.Core/Primitives/Triangle.cs @@ -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) /// /// The object to compare with the current . /// if the specified object is equal to the current ; otherwise, . - 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; /// /// Generates a hash code for the .