< Summary

Information
Class: FixedMathSharp.Vector2dExtensions
Assembly: FixedMathSharp
File(s): /home/runner/work/FixedMathSharp/FixedMathSharp/src/FixedMathSharp/Numerics/Vectors/Vector2dExtensions.cs
Line coverage
100%
Covered lines: 15
Uncovered lines: 0
Coverable lines: 15
Total lines: 110
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Clamp(...)100%11100%
ClampOne(...)100%11100%
CheckDistance(...)100%22100%
Rotate(...)100%11100%
TryRotate(...)100%11100%
Abs(...)100%11100%
Sign(...)100%11100%
ToDegrees(...)100%11100%
ToRadians(...)100%11100%
FuzzyEqualAbsolute(...)100%22100%
FuzzyEqual(...)100%44100%

File(s)

/home/runner/work/FixedMathSharp/FixedMathSharp/src/FixedMathSharp/Numerics/Vectors/Vector2dExtensions.cs

#LineLine coverage
 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
 8using System.Runtime.CompilerServices;
 9using FixedMathSharp.Geometry;
 10
 11namespace FixedMathSharp;
 12
 13/// <summary>
 14/// Provides extension methods for the Vector2d type to support additional vector operations, comparisons, and conversio
 15/// </summary>
 16public static partial class Vector2dExtensions
 17{
 18    #region Vector2d Operations
 19
 20    /// <inheritdoc cref="Vector2d.Clamp(Vector2d, Vector2d, Vector2d)" />
 21    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 122    public static Vector2d Clamp(this Vector2d value, Vector2d min, Vector2d max) => Vector2d.Clamp(value, min, max);
 23
 24    /// <inheritdoc cref="Vector2d.Clamp(Vector2d, Vector2d, Vector2d)" />
 25    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 126    public static Vector2d ClampOne(this Vector2d value) => Vector2d.Clamp(value, Vector2d.Negative, Vector2d.One);
 27
 28    /// <summary>
 29    /// Checks if the distance between two vectors is less than or equal to a specified factor.
 30    /// </summary>
 31    /// <param name="me">The current vector.</param>
 32    /// <param name="other">The vector to compare distance to.</param>
 33    /// <param name="factor">The maximum allowable distance.</param>
 34    /// <returns>True if the distance between the vectors is less than or equal to the factor, false otherwise.</returns
 35    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 36    public static bool CheckDistance(this Vector2d me, Vector2d other, Fixed64 factor)
 37    {
 438        if (factor < Fixed64.Zero)
 139            return false;
 40
 341        return WideGeometry.CompareDistanceToRadiusSum(me, other, factor, Fixed64.Zero) <= 0;
 42    }
 43
 44    /// <inheritdoc cref="Vector2d.Rotate(Vector2d, Fixed64)" />
 45    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 146    public static Vector2d Rotate(this Vector2d vec, Fixed64 angleInRadians) => Vector2d.Rotate(vec, angleInRadians);
 47
 48    /// <inheritdoc cref="Vector2d.TryRotate(Vector2d, Fixed64, out Vector2d)" />
 49    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 50    public static bool TryRotate(
 51        this Vector2d vector,
 52        Fixed64 angleInRadians,
 53        out Vector2d result) =>
 154        Vector2d.TryRotate(vector, angleInRadians, out result);
 55
 56    /// <inheritdoc cref="Vector2d.Abs(Vector2d)" />
 57    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 158    public static Vector2d Abs(this Vector2d value) => Vector2d.Abs(value);
 59
 60    /// <inheritdoc cref="Vector2d.Sign(Vector2d)" />
 61    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 162    public static Vector2d Sign(this Vector2d value) => Vector2d.Sign(value);
 63
 64    #endregion
 65
 66    #region Conversion
 67
 68    /// <inheritdoc cref="Vector2d.ToDegrees(Vector2d)" />
 69    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 170    public static Vector2d ToDegrees(this Vector2d radians) => Vector2d.ToDegrees(radians);
 71
 72    /// <inheritdoc cref="Vector2d.ToRadians(Vector2d)" />
 73    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 174    public static Vector2d ToRadians(this Vector2d degrees) => Vector2d.ToRadians(degrees);
 75
 76    #endregion
 77
 78    #region Equality
 79
 80    /// <summary>
 81    /// Compares two vectors for approximate equality, allowing a fixed absolute difference.
 82    /// </summary>
 83    /// <param name="me">The current vector.</param>
 84    /// <param name="other">The vector to compare against.</param>
 85    /// <param name="allowedDifference">The allowed absolute difference between each component.</param>
 86    /// <returns>True if the components are within the allowed difference, false otherwise.</returns>
 87    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 88    public static bool FuzzyEqualAbsolute(this Vector2d me, Vector2d other, Fixed64 allowedDifference)
 89    {
 590        return (me.X - other.X).Abs() <= allowedDifference &&
 591               (me.Y - other.Y).Abs() <= allowedDifference;
 92    }
 93
 94    /// <summary>
 95    /// Compares two vectors for approximate equality, allowing a fractional difference (percentage).
 96    /// Handles zero components by only using the allowed percentage difference.
 97    /// </summary>
 98    /// <param name="me">The current vector.</param>
 99    /// <param name="other">The vector to compare against.</param>
 100    /// <param name="percentage">The allowed fractional difference (percentage) for each component.</param>
 101    /// <returns>True if the components are within the allowed percentage difference, false otherwise.</returns>
 102    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 103    public static bool FuzzyEqual(this Vector2d me, Vector2d other, Fixed64? percentage = null)
 104    {
 18105        Fixed64 p = percentage ?? Fixed64.Epsilon;
 18106        return me.X.FuzzyComponentEqual(other.X, p) && me.Y.FuzzyComponentEqual(other.Y, p);
 107    }
 108
 109    #endregion
 110}