< Summary

Class:GDX.Vector2Extensions
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Vector2Extensions.cs
Covered lines:50
Uncovered lines:0
Coverable lines:50
Total lines:153
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%2.022083.33%
Midpoint(...)0%110100%
NearestIndex(...)0%6.046090%
TryParseVector2(...)0%5.194057.89%

File(s)

./Packages/com.dotbunny.gdx/GDX/Vector2Extensions.cs

#LineLine coverage
 1// Copyright (c) 2020-2024 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 Unity.Mathematics;
 9using UnityEngine;
 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            }
 53
 154            return targetVector2.y / targetVector2.x;
 255        }
 56
 57        /// <summary>
 58        ///     Get the midpoint between two <see cref="Vector2" />s.
 59        /// </summary>
 60        /// <param name="targetVector2">Point A</param>
 61        /// <param name="otherVector2">Point B</param>
 62        /// <returns>The midpoint between <paramref name="targetVector2" /> and <paramref name="otherVector2" />.</retur
 63        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 64        public static Vector2 Midpoint(this Vector2 targetVector2, Vector2 otherVector2)
 165        {
 66            const float k_HalfMultiplier = 0.5f;
 167            return new Vector2(
 68                targetVector2.x + (otherVector2.x - targetVector2.x) * k_HalfMultiplier,
 69                targetVector2.y + (otherVector2.y - targetVector2.y) * k_HalfMultiplier
 70            );
 171        }
 72
 73        /// <summary>
 74        ///     Find the index of the <see cref="Vector2" /> in <paramref name="otherVector2" /> that is nearest to the
 75        ///     <paramref name="targetVector2" />.
 76        /// </summary>
 77        /// <param name="targetVector2">The <see cref="Vector2" /> to use as the point of reference.</param>
 78        /// <param name="otherVector2">An array of <see cref="Vector2" /> positions to evaluate for which one is nearest
 79        /// <returns>
 80        ///     The index of the nearest <paramref name="otherVector2" /> element to <paramref name="targetVector2" />.
 81        ///     Returning -1 if the the <paramref name="otherVector2" /> has no elements or is null.
 82        /// </returns>
 83        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 84        public static int NearestIndex(this Vector2 targetVector2, Vector2[] otherVector2)
 285        {
 86            // We found nothing to compare against
 287            if (otherVector2 == null || otherVector2.Length == 0)
 188            {
 189                return -1;
 90            }
 91
 192            float closestSquareMagnitude = float.PositiveInfinity;
 193            int closestIndex = -1;
 194            int otherVector2Length = otherVector2.Length;
 95
 96            // Loop through the provided points and figure out what is closest (close enough).
 1297            for (int i = 0; i < otherVector2Length; i++)
 598            {
 599                float squareDistance = (otherVector2[i] - targetVector2).sqrMagnitude;
 5100                if (float.IsNaN(squareDistance) || !(squareDistance < closestSquareMagnitude))
 3101                {
 3102                    continue;
 103                }
 104
 2105                closestSquareMagnitude = squareDistance;
 2106                closestIndex = i;
 2107            }
 108
 1109            return closestIndex;
 2110        }
 111
 112        /// <summary>
 113        ///     Attempt to parse a <see cref="string" /> into a <see cref="Vector2" />.
 114        /// </summary>
 115        /// <remarks>This isn't great for runtime performance, it should be used predominantly when reconstructing data.
 116        /// <param name="targetString">The <see cref="string" /> to convert into a <see cref="Vector2" /> if possible.</
 117        /// <param name="outputVector2">The outputted <see cref="Vector2" />.</param>
 118        /// <returns>true/false if the conversion was successful.</returns>
 119        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 120        public static bool TryParseVector2(this string targetString, out Vector2 outputVector2)
 5121        {
 122            // Find split points
 5123            int splitIndex = targetString.IndexOf(',', 0);
 5124            if (splitIndex == -1)
 1125            {
 1126                outputVector2 = Vector2.zero;
 1127                return false;
 128            }
 129
 130            // Get source parts
 4131            string sourceX = targetString.Substring(0, splitIndex);
 4132            string sourceY = targetString.Substring(splitIndex + 1);
 133
 134            // Parse components
 4135            if (!float.TryParse(sourceX, out float parsedX))
 1136            {
 1137                outputVector2 = Vector2.zero;
 1138                return false;
 139            }
 140
 3141            if (!float.TryParse(sourceY, out float parsedY))
 1142            {
 1143                outputVector2 = Vector2.zero;
 1144                return false;
 145            }
 146
 147            // Everything looks good, assign the values
 2148            outputVector2 = new Vector2(parsedX, parsedY);
 2149            return true;
 5150        }
 151    }
 152}
 153#endif // !UNITY_DOTSRUNTIME

Coverage by test methods















Methods/Properties

Approximately(UnityEngine.Vector2, UnityEngine.Vector2)
Slope(UnityEngine.Vector2)
Midpoint(UnityEngine.Vector2, UnityEngine.Vector2)
NearestIndex(UnityEngine.Vector2, UnityEngine.Vector2[])
TryParseVector2(System.String, UnityEngine.Vector2&)