From e6b7b9953fe40376e509396a5b79fba7df3db244 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Sun, 15 Jun 2025 14:44:50 +0300 Subject: [PATCH] feat: ensured all primitives have ToString, GetHashCode & Equals methods --- Engine.Core/Primitives/AABB.cs | 25 ++++++++++++++++----- Engine.Core/Primitives/Circle.cs | 19 ++++++++++++++++ Engine.Core/Primitives/ColorHSV.cs | 14 ++++++------ Engine.Core/Primitives/ColorRGB.cs | 14 ++++++------ Engine.Core/Primitives/ColorRGBA.cs | 14 ++++++------ Engine.Core/Primitives/Line2D.cs | 19 ++++++++++++++++ Engine.Core/Primitives/Line2DEquation.cs | 19 ++++++++++++++++ Engine.Core/Primitives/Projection1D.cs | 19 ++++++++++++++++ Engine.Core/Primitives/Quaternion.cs | 16 +++++++------- Engine.Core/Primitives/Ray2D.cs | 19 ++++++++++++++++ Engine.Core/Primitives/Shape2D.cs | 28 ++++++++++++++++++++++++ Engine.Core/Primitives/Triangle.cs | 19 ++++++++++++++++ Engine.Core/Primitives/Vector2D.cs | 14 ++++++------ Engine.Core/Primitives/Vector3D.cs | 14 ++++++------ 14 files changed, 204 insertions(+), 49 deletions(-) diff --git a/Engine.Core/Primitives/AABB.cs b/Engine.Core/Primitives/AABB.cs index fad4569..7a04cb7 100644 --- a/Engine.Core/Primitives/AABB.cs +++ b/Engine.Core/Primitives/AABB.cs @@ -63,12 +63,6 @@ public readonly struct AABB(Vector2D lowerBoundary, Vector2D upperBoundary) return new(lowerBoundary, upperBoundary); } - /// - /// Converts the to its string representation. - /// - /// A string representation of the . - public override string ToString() => $"{nameof(AABB)}({LowerBoundary}, {UpperBoundary})"; - /// /// Checks if two s are approximately equal. /// @@ -78,6 +72,25 @@ public readonly struct AABB(Vector2D lowerBoundary, Vector2D upperBoundary) /// if the s are approximately equal; otherwise, . public static bool ApproximatelyEquals(AABB left, AABB right, float epsilon = float.Epsilon) => left.LowerBoundary.ApproximatelyEquals(right.LowerBoundary, epsilon) && left.UpperBoundary.ApproximatelyEquals(right.UpperBoundary, epsilon); + + /// + /// Determines whether the specified object is equal to the current . + /// + /// 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); + + /// + /// Generates a hash code for the . + /// + /// A hash code for the . + public override int GetHashCode() => System.HashCode.Combine(LowerBoundary, UpperBoundary); + + /// + /// Converts the to its string representation. + /// + /// A string representation of the . + public override string ToString() => $"{nameof(AABB)}({LowerBoundary}, {UpperBoundary})"; } /// diff --git a/Engine.Core/Primitives/Circle.cs b/Engine.Core/Primitives/Circle.cs index 1b4dccf..2d63ec3 100644 --- a/Engine.Core/Primitives/Circle.cs +++ b/Engine.Core/Primitives/Circle.cs @@ -77,6 +77,25 @@ public readonly struct Circle(Vector2D center, float radius) /// if the s are approximately equal; otherwise, . public static bool ApproximatelyEquals(Circle left, Circle right, float epsilon = float.Epsilon) => left.Center.ApproximatelyEquals(right.Center, epsilon) && left.Radius.ApproximatelyEquals(right.Radius, epsilon); + + /// + /// Determines whether the specified object is equal to the current . + /// + /// 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); + + /// + /// Generates a hash code for the . + /// + /// A hash code for the . + public override int GetHashCode() => System.HashCode.Combine(Center, Radius); + + /// + /// Converts the to its string representation. + /// + /// A string representation of the . + public override string ToString() => $"{nameof(Circle)}({Center}, {Radius})"; } /// diff --git a/Engine.Core/Primitives/ColorHSV.cs b/Engine.Core/Primitives/ColorHSV.cs index f4404c5..12a6aeb 100644 --- a/Engine.Core/Primitives/ColorHSV.cs +++ b/Engine.Core/Primitives/ColorHSV.cs @@ -127,12 +127,6 @@ public readonly struct ColorHSV(float hue, float saturation, float value) /// The interpolated . public static ColorHSV Lerp(ColorHSV from, ColorHSV to, float t) => from + FromTo(from, to) * t; - /// - /// Converts the to its string representation. - /// - /// A string representation of the . - public override string ToString() => $"{nameof(ColorHSV)}({Hue}, {Saturation}, {Value})"; - /// /// Checks if two s are approximately equal within a specified epsilon range. /// @@ -148,13 +142,19 @@ public readonly struct ColorHSV(float hue, float saturation, float value) /// /// 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 ColorHSV objVec && Hue.Equals(objVec.Hue) && Saturation.Equals(objVec.Saturation) && Value.Equals(objVec.Value); + public override bool Equals(object? obj) => obj is ColorHSV colorHSV && Hue.Equals(colorHSV.Hue) && Saturation.Equals(colorHSV.Saturation) && Value.Equals(colorHSV.Value); /// /// Generates a hash code for the . /// /// A hash code for the . public override int GetHashCode() => System.HashCode.Combine(Hue, Saturation, Value); + + /// + /// Converts the to its string representation. + /// + /// A string representation of the . + public override string ToString() => $"{nameof(ColorHSV)}({Hue}, {Saturation}, {Value})"; } /// diff --git a/Engine.Core/Primitives/ColorRGB.cs b/Engine.Core/Primitives/ColorRGB.cs index d37bfb6..d973458 100644 --- a/Engine.Core/Primitives/ColorRGB.cs +++ b/Engine.Core/Primitives/ColorRGB.cs @@ -119,24 +119,24 @@ public readonly struct ColorRGB(byte r, byte g, byte b) /// The interpolated . public static ColorRGB Lerp(ColorRGB from, ColorRGB to, float t) => from + FromTo(from, to) * t; - /// - /// Converts the to its string representation. - /// - /// A string representation of the . - public override string ToString() => $"{nameof(ColorRGB)}({R}, {G}, {B})"; - /// /// Determines whether the specified object is equal to the current . /// /// 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 ColorRGB objVec && R.Equals(objVec.R) && G.Equals(objVec.G) && B.Equals(objVec.B); + public override bool Equals(object? obj) => obj is ColorRGB colorRGB && R.Equals(colorRGB.R) && G.Equals(colorRGB.G) && B.Equals(colorRGB.B); /// /// Generates a hash code for the . /// /// A hash code for the . public override int GetHashCode() => System.HashCode.Combine(R, G, B); + + /// + /// Converts the to its string representation. + /// + /// A string representation of the . + public override string ToString() => $"{nameof(ColorRGB)}({R}, {G}, {B})"; } /// diff --git a/Engine.Core/Primitives/ColorRGBA.cs b/Engine.Core/Primitives/ColorRGBA.cs index 82d997e..01fe2b8 100644 --- a/Engine.Core/Primitives/ColorRGBA.cs +++ b/Engine.Core/Primitives/ColorRGBA.cs @@ -102,24 +102,24 @@ public readonly struct ColorRGBA(byte r, byte g, byte b, byte a = 255) /// The interpolated . public static ColorRGBA Lerp(ColorRGBA from, ColorRGBA to, float t) => from + FromTo(from, to) * t; - /// - /// Converts the to its string representation. - /// - /// A string representation of the . - public override string ToString() => $"{nameof(ColorRGBA)}({R}, {G}, {B}, {A})"; - /// /// Determines whether the specified object is equal to the current . /// /// 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 ColorRGBA objVec && R.Equals(objVec.R) && G.Equals(objVec.G) && B.Equals(objVec.B) && A.Equals(objVec.A); + public override bool Equals(object? obj) => obj is ColorRGBA colorRGBA && R.Equals(colorRGBA.R) && G.Equals(colorRGBA.G) && B.Equals(colorRGBA.B) && A.Equals(colorRGBA.A); /// /// Generates a hash code for the . /// /// A hash code for the . public override int GetHashCode() => System.HashCode.Combine(R, G, B, A); + + /// + /// Converts the to its string representation. + /// + /// A string representation of the . + public override string ToString() => $"{nameof(ColorRGBA)}({R}, {G}, {B}, {A})"; } /// diff --git a/Engine.Core/Primitives/Line2D.cs b/Engine.Core/Primitives/Line2D.cs index e5c2022..d4ddbb4 100644 --- a/Engine.Core/Primitives/Line2D.cs +++ b/Engine.Core/Primitives/Line2D.cs @@ -186,6 +186,25 @@ public readonly struct Line2D(Vector2D from, Vector2D to) /// if the s are approximately equal; otherwise, . public static bool ApproximatelyEquals(Line2D left, Line2D right, float epsilon = float.Epsilon) => left.From.ApproximatelyEquals(right.From, epsilon) && left.To.ApproximatelyEquals(right.To, epsilon); + + /// + /// Determines whether the specified object is equal to the current . + /// + /// 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); + + /// + /// Generates a hash code for the . + /// + /// A hash code for the . + public override int GetHashCode() => System.HashCode.Combine(From, To); + + /// + /// Converts the to its string representation. + /// + /// A string representation of the . + public override string ToString() => $"{nameof(Line2D)}({From}, {To})"; } /// diff --git a/Engine.Core/Primitives/Line2DEquation.cs b/Engine.Core/Primitives/Line2DEquation.cs index d76c20d..3c036c7 100644 --- a/Engine.Core/Primitives/Line2DEquation.cs +++ b/Engine.Core/Primitives/Line2DEquation.cs @@ -38,6 +38,25 @@ public readonly struct Line2DEquation(float slope, float offsetY) /// True if the line equations are approximately equal; otherwise, false. public static bool ApproximatelyEquals(Line2DEquation left, Line2DEquation right, float epsilon = float.Epsilon) => left.Slope.ApproximatelyEquals(right.Slope, epsilon) && left.OffsetY.ApproximatelyEquals(right.OffsetY, epsilon); + + /// + /// Determines whether the specified object is equal to the current . + /// + /// 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); + + /// + /// Generates a hash code for the . + /// + /// A hash code for the . + public override int GetHashCode() => System.HashCode.Combine(Slope, OffsetY); + + /// + /// Converts the to its string representation. + /// + /// A string representation of the . + public override string ToString() => $"{nameof(Line2DEquation)}({Slope}, {OffsetY})"; } /// diff --git a/Engine.Core/Primitives/Projection1D.cs b/Engine.Core/Primitives/Projection1D.cs index 8a78413..3a664b4 100644 --- a/Engine.Core/Primitives/Projection1D.cs +++ b/Engine.Core/Primitives/Projection1D.cs @@ -70,6 +70,25 @@ public readonly struct Projection1D(float min, float max) depth = 0f; return false; } + + /// + /// Determines whether the specified object is equal to the current . + /// + /// 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); + + /// + /// Generates a hash code for the . + /// + /// A hash code for the . + public override int GetHashCode() => System.HashCode.Combine(Min, Max); + + /// + /// Converts the to its string representation. + /// + /// A string representation of the . + public override string ToString() => $"{nameof(Projection1D)}({Min}, {Max})"; } /// diff --git a/Engine.Core/Primitives/Quaternion.cs b/Engine.Core/Primitives/Quaternion.cs index 75ab4f7..5124817 100644 --- a/Engine.Core/Primitives/Quaternion.cs +++ b/Engine.Core/Primitives/Quaternion.cs @@ -282,24 +282,24 @@ public readonly struct Quaternion(float x, float y, float z, float w) public static bool ApproximatelyEquals(Quaternion left, Quaternion right, float epsilon = float.Epsilon) => left.X.ApproximatelyEquals(right.X, epsilon) && left.Y.ApproximatelyEquals(right.Y, epsilon) && left.Z.ApproximatelyEquals(right.Z, epsilon) && left.W.ApproximatelyEquals(right.W, epsilon); - /// - /// Converts the to its string representation. - /// - /// A string representation of the . - public override string ToString() => $"{nameof(Quaternion)}({W}, {X}, {Y}, {Z})"; - /// /// Determines whether the specified object is equal to the current . /// /// 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 Quaternion objVec && X.Equals(objVec.X) && Y.Equals(objVec.Y) && Z.Equals(objVec.Z) && W.Equals(objVec.W); + public override bool Equals(object? obj) => obj is Quaternion quaternion && X.Equals(quaternion.X) && Y.Equals(quaternion.Y) && Z.Equals(quaternion.Z) && W.Equals(quaternion.W); /// /// Generates a hash code for the . /// /// A hash code for the . - public override int GetHashCode() => System.HashCode.Combine(X, Y, Z); + public override int GetHashCode() => System.HashCode.Combine(W, X, Y, Z); + + /// + /// Converts the to its string representation. + /// + /// A string representation of the . + public override string ToString() => $"{nameof(Quaternion)}({W}, {X}, {Y}, {Z})"; } /// diff --git a/Engine.Core/Primitives/Ray2D.cs b/Engine.Core/Primitives/Ray2D.cs index 3e16bed..f72ad03 100644 --- a/Engine.Core/Primitives/Ray2D.cs +++ b/Engine.Core/Primitives/Ray2D.cs @@ -53,6 +53,25 @@ public readonly struct Ray2D(Vector2D Origin, Vector2D Direction) return ray.Origin + ray.Direction * dot; } + + /// + /// Determines whether the specified object is equal to the current . + /// + /// 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); + + /// + /// Generates a hash code for the . + /// + /// A hash code for the . + public override int GetHashCode() => System.HashCode.Combine(Origin, Direction); + + /// + /// Converts the to its string representation. + /// + /// A string representation of the . + public override string ToString() => $"{nameof(Ray2D)}({Origin}, {Direction})"; } /// diff --git a/Engine.Core/Primitives/Shape2D.cs b/Engine.Core/Primitives/Shape2D.cs index fbef6a2..7a72dd0 100644 --- a/Engine.Core/Primitives/Shape2D.cs +++ b/Engine.Core/Primitives/Shape2D.cs @@ -251,6 +251,34 @@ public class Shape2D(List vertices) : IEnumerable return true; } + /// + /// Determines whether the specified object is equal to the current . + /// + /// 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 Shape2D shape2D && _vertices.Equals(shape2D._vertices); + + /// + /// Generates a hash code for the . + /// + /// A hash code for the . + public override int GetHashCode() => System.HashCode.Combine(Vertices); + + /// + /// Converts the to its string representation. + /// + /// A string representation of the . + public override string ToString() + { + System.Text.StringBuilder stringBuilder = new(Vertices[0].ToString()); + for (int i = 1; i < Vertices.Count; i++) + { + stringBuilder.Append(", "); + stringBuilder.Append(Vertices[i].ToString()); + } + return $"{nameof(Shape2D)}({stringBuilder})"; + } + /// public IEnumerator GetEnumerator() => Vertices.GetEnumerator(); diff --git a/Engine.Core/Primitives/Triangle.cs b/Engine.Core/Primitives/Triangle.cs index aead0e3..064b047 100644 --- a/Engine.Core/Primitives/Triangle.cs +++ b/Engine.Core/Primitives/Triangle.cs @@ -44,6 +44,25 @@ public readonly struct Triangle(Vector2D A, Vector2D B, Vector2D C) /// true if the s are approximately equal; otherwise, false. public static bool ApproximatelyEquals(Triangle left, Triangle right, float epsilon = float.Epsilon) => left.A.ApproximatelyEquals(right.A, epsilon) && left.B.ApproximatelyEquals(right.B, epsilon) && left.C.ApproximatelyEquals(right.C, epsilon); + + /// + /// Determines whether the specified object is equal to the current . + /// + /// 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); + + /// + /// Generates a hash code for the . + /// + /// A hash code for the . + public override int GetHashCode() => System.HashCode.Combine(A, B, C); + + /// + /// Converts the to its string representation. + /// + /// A string representation of the . + public override string ToString() => $"{nameof(Triangle)}({A}, {B}, {C})"; } public static class TriangleExtensions diff --git a/Engine.Core/Primitives/Vector2D.cs b/Engine.Core/Primitives/Vector2D.cs index 0d69f05..3ede4d1 100644 --- a/Engine.Core/Primitives/Vector2D.cs +++ b/Engine.Core/Primitives/Vector2D.cs @@ -302,24 +302,24 @@ public readonly struct Vector2D(float x, float y) public static bool ApproximatelyEquals(Vector2D left, Vector2D right, float epsilon = float.Epsilon) => left.X.ApproximatelyEquals(right.X, epsilon) && left.Y.ApproximatelyEquals(right.Y, epsilon); - /// - /// Converts the to its string representation. - /// - /// A string representation of the . - public override string ToString() => $"{nameof(Vector2D)}({X}, {Y})"; - /// /// Determines whether the specified object is equal to the current . /// /// 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 Vector2D objVec && X.Equals(objVec.X) && Y.Equals(objVec.Y); + public override bool Equals(object? obj) => obj is Vector2D vector2D && X.Equals(vector2D.X) && Y.Equals(vector2D.Y); /// /// Generates a hash code for the . /// /// A hash code for the . public override int GetHashCode() => System.HashCode.Combine(X, Y); + + /// + /// Converts the to its string representation. + /// + /// A string representation of the . + public override string ToString() => $"{nameof(Vector2D)}({X}, {Y})"; } /// diff --git a/Engine.Core/Primitives/Vector3D.cs b/Engine.Core/Primitives/Vector3D.cs index 420abe4..2582b30 100644 --- a/Engine.Core/Primitives/Vector3D.cs +++ b/Engine.Core/Primitives/Vector3D.cs @@ -271,24 +271,24 @@ public readonly struct Vector3D(float x, float y, float z) public static bool ApproximatelyEquals(Vector3D left, Vector3D right, float epsilon = float.Epsilon) => left.X.ApproximatelyEquals(right.X, epsilon) && left.Y.ApproximatelyEquals(right.Y, epsilon) && left.Z.ApproximatelyEquals(right.Z, epsilon); - /// - /// Converts the to its string representation. - /// - /// A string representation of the . - public override string ToString() => $"{nameof(Vector3D)}({X}, {Y}, {Z})"; - /// /// Determines whether the specified object is equal to the current . /// /// 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 Vector3D objVec && X.Equals(objVec.X) && Y.Equals(objVec.Y) && Z.Equals(objVec.Z); + public override bool Equals(object? obj) => obj is Vector3D vector3D && X.Equals(vector3D.X) && Y.Equals(vector3D.Y) && Z.Equals(vector3D.Z); /// /// Generates a hash code for the . /// /// A hash code for the . public override int GetHashCode() => System.HashCode.Combine(X, Y, Z); + + /// + /// Converts the to its string representation. + /// + /// A string representation of the . + public override string ToString() => $"{nameof(Vector3D)}({X}, {Y}, {Z})"; } ///