< Summary

Class:GDX.Vector2Extensions
Assembly:GDX
File(s):D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/Vector2Extensions.cs
Covered lines:50
Uncovered lines:0
Coverable lines:50
Total lines:152
Line coverage:100% (50 of 50)
Covered branches:0
Total branches:0
Covered methods:5
Total methods:5
Method coverage:100% (5 of 5)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Approximately(...)0%220100%
Slope(...)0%220100%
Midpoint(...)0%110100%
NearestIndex(...)0%660100%
TryParseVector2(...)0%440100%

File(s)

D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/Vector2Extensions.cs

#LineLine coverage
 1// Copyright (c) 2020-2022 dotBunny Inc.
 2// dotBunny licenses this file to you under the BSL-1.0 license.
 3// See the LICENSE file in the project root for more information.
 4
 5#if !UNITY_DOTSRUNTIME
 6
 7using System.Runtime.CompilerServices;
 8using UnityEngine;
 9using Unity.Mathematics;
 10
 11namespace GDX
 12{
 13    /// <summary>
 14    ///     <see cref="Vector2" /> Based Extension Methods
 15    /// </summary>
 16    /// <exception cref="UnsupportedRuntimeException">Not supported on DOTS Runtime.</exception>
 17    [VisualScriptingCompatible(2)]
 18    public static class Vector2Extensions
 19    {
 20        /// <summary>
 21        ///     Is one <see cref="Vector2" /> approximately similar to another <see cref="Vector2" />?
 22        /// </summary>
 23        /// <remarks>Includes optimized Unity.Mathematics approach.</remarks>
 24        /// <param name="targetVector2">Point A</param>
 25        /// <param name="otherVector2">Point B</param>
 26        /// <returns>Are the two <see cref="Vector2" /> approximately the same?</returns>
 27        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 28        public static bool Approximately(this Vector2 targetVector2, Vector2 otherVector2)
 429        {
 430            float moddedEpsilon = Mathf.Epsilon * 8f;
 31            const float k_BaseMultiplier = 1E-06f;
 32
 433            return math.abs(otherVector2.x - targetVector2.x) < math.max(
 34                       k_BaseMultiplier * math.max(math.abs(targetVector2.x), math.abs(otherVector2.x)),
 35                       moddedEpsilon) &&
 36                   math.abs(otherVector2.y - targetVector2.y) < math.max(
 37                       k_BaseMultiplier * math.max(math.abs(targetVector2.y), math.abs(otherVector2.y)),
 38                       moddedEpsilon);
 439        }
 40
 41        /// <summary>
 42        /// Get the slope of a <see cref="Vector2"/>.
 43        /// </summary>
 44        /// <param name="targetVector2">The <see cref="Vector2"/> to evaluate.</param>
 45        /// <returns>The slope value.</returns>
 46        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 47        public static float Slope(this Vector2 targetVector2)
 248        {
 249            if (targetVector2.x == 0f)
 150            {
 151                return 0f;
 52            }
 153            return targetVector2.y / targetVector2.x;
 254        }
 55
 56        /// <summary>
 57        ///     Get the midpoint between two <see cref="Vector2" />s.
 58        /// </summary>
 59        /// <param name="targetVector2">Point A</param>
 60        /// <param name="otherVector2">Point B</param>
 61        /// <returns>The midpoint between <paramref name="targetVector2" /> and <paramref name="otherVector2" />.</retur
 62        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 63        public static Vector2 Midpoint(this Vector2 targetVector2, Vector2 otherVector2)
 164        {
 65            const float k_HalfMultiplier = 0.5f;
 166            return new Vector2(
 67                targetVector2.x + (otherVector2.x - targetVector2.x) * k_HalfMultiplier,
 68                targetVector2.y + (otherVector2.y - targetVector2.y) * k_HalfMultiplier
 69            );
 170        }
 71
 72        /// <summary>
 73        ///     Find the index of the <see cref="Vector2" /> in <paramref name="otherVector2" /> that is nearest to the
 74        ///     <paramref name="targetVector2" />.
 75        /// </summary>
 76        /// <param name="targetVector2">The <see cref="Vector2" /> to use as the point of reference.</param>
 77        /// <param name="otherVector2">An array of <see cref="Vector2" /> positions to evaluate for which one is nearest
 78        /// <returns>
 79        ///     The index of the nearest <paramref name="otherVector2" /> element to <paramref name="targetVector2" />.
 80        ///     Returning -1 if the the <paramref name="otherVector2" /> has no elements or is null.
 81        /// </returns>
 82        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 83        public static int NearestIndex(this Vector2 targetVector2, Vector2[] otherVector2)
 284        {
 85            // We found nothing to compare against
 286            if (otherVector2 == null || otherVector2.Length == 0)
 187            {
 188                return -1;
 89            }
 90
 191            float closestSquareMagnitude = float.PositiveInfinity;
 192            int closestIndex = -1;
 193            int otherVector2Length = otherVector2.Length;
 94
 95            // Loop through the provided points and figure out what is closest (close enough).
 1296            for (int i = 0; i < otherVector2Length; i++)
 597            {
 598                float squareDistance = (otherVector2[i] - targetVector2).sqrMagnitude;
 599                if (float.IsNaN(squareDistance) || !(squareDistance < closestSquareMagnitude))
 3100                {
 3101                    continue;
 102                }
 103
 2104                closestSquareMagnitude = squareDistance;
 2105                closestIndex = i;
 2106            }
 107
 1108            return closestIndex;
 2109        }
 110
 111         /// <summary>
 112        ///     Attempt to parse a <see cref="string" /> into a <see cref="Vector2" />.
 113        /// </summary>
 114        /// <remarks>This isn't great for runtime performance, it should be used predominantly when reconstructing data.
 115        /// <param name="targetString">The <see cref="string" /> to convert into a <see cref="Vector2" /> if possible.</
 116        /// <param name="outputVector2">The outputted <see cref="Vector2" />.</param>
 117        /// <returns>true/false if the conversion was successful.</returns>
 118        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 119        public static bool TryParseVector2(this string targetString, out Vector2 outputVector2)
 5120        {
 121            // Find split points
 5122            int splitIndex = targetString.IndexOf(',', 0);
 5123            if (splitIndex == -1)
 1124            {
 1125                outputVector2 = Vector2.zero;
 1126                return false;
 127            }
 128
 129            // Get source parts
 4130            string sourceX = targetString.Substring(0, splitIndex);
 4131            string sourceY = targetString.Substring(splitIndex + 1);
 132
 133            // Parse components
 4134            if (!float.TryParse(sourceX, out float parsedX))
 1135            {
 1136                outputVector2 = Vector2.zero;
 1137                return false;
 138            }
 139
 3140            if (!float.TryParse(sourceY, out float parsedY))
 1141            {
 1142                outputVector2 = Vector2.zero;
 1143                return false;
 144            }
 145
 146            // Everything looks good, assign the values
 2147            outputVector2 = new Vector2(parsedX, parsedY);
 2148            return true;
 5149        }
 150    }
 151}
 152#endif