Skip to content

Commit f39968e

Browse files
author
LoneWandererProductions
committed
refine my coordinates
1 parent f382302 commit f39968e

9 files changed

Lines changed: 50 additions & 174 deletions

File tree

189 Bytes
Loading
48 Bytes
Loading

CommonLibraryTests/HelperMethods.cs

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -137,91 +137,5 @@ internal static BaseMatrix MatrixTestTwo(double[,] m1, double[,] m2)
137137

138138
return newMatrix;
139139
}
140-
141-
/// <summary>
142-
/// Plots the line if it is falling.
143-
/// </summary>
144-
/// <param name="from">Start coordinate.</param>
145-
/// <param name="to">End coordinate.</param>
146-
/// <returns>List of points on the line between both coordinates</returns>
147-
private static List<Coordinate2D> PlotLineLow(Coordinate2D from, Coordinate2D to)
148-
{
149-
var lst = new List<Coordinate2D>();
150-
151-
var dx = to.X - from.X;
152-
var dy = to.Y - from.Y;
153-
154-
var yi = 1;
155-
156-
if (dy < 0)
157-
{
158-
yi = -1;
159-
dy = -dy;
160-
}
161-
162-
var d = (2 * dy) - dx;
163-
var y = from.Y;
164-
165-
for (var i = from.X; i <= to.X; i++)
166-
{
167-
var point = new Coordinate2D { X = i, Y = y };
168-
lst.Add(point);
169-
170-
if (d > 0)
171-
{
172-
y += yi;
173-
d += 2 * (dy - dx);
174-
}
175-
else
176-
{
177-
d += 2 * dy;
178-
}
179-
}
180-
181-
return lst;
182-
}
183-
184-
/// <summary>
185-
/// Plots the line if the line is rising.
186-
/// </summary>
187-
/// <param name="from">Start coordinate.</param>
188-
/// <param name="to">End coordinate.</param>
189-
/// <returns>List of points on the line between both coordinates</returns>
190-
private static List<Coordinate2D> PlotLineHigh(Coordinate2D from, Coordinate2D to)
191-
{
192-
var lst = new List<Coordinate2D>();
193-
194-
var dx = to.X - from.X;
195-
var dy = to.Y - from.Y;
196-
197-
var xi = 1;
198-
199-
if (dx < 0)
200-
{
201-
xi = -1;
202-
dx = -dx;
203-
}
204-
205-
var d = (2 * dx) - dy;
206-
var x = from.X;
207-
208-
for (var i = from.Y; i < to.Y; i++)
209-
{
210-
var point = new Coordinate2D { X = x, Y = i };
211-
lst.Add(point);
212-
213-
if (d > 0)
214-
{
215-
x += xi;
216-
d += 2 * (dx - dy);
217-
}
218-
else
219-
{
220-
d += 2 * dx;
221-
}
222-
}
223-
224-
return lst;
225-
}
226140
}
227141
}

Imaging/Cif.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ public Color GetColor(int id)
328328
var dbm = DirectBitmap.GetInstance(image);
329329

330330
foreach (var (key, value) in CifImage)
331-
foreach (var coordinate in value.Select(id => Coordinate2D.GetInstance(id, Width)))
331+
foreach (var coordinate in value.Select(id => Coordinate2D.FromId(id, Width)))
332332
{
333333
dbm.SetPixel(coordinate.X, coordinate.Y, key);
334334
}

ImagingTests/HelperMethods.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private static List<Coordinate2D> PlotLineLow(Coordinate2D from, Coordinate2D to
6868

6969
for (var i = from.X; i <= to.X; i++)
7070
{
71-
var point = new Coordinate2D { X = i, Y = y };
71+
var point = new Coordinate2D(i, y);
7272
lst.Add(point);
7373

7474
if (d > 0)
@@ -111,7 +111,7 @@ private static List<Coordinate2D> PlotLineHigh(Coordinate2D from, Coordinate2D t
111111

112112
for (var i = from.Y; i < to.Y; i++)
113113
{
114-
var point = new Coordinate2D { X = x, Y = i };
114+
var point = new Coordinate2D(x, i);
115115
lst.Add(point);
116116

117117
if (d > 0)

Mathematics/Coordinate2D.cs

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -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" />.

Mathematics/Lines.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static List<Coordinate2D> BresenhamLine(Coordinate2D from, Coordinate2D t
4747
var error = dy2 - Math.Abs(dx);
4848
while (x != xEnd)
4949
{
50-
lst.Add(new Coordinate2D { X = x, Y = y });
50+
lst.Add(new Coordinate2D (x, y ));
5151
if (error > 0)
5252
{
5353
y += signY;
@@ -64,7 +64,7 @@ public static List<Coordinate2D> BresenhamLine(Coordinate2D from, Coordinate2D t
6464
var error = dx2 - Math.Abs(dy);
6565
while (y != yEnd)
6666
{
67-
lst.Add(new Coordinate2D { X = x, Y = y });
67+
lst.Add(new Coordinate2D(x, y));
6868
if (error > 0)
6969
{
7070
x += signX;
@@ -76,7 +76,7 @@ public static List<Coordinate2D> BresenhamLine(Coordinate2D from, Coordinate2D t
7676
}
7777
}
7878

79-
lst.Add(new Coordinate2D { X = x, Y = y }); // Add the last point
79+
lst.Add(new Coordinate2D(x, y)); // Add the last point
8080
return lst;
8181
}
8282

Solaris/Aurora.xaml.cs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -403,29 +403,17 @@ private void UpdateMapAndBitmap()
403403
/// <param name="e">The <see cref="MouseButtonEventArgs" /> instance containing the event data.</param>
404404
private void Touch_MouseDown(object sender, MouseButtonEventArgs e)
405405
{
406-
_cursor = new Coordinate2D();
407-
408406
var position = e.GetPosition(Touch);
409407

410-
if (position.X < AuroraTextureSize)
411-
{
412-
_cursor.X = 0;
413-
}
414-
else
415-
{
416-
_cursor.X = (int)position.X / AuroraTextureSize;
417-
}
408+
// Let standard integer division do the heavy lifting
409+
int gridX = (int)position.X / AuroraTextureSize;
410+
int gridY = (int)position.Y / AuroraTextureSize;
418411

419-
if (position.Y < AuroraTextureSize)
420-
{
421-
_cursor.Y = 0;
422-
}
423-
else
424-
{
425-
_cursor.Y = (int)position.Y / AuroraTextureSize;
426-
}
412+
// Create the immutable struct
413+
_cursor = new Coordinate2D(gridX, gridY);
427414

428-
var id = _cursor.CalculateId(AuroraWidth);
415+
// Get the ID using the new method name (or CalculateId if you kept the old name)
416+
var id = _cursor.ToId(AuroraWidth);
429417

430418
TileClicked?.Invoke(this, id);
431419
}

Solaris/Polaris.xaml.cs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -419,29 +419,18 @@ public void Initiate()
419419
/// <param name="e">The <see cref="MouseButtonEventArgs" /> instance containing the event data.</param>
420420
private void Touch_MouseDown(object sender, MouseButtonEventArgs e)
421421
{
422-
_cursor = new Coordinate2D();
423-
424422
var position = e.GetPosition(Touch);
425423

426-
if (position.X < PolarisTextureSize)
427-
{
428-
_cursor.X = 0;
429-
}
430-
else
431-
{
432-
_cursor.X = (int)position.X / PolarisTextureSize;
433-
}
424+
// 1. Calculate the values first.
425+
// Integer division naturally handles the 0 case for us!
426+
int gridX = (int)position.X / PolarisTextureSize;
427+
int gridY = (int)position.Y / PolarisTextureSize;
434428

435-
if (position.Y < PolarisTextureSize)
436-
{
437-
_cursor.Y = 0;
438-
}
439-
else
440-
{
441-
_cursor.Y = (int)position.Y / PolarisTextureSize;
442-
}
429+
// 2. Create the immutable struct all at once.
430+
_cursor = new Coordinate2D(gridX, gridY);
443431

444-
var id = _cursor.CalculateId(PolarisWidth);
432+
// 3. Calculate the 1D array index (using whatever you named the method)
433+
var id = _cursor.ToId(PolarisWidth);
445434

446435
Clicked?.Invoke(this, id);
447436
}

0 commit comments

Comments
 (0)