|
| 1 | +/* |
| 2 | + * COPYRIGHT: See COPYING in the top level directory |
| 3 | + * PROJECT: Imaging |
| 4 | + * FILE: PointId.cs |
| 5 | + * PURPOSE: A more clever way to handle some 2D coordinate Stuff |
| 6 | + * PROGRAMER: Peter Geinitz (Wayfarer) |
| 7 | + */ |
| 8 | + |
| 9 | +// ReSharper disable MemberCanBeInternal |
| 10 | +// ReSharper disable MemberCanBePrivate.Global |
| 11 | +// ReSharper disable BadBracesSpaces |
| 12 | +// ReSharper disable ArrangeObjectCreationWhenTypeEvident |
| 13 | +// ReSharper disable UnusedMember.Global |
| 14 | + |
| 15 | +using System; |
| 16 | +using System.Diagnostics; |
| 17 | +using System.Runtime.CompilerServices; |
| 18 | + |
| 19 | +namespace Imaging |
| 20 | +{ |
| 21 | + /// <inheritdoc /> |
| 22 | + /// <summary> |
| 23 | + /// Coordinate 2d Helper Class |
| 24 | + /// </summary> |
| 25 | + [DebuggerDisplay("{ToString()}")] |
| 26 | + public readonly struct PointId : IEquatable<PointId> |
| 27 | + { |
| 28 | + /// <summary> |
| 29 | + /// Initializes a new instance of the <see cref="PointId" /> class. |
| 30 | + /// </summary> |
| 31 | + /// <param name="x">The x.</param> |
| 32 | + /// <param name="y">The y.</param> |
| 33 | + public PointId(int x, int y) |
| 34 | + { |
| 35 | + X = x; |
| 36 | + Y = y; |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Initializes a new instance of the <see cref="PointId" /> class. |
| 41 | + /// </summary> |
| 42 | + /// <param name="x">The x in double.</param> |
| 43 | + /// <param name="y">The yin double.</param> |
| 44 | + public PointId(double x, double y) |
| 45 | + { |
| 46 | + X = (int)Math.Round(x, 1, MidpointRounding.AwayFromZero); |
| 47 | + Y = (int)Math.Round(y, 1, MidpointRounding.AwayFromZero); |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Initializes a new instance of the <see cref="PointId" /> class. |
| 52 | + /// </summary> |
| 53 | + /// <param name="x">The x.</param> |
| 54 | + /// <param name="y">The y.</param> |
| 55 | + /// <param name="width">The width.</param> |
| 56 | + public PointId(int x, int y, int width) |
| 57 | + { |
| 58 | + X = x; |
| 59 | + Y = y; |
| 60 | + Id = CalculateId(x, y, width); |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Initializes a new instance of the <see cref="PointId" /> class. |
| 65 | + /// </summary> |
| 66 | + public PointId() |
| 67 | + { |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Gets the null point. |
| 72 | + /// </summary> |
| 73 | + /// <value> |
| 74 | + /// The null point. |
| 75 | + /// </value> |
| 76 | + public static PointId NullPoint { get; } = new(0, 0); |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Gets the identifier of the Coordinate in the 2D System. |
| 80 | + /// </summary> |
| 81 | + /// <value> |
| 82 | + /// The identifier. |
| 83 | + /// </value> |
| 84 | + public int Id { get; } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Gets or sets the y. |
| 88 | + /// </summary> |
| 89 | + /// <value> |
| 90 | + /// The y. |
| 91 | + /// </value> |
| 92 | + public int Y { get; } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Gets or sets the x. |
| 96 | + /// </summary> |
| 97 | + /// <value> |
| 98 | + /// The x. |
| 99 | + /// </value> |
| 100 | + public int X { get; } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Gets the instance. |
| 104 | + /// </summary> |
| 105 | + /// <param name="id">The identifier.</param> |
| 106 | + /// <param name="width">The width.</param> |
| 107 | + /// <returns>Instance of Coordinate 2D with the help of the Id</returns> |
| 108 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 109 | + public static PointId FromId(int id, int width) => |
| 110 | + new(id % width, id / width); |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Converts to id. |
| 114 | + /// </summary> |
| 115 | + /// <param name="width">The width.</param> |
| 116 | + /// <returns>Id of Point</returns> |
| 117 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 118 | + public int ToId(int width) => Y * width + X; |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Equals the specified other. |
| 122 | + /// </summary> |
| 123 | + /// <param name="other">The other.</param> |
| 124 | + /// <returns>Equal or not</returns> |
| 125 | + public bool Equals(PointId other) |
| 126 | + { |
| 127 | + return X.Equals(other.X) && Y.Equals(other.Y); |
| 128 | + } |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Determines whether the specified <see cref="object" />, is equal to this instance. |
| 132 | + /// </summary> |
| 133 | + /// <param name="obj">The <see cref="object" /> to compare with this instance.</param> |
| 134 | + /// <returns> |
| 135 | + /// <c>true</c> if the specified <see cref="object" /> is equal to this instance; otherwise, <c>false</c>. |
| 136 | + /// </returns> |
| 137 | + public override bool Equals(object obj) |
| 138 | + { |
| 139 | + return obj is PointId other && Equals(other); |
| 140 | + } |
| 141 | + |
| 142 | + /// <summary> |
| 143 | + /// Implements the operator ==. |
| 144 | + /// </summary> |
| 145 | + /// <param name="first">The first.</param> |
| 146 | + /// <param name="second">The second.</param> |
| 147 | + /// <returns> |
| 148 | + /// The result of the operator. |
| 149 | + /// </returns> |
| 150 | + public static bool operator ==(PointId first, PointId second) |
| 151 | + { |
| 152 | + return first.X == second.X && first.Y == second.Y; |
| 153 | + } |
| 154 | + |
| 155 | + /// <summary> |
| 156 | + /// Implements the operator !=. |
| 157 | + /// </summary> |
| 158 | + /// <param name="first">The first.</param> |
| 159 | + /// <param name="second">The second.</param> |
| 160 | + /// <returns> |
| 161 | + /// The result of the operator. |
| 162 | + /// </returns> |
| 163 | + public static bool operator !=(PointId first, PointId second) |
| 164 | + { |
| 165 | + return first.X != second.X || first.Y != second.Y; |
| 166 | + } |
| 167 | + |
| 168 | + /// <inheritdoc /> |
| 169 | + /// <summary> |
| 170 | + /// Returns a hash code for this instance. |
| 171 | + /// </summary> |
| 172 | + /// <returns> |
| 173 | + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. |
| 174 | + /// </returns> |
| 175 | + public override int GetHashCode() |
| 176 | + { |
| 177 | + return HashCode.Combine(X, Y); |
| 178 | + } |
| 179 | + |
| 180 | + /// <summary> |
| 181 | + /// Calculates the identifier. |
| 182 | + /// </summary> |
| 183 | + /// <param name="x">The x.</param> |
| 184 | + /// <param name="y">The y.</param> |
| 185 | + /// <param name="width">The width.</param> |
| 186 | + /// <returns>The id of the coordinate</returns> |
| 187 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 188 | + private static int CalculateId(int x, int y, int width) |
| 189 | + { |
| 190 | + return y * width + x; |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments