From dae6549badf134d517720bfefd49c8705f1412a6 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Fri, 27 Jun 2025 11:37:20 +0300 Subject: [PATCH] refactor: Equals methods to use equality operators on primitives --- Engine.Core/Primitives/ColorHSV.cs | 2 +- Engine.Core/Primitives/ColorRGB.cs | 2 +- Engine.Core/Primitives/ColorRGBA.cs | 2 +- Engine.Core/Primitives/Quaternion.cs | 2 +- Engine.Core/Primitives/Ray2D.cs | 2 ++ Engine.Core/Primitives/Vector2D.cs | 2 +- Engine.Core/Primitives/Vector3D.cs | 2 +- 7 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Engine.Core/Primitives/ColorHSV.cs b/Engine.Core/Primitives/ColorHSV.cs index 12a6aeb..35c81c7 100644 --- a/Engine.Core/Primitives/ColorHSV.cs +++ b/Engine.Core/Primitives/ColorHSV.cs @@ -142,7 +142,7 @@ 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 colorHSV && Hue.Equals(colorHSV.Hue) && Saturation.Equals(colorHSV.Saturation) && Value.Equals(colorHSV.Value); + public override bool Equals(object? obj) => obj is ColorHSV colorHSV && this == colorHSV; /// /// Generates a hash code for the . diff --git a/Engine.Core/Primitives/ColorRGB.cs b/Engine.Core/Primitives/ColorRGB.cs index d973458..ad52500 100644 --- a/Engine.Core/Primitives/ColorRGB.cs +++ b/Engine.Core/Primitives/ColorRGB.cs @@ -124,7 +124,7 @@ public readonly struct ColorRGB(byte r, byte g, byte b) /// /// 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 colorRGB && R.Equals(colorRGB.R) && G.Equals(colorRGB.G) && B.Equals(colorRGB.B); + public override bool Equals(object? obj) => obj is ColorRGB colorRGB && this == colorRGB; /// /// Generates a hash code for the . diff --git a/Engine.Core/Primitives/ColorRGBA.cs b/Engine.Core/Primitives/ColorRGBA.cs index 01fe2b8..9a1b285 100644 --- a/Engine.Core/Primitives/ColorRGBA.cs +++ b/Engine.Core/Primitives/ColorRGBA.cs @@ -107,7 +107,7 @@ public readonly struct ColorRGBA(byte r, byte g, byte b, byte a = 255) /// /// 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 colorRGBA && R.Equals(colorRGBA.R) && G.Equals(colorRGBA.G) && B.Equals(colorRGBA.B) && A.Equals(colorRGBA.A); + public override bool Equals(object? obj) => obj is ColorRGBA colorRGBA && this == colorRGBA; /// /// Generates a hash code for the . diff --git a/Engine.Core/Primitives/Quaternion.cs b/Engine.Core/Primitives/Quaternion.cs index 5124817..e2e1e53 100644 --- a/Engine.Core/Primitives/Quaternion.cs +++ b/Engine.Core/Primitives/Quaternion.cs @@ -287,7 +287,7 @@ public readonly struct Quaternion(float x, float y, float z, float w) /// /// 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 quaternion && X.Equals(quaternion.X) && Y.Equals(quaternion.Y) && Z.Equals(quaternion.Z) && W.Equals(quaternion.W); + public override bool Equals(object? obj) => obj is Quaternion quaternion && this == quaternion; /// /// Generates a hash code for the . diff --git a/Engine.Core/Primitives/Ray2D.cs b/Engine.Core/Primitives/Ray2D.cs index f72ad03..721dfdd 100644 --- a/Engine.Core/Primitives/Ray2D.cs +++ b/Engine.Core/Primitives/Ray2D.cs @@ -22,6 +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 implicit operator Ray2D(Line2D line) => new(line.From, line.From.FromTo(line.To).Normalized); /// diff --git a/Engine.Core/Primitives/Vector2D.cs b/Engine.Core/Primitives/Vector2D.cs index 3ede4d1..ff327e3 100644 --- a/Engine.Core/Primitives/Vector2D.cs +++ b/Engine.Core/Primitives/Vector2D.cs @@ -307,7 +307,7 @@ public readonly struct Vector2D(float x, float y) /// /// 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 vector2D && X.Equals(vector2D.X) && Y.Equals(vector2D.Y); + public override bool Equals(object? obj) => obj is Vector2D vector2D && this == vector2D; /// /// Generates a hash code for the . diff --git a/Engine.Core/Primitives/Vector3D.cs b/Engine.Core/Primitives/Vector3D.cs index 2582b30..190caf7 100644 --- a/Engine.Core/Primitives/Vector3D.cs +++ b/Engine.Core/Primitives/Vector3D.cs @@ -276,7 +276,7 @@ public readonly struct Vector3D(float x, float y, float z) /// /// 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 vector3D && X.Equals(vector3D.X) && Y.Equals(vector3D.Y) && Z.Equals(vector3D.Z); + public override bool Equals(object? obj) => obj is Vector3D vector3D && this == vector3D; /// /// Generates a hash code for the .