@@ -23,7 +23,7 @@ namespace Mathematics
2323 /// Coordinate 2d Helper Class
2424 /// </summary>
2525 [ DebuggerDisplay ( "{ToString()}" ) ]
26- public sealed class Coordinate2D : ICloneable
26+ public readonly struct Coordinate2D : ICloneable , IEquatable < Coordinate2D >
2727 {
2828 /// <summary>
2929 /// Initializes a new instance of the <see cref="Coordinate2D" /> class.
@@ -89,15 +89,15 @@ public Coordinate2D()
8989 /// <value>
9090 /// The y.
9191 /// </value>
92- public int Y { get ; set ; }
92+ public int Y { get ; }
9393
9494 /// <summary>
9595 /// Gets or sets the x.
9696 /// </summary>
9797 /// <value>
9898 /// The x.
9999 /// </value>
100- public int X { get ; set ; }
100+ public int X { get ; }
101101
102102 /// <inheritdoc />
103103 /// <summary>
@@ -118,10 +118,17 @@ public object Clone()
118118 /// <param name="id">The identifier.</param>
119119 /// <param name="width">The width.</param>
120120 /// <returns>Instance of Coordinate 2D with the help of the Id</returns>
121- public static Coordinate2D GetInstance ( int id , int width )
122- {
123- return new Coordinate2D { X = IdToX ( id , width ) , Y = IdToY ( id , width ) } ;
124- }
121+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
122+ public static Coordinate2D FromId ( int id , int width ) =>
123+ new ( id % width , id / width ) ;
124+
125+ /// <summary>
126+ /// Converts to id.
127+ /// </summary>
128+ /// <param name="width">The width.</param>
129+ /// <returns>Id of Point</returns>
130+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
131+ public int ToId ( int width ) => ( Y * width ) + X ;
125132
126133 /// <summary>
127134 /// Equals the specified other.
@@ -130,7 +137,7 @@ public static Coordinate2D GetInstance(int id, int width)
130137 /// <returns>Equal or not</returns>
131138 public bool Equals ( Coordinate2D other )
132139 {
133- return X . Equals ( other ? . X ) && Y . Equals ( other ? . Y ) ;
140+ return X . Equals ( other . X ) && Y . Equals ( other . Y ) ;
134141 }
135142
136143 /// <summary>
@@ -155,7 +162,7 @@ public override bool Equals(object obj)
155162 /// </returns>
156163 public static bool operator == ( Coordinate2D first , Coordinate2D second )
157164 {
158- return second is not null && first is not null && first . X == second . X && first . Y == second . Y ;
165+ return first . X == second . X && first . Y == second . Y ;
159166 }
160167
161168 /// <summary>
@@ -168,7 +175,7 @@ public override bool Equals(object obj)
168175 /// </returns>
169176 public static bool operator != ( Coordinate2D first , Coordinate2D second )
170177 {
171- return second is not null && first is not null && ( first . X != second . X || first . Y != second . Y ) ;
178+ return ( first . X != second . X || first . Y != second . Y ) ;
172179 }
173180
174181 /// <inheritdoc />
@@ -222,48 +229,26 @@ private static int CalculateId(int x, int y, int width)
222229 }
223230
224231 /// <summary>
225- /// Converts a given identifier (ID) in a 2D grid to its corresponding X-coordinate.
226- /// The ID is treated as a linear index into a 2D grid, and this method computes the
227- /// X-coordinate based on that index and the grid's width.
232+ /// Implements the operator +.
228233 /// </summary>
229- /// <param name="masterId">The identifier (linear index) of the coordinate in the grid .</param>
230- /// <param name="width ">The width of the 2D grid (the number of columns) .</param>
234+ /// <param name="a">a .</param>
235+ /// <param name="b ">The b .</param>
231236 /// <returns>
232- /// The X-coordinate corresponding to the given identifier .
237+ /// The result of the operator .
233238 /// </returns>
234- /// <remarks>
235- /// This method uses the modulus operator to "wrap" the identifier into a valid X-coordinate.
236- /// It assumes that the IDs are assigned in a row-major order, starting at the top-left corner
237- /// and progressing left to right, top to bottom across the grid.
238- /// </remarks>
239- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
240- private static int IdToX ( int masterId , int width )
241- {
242- return masterId % width ;
243- }
239+ public static Coordinate2D operator + ( Coordinate2D a , Coordinate2D b ) =>
240+ new ( a . X + b . X , a . Y + b . Y ) ;
244241
245242 /// <summary>
246- /// Converts a given identifier (ID) in a 2D grid to its corresponding Y-coordinate.
247- /// The ID is treated as a linear index into a 2D grid, and this method computes the
248- /// Y-coordinate based on that index and the grid's width.
243+ /// Implements the operator -.
249244 /// </summary>
250- /// <param name="masterId">The identifier (linear index) of the coordinate in the grid .</param>
251- /// <param name="width ">The width of the 2D grid (the number of columns) .</param>
245+ /// <param name="a">a .</param>
246+ /// <param name="b ">The b .</param>
252247 /// <returns>
253- /// The Y-coordinate corresponding to the given identifier .
248+ /// The result of the operator .
254249 /// </returns>
255- /// <remarks>
256- /// This method uses integer division to calculate the row (Y-coordinate) of the identifier.
257- /// It assumes that the IDs are assigned in a row-major order, where each new row increments the Y-coordinate.
258- /// For example, if width = 5, the ID 6 would map to the second row (Y = 1), and ID 12 would map to the third row (Y =
259- /// 2).
260- /// </remarks>
261- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
262- private static int IdToY ( int masterId , int width )
263- {
264- return masterId / width ;
265- }
266-
250+ public static Coordinate2D operator - ( Coordinate2D a , Coordinate2D b ) =>
251+ new ( a . X - b . X , a . Y - b . Y ) ;
267252
268253 /// <summary>
269254 /// Performs an explicit conversion from <see cref="Vector3D" /> to <see cref="Coordinate2D" />.
0 commit comments