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