| | | 1 | | //======================================================================= |
| | | 2 | | // Vector2d.Extensions.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | |
| | | 10 | | namespace FixedMathSharp; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Provides extension methods for the Vector2d type to support additional vector operations, comparisons, and conversio |
| | | 14 | | /// </summary> |
| | | 15 | | public static partial class Vector2dExtensions |
| | | 16 | | { |
| | | 17 | | #region Vector2d Operations |
| | | 18 | | |
| | | 19 | | /// <inheritdoc cref="Vector2d.Clamp(Vector2d, Vector2d, Vector2d)" /> |
| | | 20 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 21 | | public static Vector2d Clamp(this Vector2d value, Vector2d min, Vector2d max) => Vector2d.Clamp(value, min, max); |
| | | 22 | | |
| | | 23 | | /// <inheritdoc cref="Vector2d.Clamp(Vector2d, Vector2d, Vector2d)" /> |
| | | 24 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 25 | | public static Vector2d ClampOne(this Vector2d value) => Vector2d.Clamp(value, Vector2d.Negative, Vector2d.One); |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Checks if the distance between two vectors is less than or equal to a specified factor. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="me">The current vector.</param> |
| | | 31 | | /// <param name="other">The vector to compare distance to.</param> |
| | | 32 | | /// <param name="factor">The maximum allowable distance.</param> |
| | | 33 | | /// <returns>True if the distance between the vectors is less than or equal to the factor, false otherwise.</returns |
| | | 34 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 35 | | public static bool CheckDistance(this Vector2d me, Vector2d other, Fixed64 factor) |
| | | 36 | | { |
| | 2 | 37 | | if (factor < Fixed64.Zero) |
| | 1 | 38 | | return false; |
| | | 39 | | |
| | 1 | 40 | | return Vector2d.DistanceSquared(me, other) <= factor * factor; |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <inheritdoc cref="Vector2d.Rotate(Vector2d, Fixed64)" /> |
| | | 44 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 45 | | public static Vector2d Rotate(this Vector2d vec, Fixed64 angleInRadians) => Vector2d.Rotate(vec, angleInRadians); |
| | | 46 | | |
| | | 47 | | /// <inheritdoc cref="Vector2d.Abs(Vector2d)" /> |
| | | 48 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 49 | | public static Vector2d Abs(this Vector2d value) => Vector2d.Abs(value); |
| | | 50 | | |
| | | 51 | | /// <inheritdoc cref="Vector2d.Sign(Vector2d)" /> |
| | | 52 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 53 | | public static Vector2d Sign(this Vector2d value) => Vector2d.Sign(value); |
| | | 54 | | |
| | | 55 | | #endregion |
| | | 56 | | |
| | | 57 | | #region Conversion |
| | | 58 | | |
| | | 59 | | /// <inheritdoc cref="Vector2d.ToDegrees(Vector2d)" /> |
| | | 60 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 61 | | public static Vector2d ToDegrees(this Vector2d radians) => Vector2d.ToDegrees(radians); |
| | | 62 | | |
| | | 63 | | /// <inheritdoc cref="Vector2d.ToRadians(Vector2d)" /> |
| | | 64 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 65 | | public static Vector2d ToRadians(this Vector2d degrees) => Vector2d.ToRadians(degrees); |
| | | 66 | | |
| | | 67 | | #endregion |
| | | 68 | | |
| | | 69 | | #region Equality |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Compares two vectors for approximate equality, allowing a fixed absolute difference. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <param name="me">The current vector.</param> |
| | | 75 | | /// <param name="other">The vector to compare against.</param> |
| | | 76 | | /// <param name="allowedDifference">The allowed absolute difference between each component.</param> |
| | | 77 | | /// <returns>True if the components are within the allowed difference, false otherwise.</returns> |
| | | 78 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 79 | | public static bool FuzzyEqualAbsolute(this Vector2d me, Vector2d other, Fixed64 allowedDifference) |
| | | 80 | | { |
| | 3 | 81 | | return (me.X - other.X).Abs() <= allowedDifference && |
| | 3 | 82 | | (me.Y - other.Y).Abs() <= allowedDifference; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Compares two vectors for approximate equality, allowing a fractional difference (percentage). |
| | | 87 | | /// Handles zero components by only using the allowed percentage difference. |
| | | 88 | | /// </summary> |
| | | 89 | | /// <param name="me">The current vector.</param> |
| | | 90 | | /// <param name="other">The vector to compare against.</param> |
| | | 91 | | /// <param name="percentage">The allowed fractional difference (percentage) for each component.</param> |
| | | 92 | | /// <returns>True if the components are within the allowed percentage difference, false otherwise.</returns> |
| | | 93 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 94 | | public static bool FuzzyEqual(this Vector2d me, Vector2d other, Fixed64? percentage = null) |
| | | 95 | | { |
| | 13 | 96 | | Fixed64 p = percentage ?? Fixed64.Epsilon; |
| | 13 | 97 | | return me.X.FuzzyComponentEqual(other.X, p) && me.Y.FuzzyComponentEqual(other.Y, p); |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | #endregion |
| | | 101 | | } |