| | | 1 | | using MemoryPack; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Runtime.CompilerServices; |
| | | 5 | | using System.Text.Json.Serialization; |
| | | 6 | | |
| | | 7 | | namespace FixedMathSharp; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Represents a 2D vector with fixed-point precision, offering a range of mathematical operations |
| | | 11 | | /// and transformations such as rotation, scaling, reflection, and interpolation. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// The Vector2d struct is designed for applications that require precise numerical operations, |
| | | 15 | | /// such as games, simulations, or physics engines. It provides methods for common vector operations |
| | | 16 | | /// like addition, subtraction, dot product, cross product, distance calculations, and rotation. |
| | | 17 | | /// |
| | | 18 | | /// Use Cases: |
| | | 19 | | /// - Modeling 2D positions, directions, and velocities in fixed-point math environments. |
| | | 20 | | /// - Performing vector transformations, including rotations and reflections. |
| | | 21 | | /// - Handling interpolation and distance calculations in physics or simulation systems. |
| | | 22 | | /// - Useful for fixed-point math scenarios where floating-point precision is insufficient or not desired. |
| | | 23 | | /// </remarks> |
| | | 24 | | [Serializable] |
| | | 25 | | [MemoryPackable] |
| | | 26 | | public partial struct Vector2d : IEquatable<Vector2d>, IComparable<Vector2d>, IEqualityComparer<Vector2d> |
| | | 27 | | { |
| | | 28 | | #region Static Readonly Fields |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// (1, 0) |
| | | 32 | | /// </summary> |
| | | 33 | | public static readonly Vector2d DefaultRotation = new Vector2d(1, 0); |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// (0, 1) |
| | | 37 | | /// </summary> |
| | | 38 | | public static readonly Vector2d Forward = new Vector2d(0, 1); |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// (1, 0) |
| | | 42 | | /// </summary> |
| | | 43 | | public static readonly Vector2d Right = new Vector2d(1, 0); |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// (0, -1) |
| | | 47 | | /// </summary> |
| | | 48 | | public static readonly Vector2d Down = new Vector2d(0, -1); |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// (-1, 0) |
| | | 52 | | /// </summary> |
| | | 53 | | public static readonly Vector2d Left = new Vector2d(-1, 0); |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// (1, 1) |
| | | 57 | | /// </summary> |
| | | 58 | | public static readonly Vector2d One = new Vector2d(1, 1); |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// (-1, -1) |
| | | 62 | | /// </summary> |
| | | 63 | | public static readonly Vector2d Negative = new Vector2d(-1, -1); |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// (0, 0) |
| | | 67 | | /// </summary> |
| | | 68 | | public static readonly Vector2d Zero = new Vector2d(0, 0); |
| | | 69 | | |
| | | 70 | | #endregion |
| | | 71 | | |
| | | 72 | | #region Fields |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// The X component of the vector. |
| | | 76 | | /// </summary> |
| | | 77 | | [JsonInclude] |
| | | 78 | | [MemoryPackOrder(0)] |
| | | 79 | | public Fixed64 x; |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// The Y component of the vector. |
| | | 83 | | /// </summary> |
| | | 84 | | [JsonInclude] |
| | | 85 | | [MemoryPackOrder(1)] |
| | | 86 | | public Fixed64 y; |
| | | 87 | | |
| | | 88 | | #endregion |
| | | 89 | | |
| | | 90 | | #region Constructors |
| | | 91 | | |
| | 396 | 92 | | public Vector2d(int xInt, int yInt) : this((Fixed64)xInt, (Fixed64)yInt) { } |
| | | 93 | | |
| | 0 | 94 | | public Vector2d(float xFloat, float yFloat) : this((Fixed64)xFloat, (Fixed64)yFloat) { } |
| | | 95 | | |
| | 21 | 96 | | public Vector2d(double xDoub, double yDoub) : this((Fixed64)xDoub, (Fixed64)yDoub) { } |
| | | 97 | | |
| | | 98 | | [JsonConstructor] |
| | | 99 | | public Vector2d(Fixed64 x, Fixed64 y) |
| | 199 | 100 | | { |
| | 199 | 101 | | this.x = x; |
| | 199 | 102 | | this.y = y; |
| | 199 | 103 | | } |
| | | 104 | | |
| | | 105 | | #endregion |
| | | 106 | | |
| | | 107 | | #region Properties |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Rotates the vector to the right (90 degrees clockwise). |
| | | 111 | | /// </summary> |
| | | 112 | | [JsonIgnore] |
| | | 113 | | [MemoryPackIgnore] |
| | | 114 | | public Vector2d RotatedRight |
| | | 115 | | { |
| | | 116 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 117 | | get => new Vector2d(y, -x); |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Rotates the vector to the left (90 degrees counterclockwise). |
| | | 122 | | /// </summary> |
| | | 123 | | [JsonIgnore] |
| | | 124 | | [MemoryPackIgnore] |
| | | 125 | | public Vector2d RotatedLeft |
| | | 126 | | { |
| | | 127 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 128 | | get => new Vector2d(-y, x); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Gets the right-hand (counter-clockwise) normal vector. |
| | | 133 | | /// </summary> |
| | | 134 | | [JsonIgnore] |
| | | 135 | | [MemoryPackIgnore] |
| | | 136 | | public Vector2d RightHandNormal |
| | | 137 | | { |
| | | 138 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 139 | | get => new Vector2d(-y, x); |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Gets the left-hand (clockwise) normal vector. |
| | | 144 | | /// </summary> |
| | | 145 | | [JsonIgnore] |
| | | 146 | | [MemoryPackIgnore] |
| | | 147 | | public Vector2d LeftHandNormal |
| | | 148 | | { |
| | | 149 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 150 | | get => new Vector2d(y, -x); |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | /// <inheritdoc cref="GetNormalized(Vector2d)"/> |
| | | 154 | | [JsonIgnore] |
| | | 155 | | [MemoryPackIgnore] |
| | | 156 | | public Vector2d Normal |
| | | 157 | | { |
| | | 158 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 159 | | get => GetNormalized(this); |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// Returns the actual length of this vector (RO). |
| | | 164 | | /// </summary> |
| | | 165 | | [JsonIgnore] |
| | | 166 | | [MemoryPackIgnore] |
| | | 167 | | public Fixed64 Magnitude |
| | | 168 | | { |
| | | 169 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 15 | 170 | | get => GetMagnitude(this); |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Returns the square magnitude of the vector (avoids calculating the square root). |
| | | 175 | | /// </summary> |
| | | 176 | | [JsonIgnore] |
| | | 177 | | [MemoryPackIgnore] |
| | | 178 | | public Fixed64 SqrMagnitude |
| | | 179 | | { |
| | | 180 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 4 | 181 | | get => x * x + y * y; |
| | | 182 | | } |
| | | 183 | | |
| | | 184 | | /// <summary> |
| | | 185 | | /// Returns a long hash of the vector based on its x and y values. |
| | | 186 | | /// </summary> |
| | | 187 | | [JsonIgnore] |
| | | 188 | | [MemoryPackIgnore] |
| | | 189 | | public long LongStateHash |
| | | 190 | | { |
| | | 191 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 7 | 192 | | get => x.m_rawValue * 31 + y.m_rawValue * 7; |
| | | 193 | | } |
| | | 194 | | |
| | | 195 | | /// <summary> |
| | | 196 | | /// Returns a hash of the vector based on its state. |
| | | 197 | | /// </summary> |
| | | 198 | | [JsonIgnore] |
| | | 199 | | [MemoryPackIgnore] |
| | | 200 | | public int StateHash |
| | | 201 | | { |
| | | 202 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 5 | 203 | | get => (int)(LongStateHash % int.MaxValue); |
| | | 204 | | } |
| | | 205 | | |
| | | 206 | | [JsonIgnore] |
| | | 207 | | [MemoryPackIgnore] |
| | | 208 | | public Fixed64 this[int index] |
| | | 209 | | { |
| | | 210 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 211 | | get |
| | 4 | 212 | | { |
| | 4 | 213 | | return index switch |
| | 4 | 214 | | { |
| | 1 | 215 | | 0 => x, |
| | 1 | 216 | | 1 => y, |
| | 2 | 217 | | _ => throw new IndexOutOfRangeException("Invalid Vector2d index!"), |
| | 4 | 218 | | }; |
| | 2 | 219 | | } |
| | | 220 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 221 | | set |
| | 4 | 222 | | { |
| | 4 | 223 | | switch (index) |
| | | 224 | | { |
| | | 225 | | case 0: |
| | 1 | 226 | | x = value; |
| | 1 | 227 | | break; |
| | | 228 | | case 1: |
| | 1 | 229 | | y = value; |
| | 1 | 230 | | break; |
| | | 231 | | default: |
| | 2 | 232 | | throw new IndexOutOfRangeException("Invalid Vector2d index!"); |
| | | 233 | | } |
| | 2 | 234 | | } |
| | | 235 | | } |
| | | 236 | | |
| | | 237 | | #endregion |
| | | 238 | | |
| | | 239 | | #region Methods (Instance) |
| | | 240 | | |
| | | 241 | | /// <summary> |
| | | 242 | | /// Set x, y and z components of an existing Vector3. |
| | | 243 | | /// </summary> |
| | | 244 | | /// <param name="newX"></param> |
| | | 245 | | /// <param name="newY"></param> |
| | | 246 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 247 | | public void Set(Fixed64 newX, Fixed64 newY) |
| | 1 | 248 | | { |
| | 1 | 249 | | x = newX; |
| | 1 | 250 | | y = newY; |
| | 1 | 251 | | } |
| | | 252 | | |
| | | 253 | | /// <summary> |
| | | 254 | | /// Adds the specified values to the components of the vector in place and returns the modified vector. |
| | | 255 | | /// </summary> |
| | | 256 | | /// <param name="amount">The amount to add to the components.</param> |
| | | 257 | | /// <returns>The modified vector after addition.</returns> |
| | | 258 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 259 | | public Vector2d AddInPlace(Fixed64 amount) |
| | 2 | 260 | | { |
| | 2 | 261 | | x += amount; |
| | 2 | 262 | | y += amount; |
| | 2 | 263 | | return this; |
| | 2 | 264 | | } |
| | | 265 | | |
| | | 266 | | /// <summary> |
| | | 267 | | /// Adds the specified values to the components of the vector in place and returns the modified vector. |
| | | 268 | | /// </summary> |
| | | 269 | | /// <param name="xAmount">The amount to add to the x component.</param> |
| | | 270 | | /// <param name="yAmount">The amount to add to the y component.</param> |
| | | 271 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 272 | | public Vector2d AddInPlace(Fixed64 xAmount, Fixed64 yAmount) |
| | 2 | 273 | | { |
| | 2 | 274 | | x += xAmount; |
| | 2 | 275 | | y += yAmount; |
| | 2 | 276 | | return this; |
| | 2 | 277 | | } |
| | | 278 | | |
| | | 279 | | /// <inheritdoc cref="AddInPlace(Fixed64, Fixed64)"/> |
| | | 280 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 281 | | public Vector2d AddInPlace(Vector2d other) |
| | 1 | 282 | | { |
| | 1 | 283 | | AddInPlace(other.x, other.y); |
| | 1 | 284 | | return this; |
| | 1 | 285 | | } |
| | | 286 | | |
| | | 287 | | /// <summary> |
| | | 288 | | /// Subtracts the specified value from all components of the vector in place and returns the modified vector. |
| | | 289 | | /// </summary> |
| | | 290 | | /// <param name="amount">The amount to subtract from each component.</param> |
| | | 291 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 292 | | public Vector2d SubtractInPlace(Fixed64 amount) |
| | 2 | 293 | | { |
| | 2 | 294 | | x -= amount; |
| | 2 | 295 | | y -= amount; |
| | 2 | 296 | | return this; |
| | 2 | 297 | | } |
| | | 298 | | |
| | | 299 | | /// <summary> |
| | | 300 | | /// Subtracts the specified values from the components of the vector in place and returns the modified vector. |
| | | 301 | | /// </summary> |
| | | 302 | | /// <param name="xAmount">The amount to subtract from the x component.</param> |
| | | 303 | | /// <param name="yAmount">The amount to subtract from the y component.</param> |
| | | 304 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 305 | | public Vector2d SubtractInPlace(Fixed64 xAmount, Fixed64 yAmount) |
| | 2 | 306 | | { |
| | 2 | 307 | | x -= xAmount; |
| | 2 | 308 | | y -= yAmount; |
| | 2 | 309 | | return this; |
| | 2 | 310 | | } |
| | | 311 | | |
| | | 312 | | /// <summary> |
| | | 313 | | /// Subtracts the specified vector from the components of the vector in place and returns the modified vector. |
| | | 314 | | /// </summary> |
| | | 315 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 316 | | public Vector2d SubtractInPlace(Vector2d other) |
| | 1 | 317 | | { |
| | 1 | 318 | | SubtractInPlace(other.x, other.y); |
| | 1 | 319 | | return this; |
| | 1 | 320 | | } |
| | | 321 | | |
| | | 322 | | /// <summary> |
| | | 323 | | /// Scales the components of the vector by the specified scalar factor in place and returns the modified vector. |
| | | 324 | | /// </summary> |
| | | 325 | | /// <param name="scaleFactor">The scalar factor to multiply each component by.</param> |
| | | 326 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 327 | | public Vector2d ScaleInPlace(Fixed64 scaleFactor) |
| | 1 | 328 | | { |
| | 1 | 329 | | x *= scaleFactor; |
| | 1 | 330 | | y *= scaleFactor; |
| | 1 | 331 | | return this; |
| | 1 | 332 | | } |
| | | 333 | | |
| | | 334 | | /// <summary> |
| | | 335 | | /// Scales each component of the vector by the corresponding component of the given vector in place and returns the |
| | | 336 | | /// </summary> |
| | | 337 | | /// <param name="scale">The vector containing the scale factors for each component.</param> |
| | | 338 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 339 | | public Vector2d ScaleInPlace(Vector2d scale) |
| | 1 | 340 | | { |
| | 1 | 341 | | x *= scale.x; |
| | 1 | 342 | | y *= scale.y; |
| | 1 | 343 | | return this; |
| | 1 | 344 | | } |
| | | 345 | | |
| | | 346 | | /// <summary> |
| | | 347 | | /// Normalizes this vector in place, making its magnitude (length) equal to 1, and returns the modified vector. |
| | | 348 | | /// </summary> |
| | | 349 | | /// <remarks> |
| | | 350 | | /// If the vector is zero-length or already normalized, no operation is performed. |
| | | 351 | | /// This method modifies the current vector in place and supports method chaining. |
| | | 352 | | /// </remarks> |
| | | 353 | | /// <returns>The normalized vector.</returns> |
| | | 354 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 355 | | public Vector2d Normalize() |
| | 3 | 356 | | { |
| | 3 | 357 | | return this = GetNormalized(this); |
| | 3 | 358 | | } |
| | | 359 | | |
| | | 360 | | /// <summary> |
| | | 361 | | /// Normalizes this vector in place and outputs its original magnitude. |
| | | 362 | | /// </summary> |
| | | 363 | | /// <param name="mag">The original magnitude of the vector before normalization.</param> |
| | | 364 | | /// <remarks> |
| | | 365 | | /// If the vector is zero-length or already normalized, no operation is performed, but the original magnitude will s |
| | | 366 | | /// </remarks> |
| | | 367 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 368 | | public Vector2d Normalize(out Fixed64 mag) |
| | 3 | 369 | | { |
| | 3 | 370 | | mag = GetMagnitude(this); |
| | | 371 | | |
| | | 372 | | // If magnitude is zero, return a zero vector to avoid divide-by-zero errors |
| | 3 | 373 | | if (mag == Fixed64.Zero) |
| | 1 | 374 | | { |
| | 1 | 375 | | x = Fixed64.Zero; |
| | 1 | 376 | | y = Fixed64.Zero; |
| | 1 | 377 | | return this; |
| | | 378 | | } |
| | | 379 | | |
| | | 380 | | // If already normalized, return as-is |
| | 2 | 381 | | if (mag == Fixed64.One) |
| | 1 | 382 | | return this; |
| | | 383 | | |
| | 1 | 384 | | x /= mag; |
| | 1 | 385 | | y /= mag; |
| | | 386 | | |
| | 1 | 387 | | return this; |
| | 3 | 388 | | } |
| | | 389 | | |
| | | 390 | | /// <summary> |
| | | 391 | | /// Linearly interpolates this vector toward the target vector by the specified amount. |
| | | 392 | | /// </summary> |
| | | 393 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 394 | | public Vector2d LerpInPlace(Vector2d target, Fixed64 amount) |
| | 2 | 395 | | { |
| | 2 | 396 | | LerpInPlace(target.x, target.y, amount); |
| | 2 | 397 | | return this; |
| | 2 | 398 | | } |
| | | 399 | | |
| | | 400 | | /// <summary> |
| | | 401 | | /// Linearly interpolates this vector toward the target values by the specified amount. |
| | | 402 | | /// </summary> |
| | | 403 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 404 | | public Vector2d LerpInPlace(Fixed64 targetx, Fixed64 targety, Fixed64 amount) |
| | 3 | 405 | | { |
| | 3 | 406 | | if (amount >= Fixed64.One) |
| | 1 | 407 | | { |
| | 1 | 408 | | x = targetx; |
| | 1 | 409 | | y = targety; |
| | 1 | 410 | | } |
| | 2 | 411 | | else if (amount > Fixed64.Zero) |
| | 2 | 412 | | { |
| | 2 | 413 | | x = targetx * amount + x * (Fixed64.One - amount); |
| | 2 | 414 | | y = targety * amount + y * (Fixed64.One - amount); |
| | 2 | 415 | | } |
| | 3 | 416 | | return this; |
| | 3 | 417 | | } |
| | | 418 | | |
| | | 419 | | /// <summary> |
| | | 420 | | /// Linearly interpolates between two vectors. |
| | | 421 | | /// </summary> |
| | | 422 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 423 | | public static Vector2d Lerp(Vector2d a, Vector2d b, Fixed64 amount) |
| | 2 | 424 | | { |
| | 2 | 425 | | amount = FixedMath.Clamp01(amount); |
| | 2 | 426 | | return new Vector2d(a.x + (b.x - a.x) * amount, a.y + (b.y - a.y) * amount); |
| | 2 | 427 | | } |
| | | 428 | | |
| | | 429 | | /// <summary> |
| | | 430 | | /// Returns a new vector that is the result of linear interpolation toward the target by the specified amount. |
| | | 431 | | /// </summary> |
| | | 432 | | public Vector2d Lerped(Vector2d target, Fixed64 amount) |
| | 1 | 433 | | { |
| | 1 | 434 | | Vector2d vec = this; |
| | 1 | 435 | | vec.LerpInPlace(target.x, target.y, amount); |
| | 1 | 436 | | return vec; |
| | 1 | 437 | | } |
| | | 438 | | |
| | | 439 | | /// <summary> |
| | | 440 | | /// Rotates this vector by the specified cosine and sine values (counter-clockwise). |
| | | 441 | | /// </summary> |
| | | 442 | | public Vector2d RotateInPlace(Fixed64 cos, Fixed64 sin) |
| | 4 | 443 | | { |
| | 4 | 444 | | Fixed64 temp1 = x * cos - y * sin; |
| | 4 | 445 | | y = x * sin + y * cos; |
| | 4 | 446 | | x = temp1; |
| | 4 | 447 | | return this; |
| | 4 | 448 | | } |
| | | 449 | | |
| | | 450 | | /// <summary> |
| | | 451 | | /// Returns a new vector that is the result of rotating this vector by the specified cosine and sine values. |
| | | 452 | | /// </summary> |
| | | 453 | | public Vector2d Rotated(Fixed64 cos, Fixed64 sin) |
| | 2 | 454 | | { |
| | 2 | 455 | | Vector2d vec = this; |
| | 2 | 456 | | vec.RotateInPlace(cos, sin); |
| | 2 | 457 | | return vec; |
| | 2 | 458 | | } |
| | | 459 | | |
| | | 460 | | /// <summary> |
| | | 461 | | /// Rotates this vector using another vector representing the cosine and sine of the rotation angle. |
| | | 462 | | /// </summary> |
| | | 463 | | /// <param name="rotation">The vector containing the cosine and sine values for rotation.</param> |
| | | 464 | | /// <returns>A new vector representing the result of the rotation.</returns> |
| | | 465 | | public Vector2d Rotated(Vector2d rotation) |
| | 1 | 466 | | { |
| | 1 | 467 | | return Rotated(rotation.x, rotation.y); |
| | 1 | 468 | | } |
| | | 469 | | |
| | | 470 | | /// <summary> |
| | | 471 | | /// Rotates this vector in the inverse direction using cosine and sine values. |
| | | 472 | | /// </summary> |
| | | 473 | | /// <param name="cos">The cosine of the rotation angle.</param> |
| | | 474 | | /// <param name="sin">The sine of the rotation angle.</param> |
| | | 475 | | public void RotateInverse(Fixed64 cos, Fixed64 sin) |
| | 1 | 476 | | { |
| | 1 | 477 | | RotateInPlace(cos, -sin); |
| | 1 | 478 | | } |
| | | 479 | | |
| | | 480 | | /// <summary> |
| | | 481 | | /// Rotates this vector 90 degrees to the right (clockwise). |
| | | 482 | | /// </summary> |
| | | 483 | | public Vector2d RotateRightInPlace() |
| | 1 | 484 | | { |
| | 1 | 485 | | Fixed64 temp1 = x; |
| | 1 | 486 | | x = y; |
| | 1 | 487 | | y = -temp1; |
| | 1 | 488 | | return this; |
| | 1 | 489 | | } |
| | | 490 | | |
| | | 491 | | /// <summary> |
| | | 492 | | /// Rotates this vector 90 degrees to the left (counterclockwise). |
| | | 493 | | /// </summary> |
| | | 494 | | public Vector2d RotateLeftInPlace() |
| | 1 | 495 | | { |
| | 1 | 496 | | Fixed64 temp1 = x; |
| | 1 | 497 | | x = -y; |
| | 1 | 498 | | y = temp1; |
| | 1 | 499 | | return this; |
| | 1 | 500 | | } |
| | | 501 | | |
| | | 502 | | /// <summary> |
| | | 503 | | /// Reflects this vector across the specified axis vector. |
| | | 504 | | /// </summary> |
| | | 505 | | public Vector2d ReflectInPlace(Vector2d axis) |
| | 1 | 506 | | { |
| | 1 | 507 | | return ReflectInPlace(axis.x, axis.y); |
| | 1 | 508 | | } |
| | | 509 | | |
| | | 510 | | /// <summary> |
| | | 511 | | /// Reflects this vector across the specified x and y axis. |
| | | 512 | | /// </summary> |
| | | 513 | | public Vector2d ReflectInPlace(Fixed64 axisX, Fixed64 axisY) |
| | 4 | 514 | | { |
| | 4 | 515 | | Fixed64 projection = Dot(axisX, axisY); |
| | 4 | 516 | | return ReflectInPlace(axisX, axisY, projection); |
| | 4 | 517 | | } |
| | | 518 | | |
| | | 519 | | /// <summary> |
| | | 520 | | /// Reflects this vector across the specified axis using the provided projection of this vector onto the axis. |
| | | 521 | | /// </summary> |
| | | 522 | | /// /// <param name="axisX">The x component of the axis to reflect across.</param> |
| | | 523 | | /// <param name="axisY">The y component of the axis to reflect across.</param> |
| | | 524 | | /// <param name="projection">The precomputed projection of this vector onto the reflection axis.</param> |
| | | 525 | | public Vector2d ReflectInPlace(Fixed64 axisX, Fixed64 axisY, Fixed64 projection) |
| | 5 | 526 | | { |
| | 5 | 527 | | Fixed64 temp1 = axisX * projection; |
| | 5 | 528 | | Fixed64 temp2 = axisY * projection; |
| | 5 | 529 | | x = temp1 + temp1 - x; |
| | 5 | 530 | | y = temp2 + temp2 - y; |
| | 5 | 531 | | return this; |
| | 5 | 532 | | } |
| | | 533 | | |
| | | 534 | | /// <summary> |
| | | 535 | | /// Reflects this vector across the specified x and y axis. |
| | | 536 | | /// </summary> |
| | | 537 | | /// <returns>A new vector representing the result of the reflection.</returns> |
| | | 538 | | public Vector2d Reflected(Fixed64 axisX, Fixed64 axisY) |
| | 2 | 539 | | { |
| | 2 | 540 | | Vector2d vec = this; |
| | 2 | 541 | | vec.ReflectInPlace(axisX, axisY); |
| | 2 | 542 | | return vec; |
| | 2 | 543 | | } |
| | | 544 | | |
| | | 545 | | /// <summary> |
| | | 546 | | /// Reflects this vector across the specified axis vector. |
| | | 547 | | /// </summary> |
| | | 548 | | /// <returns>A new vector representing the result of the reflection.</returns> |
| | | 549 | | public Vector2d Reflected(Vector2d axis) |
| | 1 | 550 | | { |
| | 1 | 551 | | return Reflected(axis.x, axis.y); |
| | 1 | 552 | | } |
| | | 553 | | |
| | | 554 | | /// <summary> |
| | | 555 | | /// Returns the dot product of this vector with another vector. |
| | | 556 | | /// </summary> |
| | | 557 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 558 | | public Fixed64 Dot(Fixed64 otherX, Fixed64 otherY) |
| | 7 | 559 | | { |
| | 7 | 560 | | return x * otherX + y * otherY; |
| | 7 | 561 | | } |
| | | 562 | | |
| | | 563 | | /// <summary> |
| | | 564 | | /// Returns the dot product of this vector with another vector. |
| | | 565 | | /// </summary> |
| | | 566 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 567 | | public Fixed64 Dot(Vector2d other) |
| | 2 | 568 | | { |
| | 2 | 569 | | return Dot(other.x, other.y); |
| | 2 | 570 | | } |
| | | 571 | | |
| | | 572 | | /// <summary> |
| | | 573 | | /// Computes the cross product magnitude of this vector with another vector. |
| | | 574 | | /// </summary> |
| | | 575 | | /// <param name="otherX">The X component of the other vector.</param> |
| | | 576 | | /// <param name="otherY">The Y component of the other vector.</param> |
| | | 577 | | /// <returns>The cross product magnitude.</returns> |
| | | 578 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 579 | | public Fixed64 CrossProduct(Fixed64 otherX, Fixed64 otherY) |
| | 1 | 580 | | { |
| | 1 | 581 | | return x * otherY - y * otherX; |
| | 1 | 582 | | } |
| | | 583 | | |
| | | 584 | | /// <inheritdoc cref="CrossProduct(Fixed64, Fixed64)"/> |
| | | 585 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 586 | | public Fixed64 CrossProduct(Vector2d other) |
| | 1 | 587 | | { |
| | 1 | 588 | | return CrossProduct(other.x, other.y); |
| | 1 | 589 | | } |
| | | 590 | | |
| | | 591 | | /// <summary> |
| | | 592 | | /// Returns the distance between this vector and another vector specified by its components. |
| | | 593 | | /// </summary> |
| | | 594 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 595 | | public Fixed64 Distance(Fixed64 otherX, Fixed64 otherY) |
| | 2 | 596 | | { |
| | 2 | 597 | | Fixed64 temp1 = x - otherX; |
| | 2 | 598 | | temp1 *= temp1; |
| | 2 | 599 | | Fixed64 temp2 = y - otherY; |
| | 2 | 600 | | temp2 *= temp2; |
| | 2 | 601 | | return FixedMath.Sqrt(temp1 + temp2); |
| | 2 | 602 | | } |
| | | 603 | | |
| | | 604 | | /// <summary> |
| | | 605 | | /// Returns the distance between this vector and another vector. |
| | | 606 | | /// </summary> |
| | | 607 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 608 | | public Fixed64 Distance(Vector2d other) |
| | 2 | 609 | | { |
| | 2 | 610 | | return Distance(other.x, other.y); |
| | 2 | 611 | | } |
| | | 612 | | |
| | | 613 | | /// <summary> |
| | | 614 | | /// Calculates the squared distance between two vectors, avoiding the need for a square root operation. |
| | | 615 | | /// </summary> |
| | | 616 | | /// <returns>The squared distance between the two vectors.</returns> |
| | | 617 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 618 | | public Fixed64 SqrDistance(Fixed64 otherX, Fixed64 otherY) |
| | 3 | 619 | | { |
| | 3 | 620 | | Fixed64 temp1 = x - otherX; |
| | 3 | 621 | | temp1 *= temp1; |
| | 3 | 622 | | Fixed64 temp2 = y - otherY; |
| | 3 | 623 | | temp2 *= temp2; |
| | 3 | 624 | | return temp1 + temp2; |
| | 3 | 625 | | } |
| | | 626 | | |
| | | 627 | | /// <summary> |
| | | 628 | | /// Calculates the squared distance between two vectors, avoiding the need for a square root operation. |
| | | 629 | | /// </summary> |
| | | 630 | | /// <returns>The squared distance between the two vectors.</returns> |
| | | 631 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 632 | | public Fixed64 SqrDistance(Vector2d other) |
| | 3 | 633 | | { |
| | 3 | 634 | | return SqrDistance(other.x, other.y); |
| | 3 | 635 | | } |
| | | 636 | | |
| | | 637 | | #endregion |
| | | 638 | | |
| | | 639 | | #region Vector2d Operations |
| | | 640 | | |
| | | 641 | | /// <summary> |
| | | 642 | | /// Normalizes the given vector, returning a unit vector with the same direction. |
| | | 643 | | /// </summary> |
| | | 644 | | /// <param name="value">The vector to normalize.</param> |
| | | 645 | | /// <returns>A normalized (unit) vector with the same direction.</returns> |
| | | 646 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 647 | | public static Vector2d GetNormalized(Vector2d value) |
| | 5 | 648 | | { |
| | 5 | 649 | | Fixed64 mag = GetMagnitude(value); |
| | | 650 | | |
| | 5 | 651 | | if (mag == Fixed64.Zero) |
| | 1 | 652 | | return new Vector2d(Fixed64.Zero, Fixed64.Zero); |
| | | 653 | | |
| | | 654 | | // If already normalized, return as-is |
| | 4 | 655 | | if (mag == Fixed64.One) |
| | 1 | 656 | | return value; |
| | | 657 | | |
| | | 658 | | // Normalize it exactly |
| | 3 | 659 | | return new Vector2d( |
| | 3 | 660 | | value.x / mag, |
| | 3 | 661 | | value.y / mag |
| | 3 | 662 | | ); |
| | 5 | 663 | | } |
| | | 664 | | |
| | | 665 | | /// <summary> |
| | | 666 | | /// Returns the magnitude (length) of the given vector. |
| | | 667 | | /// </summary> |
| | | 668 | | /// <param name="vector">The vector to compute the magnitude of.</param> |
| | | 669 | | /// <returns>The magnitude (length) of the vector.</returns> |
| | | 670 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 671 | | public static Fixed64 GetMagnitude(Vector2d vector) |
| | 24 | 672 | | { |
| | 24 | 673 | | Fixed64 mag = (vector.x * vector.x) + (vector.y * vector.y); |
| | | 674 | | |
| | | 675 | | // If rounding error pushed magnitude slightly above 1, clamp it |
| | 24 | 676 | | if (mag > Fixed64.One && mag <= Fixed64.One + Fixed64.Epsilon) |
| | 3 | 677 | | return Fixed64.One; |
| | | 678 | | |
| | 21 | 679 | | return mag.Abs() > Fixed64.Zero ? FixedMath.Sqrt(mag) : Fixed64.Zero; |
| | 24 | 680 | | } |
| | | 681 | | |
| | | 682 | | /// <summary> |
| | | 683 | | /// Returns a new <see cref="Vector2d"/> where each component is the absolute value of the corresponding input compo |
| | | 684 | | /// </summary> |
| | | 685 | | /// <param name="value">The input vector.</param> |
| | | 686 | | /// <returns>A vector with absolute values for each component.</returns> |
| | | 687 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 688 | | public static Vector2d Abs(Vector2d value) |
| | 1 | 689 | | { |
| | 1 | 690 | | return new Vector2d(value.x.Abs(), value.y.Abs()); |
| | 1 | 691 | | } |
| | | 692 | | |
| | | 693 | | /// <summary> |
| | | 694 | | /// Returns a new <see cref="Vector2d"/> where each component is the sign of the corresponding input component. |
| | | 695 | | /// </summary> |
| | | 696 | | /// <param name="value">The input vector.</param> |
| | | 697 | | /// <returns>A vector where each component is -1, 0, or 1 based on the sign of the input.</returns> |
| | | 698 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 699 | | public static Vector2d Sign(Vector2d value) |
| | 1 | 700 | | { |
| | 1 | 701 | | return new Vector2d(value.x.Sign(), value.y.Sign()); |
| | 1 | 702 | | } |
| | | 703 | | |
| | | 704 | | /// <summary> |
| | | 705 | | /// Creates a vector from a given angle in radians. |
| | | 706 | | /// </summary> |
| | | 707 | | public static Vector2d CreateRotation(Fixed64 angle) |
| | 1 | 708 | | { |
| | 1 | 709 | | return new Vector2d(FixedMath.Cos(angle), FixedMath.Sin(angle)); |
| | 1 | 710 | | } |
| | | 711 | | |
| | | 712 | | /// <summary> |
| | | 713 | | /// Computes the distance between two vectors using the Euclidean distance formula. |
| | | 714 | | /// </summary> |
| | | 715 | | /// <param name="start">The starting vector.</param> |
| | | 716 | | /// <param name="end">The ending vector.</param> |
| | | 717 | | /// <returns>The Euclidean distance between the two vectors.</returns> |
| | | 718 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 719 | | public static Fixed64 Distance(Vector2d start, Vector2d end) |
| | 1 | 720 | | { |
| | 1 | 721 | | return start.Distance(end); |
| | 1 | 722 | | } |
| | | 723 | | |
| | | 724 | | /// <summary> |
| | | 725 | | /// Calculates the squared distance between two vectors, avoiding the need for a square root operation. |
| | | 726 | | /// </summary> |
| | | 727 | | /// <returns>The squared distance between the two vectors.</returns> |
| | | 728 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 729 | | public static Fixed64 SqrDistance(Vector2d start, Vector2d end) |
| | 1 | 730 | | { |
| | 1 | 731 | | return start.SqrDistance(end); |
| | 1 | 732 | | } |
| | | 733 | | |
| | | 734 | | /// <summary> |
| | | 735 | | /// Calculates the forward direction vector in 2D based on a yaw (angle). |
| | | 736 | | /// </summary> |
| | | 737 | | /// <param name="angle">The angle in radians representing the rotation in 2D space.</param> |
| | | 738 | | /// <returns>A unit vector representing the forward direction.</returns> |
| | | 739 | | public static Vector2d ForwardDirection(Fixed64 angle) |
| | 1 | 740 | | { |
| | 1 | 741 | | Fixed64 x = FixedMath.Cos(angle); // Forward in the x-direction |
| | 1 | 742 | | Fixed64 y = FixedMath.Sin(angle); // Forward in the y-direction |
| | 1 | 743 | | return new Vector2d(x, y); |
| | 1 | 744 | | } |
| | | 745 | | |
| | | 746 | | /// <summary> |
| | | 747 | | /// Dot Product of two vectors. |
| | | 748 | | /// </summary> |
| | | 749 | | /// <param name="lhs"></param> |
| | | 750 | | /// <param name="rhs"></param> |
| | | 751 | | /// <returns></returns> |
| | | 752 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 753 | | public static Fixed64 Dot(Vector2d lhs, Vector2d rhs) |
| | 1 | 754 | | { |
| | 1 | 755 | | return lhs.Dot(rhs.x, rhs.y); |
| | 1 | 756 | | } |
| | | 757 | | |
| | | 758 | | /// <summary> |
| | | 759 | | /// Multiplies two vectors component-wise. |
| | | 760 | | /// </summary> |
| | | 761 | | /// <param name="a"></param> |
| | | 762 | | /// <param name="b"></param> |
| | | 763 | | /// <returns></returns> |
| | | 764 | | public static Vector2d Scale(Vector2d a, Vector2d b) |
| | 1 | 765 | | { |
| | 1 | 766 | | return new Vector2d(a.x * b.x, a.y * b.y); |
| | 1 | 767 | | } |
| | | 768 | | |
| | | 769 | | /// <summary> |
| | | 770 | | /// Cross Product of two vectors. |
| | | 771 | | /// </summary> |
| | | 772 | | /// <param name="lhs"></param> |
| | | 773 | | /// <param name="rhs"></param> |
| | | 774 | | /// <returns></returns> |
| | | 775 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 776 | | public static Fixed64 CrossProduct(Vector2d lhs, Vector2d rhs) |
| | 0 | 777 | | { |
| | 0 | 778 | | return lhs.CrossProduct(rhs); |
| | 0 | 779 | | } |
| | | 780 | | |
| | | 781 | | /// <summary> |
| | | 782 | | /// Rotates this vector by the specified angle (in radians). |
| | | 783 | | /// </summary> |
| | | 784 | | /// <param name="vec">The vector to rotate.</param> |
| | | 785 | | /// <param name="angleInRadians">The angle in radians.</param> |
| | | 786 | | /// <returns>The rotated vector.</returns> |
| | | 787 | | public static Vector2d Rotate(Vector2d vec, Fixed64 angleInRadians) |
| | 1 | 788 | | { |
| | 1 | 789 | | Fixed64 cos = FixedMath.Cos(angleInRadians); |
| | 1 | 790 | | Fixed64 sin = FixedMath.Sin(angleInRadians); |
| | 1 | 791 | | return new Vector2d( |
| | 1 | 792 | | vec.x * cos - vec.y * sin, |
| | 1 | 793 | | vec.x * sin + vec.y * cos |
| | 1 | 794 | | ); |
| | 1 | 795 | | } |
| | | 796 | | |
| | | 797 | | #endregion |
| | | 798 | | |
| | | 799 | | #region Conversion |
| | | 800 | | |
| | | 801 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 802 | | public override string ToString() |
| | 19 | 803 | | { |
| | 19 | 804 | | return $"({Math.Round((double)x, 2)}, {Math.Round((double)y, 2)})"; |
| | 19 | 805 | | } |
| | | 806 | | |
| | | 807 | | /// <summary> |
| | | 808 | | /// Converts this <see cref="Vector2d"/> to a <see cref="Vector3d"/>, |
| | | 809 | | /// mapping the Y component of this vector to the Z axis in the resulting vector. |
| | | 810 | | /// </summary> |
| | | 811 | | /// <param name="z">The value to assign to the Y axis of the resulting <see cref="Vector3d"/>.</param> |
| | | 812 | | /// <returns> |
| | | 813 | | /// A new <see cref="Vector3d"/> where (X, Y) from this <see cref="Vector2d"/> |
| | | 814 | | /// become (X, Z) in the resulting vector, with the provided Z parameter assigned to Y. |
| | | 815 | | /// </returns> |
| | | 816 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 817 | | public readonly Vector3d ToVector3d(Fixed64 z) |
| | 1 | 818 | | { |
| | 1 | 819 | | return new Vector3d(x, z, y); |
| | 1 | 820 | | } |
| | | 821 | | |
| | | 822 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 823 | | public readonly void Deconstruct(out float x, out float y) |
| | 1 | 824 | | { |
| | 1 | 825 | | x = this.x.ToPreciseFloat(); |
| | 1 | 826 | | y = this.y.ToPreciseFloat(); |
| | 1 | 827 | | } |
| | | 828 | | |
| | | 829 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 830 | | public readonly void Deconstruct(out int x, out int y) |
| | 1 | 831 | | { |
| | 1 | 832 | | x = this.x.RoundToInt(); |
| | 1 | 833 | | y = this.y.RoundToInt(); |
| | 1 | 834 | | } |
| | | 835 | | |
| | | 836 | | /// <summary> |
| | | 837 | | /// Converts each component of the vector from radians to degrees. |
| | | 838 | | /// </summary> |
| | | 839 | | /// <param name="radians">The vector with components in radians.</param> |
| | | 840 | | /// <returns>A new vector with components converted to degrees.</returns> |
| | | 841 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 842 | | public static Vector2d ToDegrees(Vector2d radians) |
| | 1 | 843 | | { |
| | 1 | 844 | | return new Vector2d( |
| | 1 | 845 | | FixedMath.RadToDeg(radians.x), |
| | 1 | 846 | | FixedMath.RadToDeg(radians.y) |
| | 1 | 847 | | ); |
| | 1 | 848 | | } |
| | | 849 | | |
| | | 850 | | /// <summary> |
| | | 851 | | /// Converts each component of the vector from degrees to radians. |
| | | 852 | | /// </summary> |
| | | 853 | | /// <param name="degrees">The vector with components in degrees.</param> |
| | | 854 | | /// <returns>A new vector with components converted to radians.</returns> |
| | | 855 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 856 | | public static Vector2d ToRadians(Vector2d degrees) |
| | 1 | 857 | | { |
| | 1 | 858 | | return new Vector2d( |
| | 1 | 859 | | FixedMath.DegToRad(degrees.x), |
| | 1 | 860 | | FixedMath.DegToRad(degrees.y) |
| | 1 | 861 | | ); |
| | 1 | 862 | | } |
| | | 863 | | |
| | | 864 | | #endregion |
| | | 865 | | |
| | | 866 | | #region Operators |
| | | 867 | | |
| | | 868 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 869 | | public static Vector2d operator +(Vector2d v1, Vector2d v2) |
| | 1 | 870 | | { |
| | 1 | 871 | | return new Vector2d(v1.x + v2.x, v1.y + v2.y); |
| | 1 | 872 | | } |
| | | 873 | | |
| | | 874 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 875 | | public static Vector2d operator +(Vector2d v1, Fixed64 mag) |
| | 2 | 876 | | { |
| | 2 | 877 | | return new Vector2d(v1.x + mag, v1.y + mag); |
| | 2 | 878 | | } |
| | | 879 | | |
| | | 880 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 881 | | public static Vector2d operator +(Fixed64 mag, Vector2d v1) |
| | 1 | 882 | | { |
| | 1 | 883 | | return v1 + mag; |
| | 1 | 884 | | } |
| | | 885 | | |
| | | 886 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 887 | | public static Vector2d operator +(Vector2d v1, (int x, int y) v2) |
| | 2 | 888 | | { |
| | 2 | 889 | | return new Vector2d(v1.x + v2.x, v1.y + v2.y); |
| | 2 | 890 | | } |
| | | 891 | | |
| | | 892 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 893 | | public static Vector2d operator +((int x, int y) v2, Vector2d v1) |
| | 1 | 894 | | { |
| | 1 | 895 | | return v1 + v2; |
| | 1 | 896 | | } |
| | | 897 | | |
| | | 898 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 899 | | public static Vector2d operator +(Vector2d v1, (float x, float y) v2) |
| | 2 | 900 | | { |
| | 2 | 901 | | return new Vector2d(v1.x + v2.x, v1.y + v2.y); |
| | 2 | 902 | | } |
| | | 903 | | |
| | | 904 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 905 | | public static Vector2d operator +((float x, float y) v1, Vector2d v2) |
| | 1 | 906 | | { |
| | 1 | 907 | | return v2 + v1; |
| | 1 | 908 | | } |
| | | 909 | | |
| | | 910 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 911 | | public static Vector2d operator -(Vector2d v1, Vector2d v2) |
| | 5 | 912 | | { |
| | 5 | 913 | | return new Vector2d(v1.x - v2.x, v1.y - v2.y); |
| | 5 | 914 | | } |
| | | 915 | | |
| | | 916 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 917 | | public static Vector2d operator -(Vector2d v1, Fixed64 mag) |
| | 1 | 918 | | { |
| | 1 | 919 | | return new Vector2d(v1.x - mag, v1.y - mag); |
| | 1 | 920 | | } |
| | | 921 | | |
| | | 922 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 923 | | public static Vector2d operator -(Fixed64 mag, Vector2d v1) |
| | 1 | 924 | | { |
| | 1 | 925 | | return new Vector2d(mag - v1.x, mag - v1.y); |
| | 1 | 926 | | } |
| | | 927 | | |
| | | 928 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 929 | | public static Vector2d operator -(Vector2d v1, (int x, int y) v2) |
| | 1 | 930 | | { |
| | 1 | 931 | | return new Vector2d(v1.x - v2.x, v1.y - v2.y); |
| | 1 | 932 | | } |
| | | 933 | | |
| | | 934 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 935 | | public static Vector2d operator -((int x, int y) v1, Vector2d v2) |
| | 1 | 936 | | { |
| | 1 | 937 | | return new Vector2d(v1.x - v2.x, v1.y - v2.y); |
| | 1 | 938 | | } |
| | | 939 | | |
| | | 940 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 941 | | public static Vector2d operator -(Vector2d v1, (float x, float y) v2) |
| | 1 | 942 | | { |
| | 1 | 943 | | return new Vector2d(v1.x - v2.x, v1.y - v2.y); |
| | 1 | 944 | | } |
| | | 945 | | |
| | | 946 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 947 | | public static Vector2d operator -((float x, float y) v1, Vector2d v2) |
| | 1 | 948 | | { |
| | 1 | 949 | | return new Vector2d(v1.x - v2.x, v1.y - v2.y); |
| | 1 | 950 | | } |
| | | 951 | | |
| | | 952 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 953 | | public static Vector2d operator -(Vector2d v1) |
| | 1 | 954 | | { |
| | 1 | 955 | | return new Vector2d(v1.x * -Fixed64.One, v1.y * -Fixed64.One); |
| | 1 | 956 | | } |
| | | 957 | | |
| | | 958 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 959 | | public static Vector2d operator *(Vector2d v1, Fixed64 mag) |
| | 1 | 960 | | { |
| | 1 | 961 | | return new Vector2d(v1.x * mag, v1.y * mag); |
| | 1 | 962 | | } |
| | | 963 | | |
| | | 964 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 965 | | public static Vector2d operator *(Vector2d v1, Vector2d v2) |
| | 1 | 966 | | { |
| | 1 | 967 | | return new Vector2d(v1.x * v2.x, v1.y * v2.y); |
| | 1 | 968 | | } |
| | | 969 | | |
| | | 970 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 971 | | public static Vector2d operator /(Vector2d v1, Fixed64 div) |
| | 1 | 972 | | { |
| | 1 | 973 | | return new Vector2d(v1.x / div, v1.y / div); |
| | 1 | 974 | | } |
| | | 975 | | |
| | | 976 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 977 | | public static bool operator ==(Vector2d left, Vector2d right) |
| | 1 | 978 | | { |
| | 1 | 979 | | return left.Equals(right); |
| | 1 | 980 | | } |
| | | 981 | | |
| | | 982 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 983 | | public static bool operator !=(Vector2d left, Vector2d right) |
| | 1 | 984 | | { |
| | 1 | 985 | | return !left.Equals(right); |
| | 1 | 986 | | } |
| | | 987 | | |
| | | 988 | | #endregion |
| | | 989 | | |
| | | 990 | | #region Equality, HashCode, and Comparable Overrides |
| | | 991 | | |
| | | 992 | | /// <summary> |
| | | 993 | | /// Are all components of this vector equal to zero? |
| | | 994 | | /// </summary> |
| | | 995 | | /// <returns></returns> |
| | | 996 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 997 | | public bool EqualsZero() |
| | 2 | 998 | | { |
| | 2 | 999 | | return this.Equals(Zero); |
| | 2 | 1000 | | } |
| | | 1001 | | |
| | | 1002 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1003 | | public bool NotZero() |
| | 1 | 1004 | | { |
| | 1 | 1005 | | return !EqualsZero(); |
| | 1 | 1006 | | } |
| | | 1007 | | |
| | | 1008 | | /// <summary> |
| | | 1009 | | /// Checks whether all components are strictly greater than <see cref="Fixed64.Epsilon"/>. |
| | | 1010 | | /// </summary> |
| | | 1011 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1012 | | public bool AllComponentsGreaterThanEpsilon() |
| | 2 | 1013 | | { |
| | 2 | 1014 | | return x > Fixed64.Epsilon && y > Fixed64.Epsilon; |
| | 2 | 1015 | | } |
| | | 1016 | | |
| | | 1017 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1018 | | public override bool Equals(object? obj) |
| | 5 | 1019 | | { |
| | 5 | 1020 | | return obj is Vector2d other && Equals(other); |
| | 5 | 1021 | | } |
| | | 1022 | | |
| | | 1023 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1024 | | public bool Equals(Vector2d other) |
| | 72 | 1025 | | { |
| | 72 | 1026 | | return other.x == x && other.y == y; |
| | 72 | 1027 | | } |
| | | 1028 | | |
| | | 1029 | | public bool Equals(Vector2d x, Vector2d y) |
| | 2 | 1030 | | { |
| | 2 | 1031 | | return x.Equals(y); |
| | 2 | 1032 | | } |
| | | 1033 | | |
| | | 1034 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1035 | | public override int GetHashCode() |
| | 3 | 1036 | | { |
| | 3 | 1037 | | return StateHash; |
| | 3 | 1038 | | } |
| | | 1039 | | |
| | | 1040 | | public int GetHashCode(Vector2d obj) |
| | 1 | 1041 | | { |
| | 1 | 1042 | | return obj.GetHashCode(); |
| | 1 | 1043 | | } |
| | | 1044 | | |
| | | 1045 | | public int CompareTo(Vector2d other) |
| | 1 | 1046 | | { |
| | 1 | 1047 | | return SqrMagnitude.CompareTo(other.SqrMagnitude); |
| | 1 | 1048 | | } |
| | | 1049 | | |
| | | 1050 | | #endregion |
| | | 1051 | | } |