| | 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 | |
|
| | 7 | | using System.Runtime.CompilerServices; |
| | 8 | | using Unity.Mathematics; |
| | 9 | | using UnityEngine; |
| | 10 | |
|
| | 11 | | namespace 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) |
| 4 | 29 | | { |
| 4 | 30 | | float moddedEpsilon = Mathf.Epsilon * 8f; |
| | 31 | | const float k_BaseMultiplier = 1E-06f; |
| | 32 | |
|
| 4 | 33 | | 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); |
| 4 | 39 | | } |
| | 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) |
| 2 | 48 | | { |
| 2 | 49 | | if (targetVector2.x == 0f) |
| 1 | 50 | | { |
| 1 | 51 | | return 0f; |
| | 52 | | } |
| | 53 | |
|
| 1 | 54 | | return targetVector2.y / targetVector2.x; |
| 2 | 55 | | } |
| | 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) |
| 1 | 65 | | { |
| | 66 | | const float k_HalfMultiplier = 0.5f; |
| 1 | 67 | | return new Vector2( |
| | 68 | | targetVector2.x + (otherVector2.x - targetVector2.x) * k_HalfMultiplier, |
| | 69 | | targetVector2.y + (otherVector2.y - targetVector2.y) * k_HalfMultiplier |
| | 70 | | ); |
| 1 | 71 | | } |
| | 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) |
| 2 | 85 | | { |
| | 86 | | // We found nothing to compare against |
| 2 | 87 | | if (otherVector2 == null || otherVector2.Length == 0) |
| 1 | 88 | | { |
| 1 | 89 | | return -1; |
| | 90 | | } |
| | 91 | |
|
| 1 | 92 | | float closestSquareMagnitude = float.PositiveInfinity; |
| 1 | 93 | | int closestIndex = -1; |
| 1 | 94 | | int otherVector2Length = otherVector2.Length; |
| | 95 | |
|
| | 96 | | // Loop through the provided points and figure out what is closest (close enough). |
| 12 | 97 | | for (int i = 0; i < otherVector2Length; i++) |
| 5 | 98 | | { |
| 5 | 99 | | float squareDistance = (otherVector2[i] - targetVector2).sqrMagnitude; |
| 5 | 100 | | if (float.IsNaN(squareDistance) || !(squareDistance < closestSquareMagnitude)) |
| 3 | 101 | | { |
| 3 | 102 | | continue; |
| | 103 | | } |
| | 104 | |
|
| 2 | 105 | | closestSquareMagnitude = squareDistance; |
| 2 | 106 | | closestIndex = i; |
| 2 | 107 | | } |
| | 108 | |
|
| 1 | 109 | | return closestIndex; |
| 2 | 110 | | } |
| | 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) |
| 5 | 121 | | { |
| | 122 | | // Find split points |
| 5 | 123 | | int splitIndex = targetString.IndexOf(',', 0); |
| 5 | 124 | | if (splitIndex == -1) |
| 1 | 125 | | { |
| 1 | 126 | | outputVector2 = Vector2.zero; |
| 1 | 127 | | return false; |
| | 128 | | } |
| | 129 | |
|
| | 130 | | // Get source parts |
| 4 | 131 | | string sourceX = targetString.Substring(0, splitIndex); |
| 4 | 132 | | string sourceY = targetString.Substring(splitIndex + 1); |
| | 133 | |
|
| | 134 | | // Parse components |
| 4 | 135 | | if (!float.TryParse(sourceX, out float parsedX)) |
| 1 | 136 | | { |
| 1 | 137 | | outputVector2 = Vector2.zero; |
| 1 | 138 | | return false; |
| | 139 | | } |
| | 140 | |
|
| 3 | 141 | | if (!float.TryParse(sourceY, out float parsedY)) |
| 1 | 142 | | { |
| 1 | 143 | | outputVector2 = Vector2.zero; |
| 1 | 144 | | return false; |
| | 145 | | } |
| | 146 | |
|
| | 147 | | // Everything looks good, assign the values |
| 2 | 148 | | outputVector2 = new Vector2(parsedX, parsedY); |
| 2 | 149 | | return true; |
| 5 | 150 | | } |
| | 151 | | } |
| | 152 | | } |
| | 153 | | #endif // !UNITY_DOTSRUNTIME |