| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | |
| | | 3 | | namespace FixedMathSharp |
| | | 4 | | { |
| | | 5 | | public static partial class Vector2dExtensions |
| | | 6 | | { |
| | | 7 | | #region Vector2d Operations |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Clamps each component of the vector to the range [-1, 1] in place and returns the modified vector. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <param name="v">The vector to clamp.</param> |
| | | 13 | | /// <returns>The clamped vector with each component between -1 and 1.</returns> |
| | | 14 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 15 | | public static Vector2d ClampOneInPlace(this Vector2d v) |
| | 1 | 16 | | { |
| | 1 | 17 | | v.x = v.x.ClampOne(); |
| | 1 | 18 | | v.y = v.y.ClampOne(); |
| | 1 | 19 | | return v; |
| | 1 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Checks if the distance between two vectors is less than or equal to a specified factor. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="me">The current vector.</param> |
| | | 26 | | /// <param name="other">The vector to compare distance to.</param> |
| | | 27 | | /// <param name="factor">The maximum allowable distance.</param> |
| | | 28 | | /// <returns>True if the distance between the vectors is less than or equal to the factor, false otherwise.</ret |
| | | 29 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 30 | | public static bool CheckDistance(this Vector2d me, Vector2d other, Fixed64 factor) |
| | 1 | 31 | | { |
| | 1 | 32 | | var dis = Vector2d.Distance(me, other); |
| | 1 | 33 | | return dis <= factor; |
| | 1 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc cref="Vector2d.Rotate(Vector2d, Fixed64)" /> |
| | | 37 | | public static Vector2d Rotate(this Vector2d vec, Fixed64 angleInRadians) |
| | 1 | 38 | | { |
| | 1 | 39 | | return Vector2d.Rotate(vec, angleInRadians); |
| | 1 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc cref="Vector2d.Abs(Vector2d)" /> |
| | | 43 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 44 | | public static Vector2d Abs(this Vector2d value) |
| | 1 | 45 | | { |
| | 1 | 46 | | return Vector2d.Abs(value); |
| | 1 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc cref="Vector2d.Sign(Vector2d)" /> |
| | | 50 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 51 | | public static Vector2d Sign(this Vector2d value) |
| | 1 | 52 | | { |
| | 1 | 53 | | return Vector2d.Sign(value); |
| | 1 | 54 | | } |
| | | 55 | | |
| | | 56 | | #endregion |
| | | 57 | | |
| | | 58 | | #region Conversion |
| | | 59 | | |
| | | 60 | | /// <inheritdoc cref="Vector2d.ToDegrees(Vector2d)" /> |
| | | 61 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 62 | | public static Vector2d ToDegrees(this Vector2d radians) |
| | 1 | 63 | | { |
| | 1 | 64 | | return Vector2d.ToDegrees(radians); |
| | 1 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <inheritdoc cref="Vector2d.ToRadians(Vector2d)" /> |
| | | 68 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 69 | | public static Vector2d ToRadians(this Vector2d degrees) |
| | 1 | 70 | | { |
| | 1 | 71 | | return Vector2d.ToRadians(degrees); |
| | 1 | 72 | | } |
| | | 73 | | |
| | | 74 | | #endregion |
| | | 75 | | |
| | | 76 | | #region Equality |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Compares two vectors for approximate equality, allowing a fixed absolute difference. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="me">The current vector.</param> |
| | | 82 | | /// <param name="other">The vector to compare against.</param> |
| | | 83 | | /// <param name="allowedDifference">The allowed absolute difference between each component.</param> |
| | | 84 | | /// <returns>True if the components are within the allowed difference, false otherwise.</returns> |
| | | 85 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 86 | | public static bool FuzzyEqualAbsolute(this Vector2d me, Vector2d other, Fixed64 allowedDifference) |
| | 1 | 87 | | { |
| | 1 | 88 | | return (me.x - other.x).Abs() <= allowedDifference && |
| | 1 | 89 | | (me.y - other.y).Abs() <= allowedDifference; |
| | 1 | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Compares two vectors for approximate equality, allowing a fractional difference (percentage). |
| | | 94 | | /// Handles zero components by only using the allowed percentage difference. |
| | | 95 | | /// </summary> |
| | | 96 | | /// <param name="me">The current vector.</param> |
| | | 97 | | /// <param name="other">The vector to compare against.</param> |
| | | 98 | | /// <param name="percentage">The allowed fractional difference (percentage) for each component.</param> |
| | | 99 | | /// <returns>True if the components are within the allowed percentage difference, false otherwise.</returns> |
| | | 100 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 101 | | public static bool FuzzyEqual(this Vector2d me, Vector2d other, Fixed64? percentage = null) |
| | 13 | 102 | | { |
| | 13 | 103 | | Fixed64 p = percentage ?? Fixed64.Epsilon; |
| | 13 | 104 | | return me.x.FuzzyComponentEqual(other.x, p) && me.y.FuzzyComponentEqual(other.y, p); |
| | 13 | 105 | | } |
| | | 106 | | |
| | | 107 | | #endregion |
| | | 108 | | } |
| | | 109 | | } |