|
5 | 5 | // Collected and expanded upon to by Freya Holmér (https://github.com/FreyaHolmer/Mathfs) |
6 | 6 |
|
7 | 7 | using System; |
| 8 | +using System.Collections.Generic; |
8 | 9 | using UnityEngine; |
9 | 10 | using Uei = UnityEngine.Internal; |
10 | 11 | using System.Linq; // used for arbitrary count min/max functions, so it's safe and won't allocate garbage don't worry~ |
@@ -1109,6 +1110,14 @@ public static Vector3 ClampMagnitude( Vector3 v, float min, float max ) { |
1109 | 1110 | /// <inheritdoc cref="DistanceSquared(Vector2,Vector2)"/> |
1110 | 1111 | [MethodImpl( INLINE )] public static float DistanceSquared( Vector4 a, Vector4 b ) => ( a.x - b.x ).Square() + ( a.y - b.y ).Square() + ( a.z - b.z ).Square() + ( a.w - b.w ).Square(); |
1111 | 1112 |
|
| 1113 | + /// <summary>The t-value (fraction) where a projected along b would be</summary> |
| 1114 | + /// <param name="a">The vector to project</param> |
| 1115 | + /// <param name="b">The vector to project onto</param> |
| 1116 | + public static float ProjectionTValue( Vector3 a, Vector3 b ) => Vector3.Dot( a, b ) / Vector3.Dot( b, b ); |
| 1117 | + |
| 1118 | + /// <inheritdoc cref="ProjectionTValue(Vector3,Vector3)"/> |
| 1119 | + public static float ProjectionTValue( Vector2 a, Vector2 b ) => Vector2.Dot( a, b ) / Vector2.Dot( b, b ); |
| 1120 | + |
1112 | 1121 | /// <summary>Calculates a rotation minimizing normal direction, given start and end conditions. This is usually used when evaluating rotation minimizing frames on curves.</summary> |
1113 | 1122 | /// <param name="posA">The start position</param> |
1114 | 1123 | /// <param name="tangentA">The start tangent direction</param> |
@@ -1306,9 +1315,15 @@ public static Pose Lerp( Pose a, Pose b, float t ) => |
1306 | 1315 | /// <summary>Returns the shortest angle between <c>a</c> and <c>b</c>, in the range 0 to tau/2 (0 to pi)</summary> |
1307 | 1316 | [MethodImpl( INLINE )] public static float AngleBetween( Vector2 a, Vector2 b ) => MathF.Acos( Vector2.Dot( a.normalized, b.normalized ).ClampNeg1to1() ); |
1308 | 1317 |
|
| 1318 | + /// <summary>Returns the shortest angle between two normalized vectors <c>a</c> and <c>b</c>, in the range 0 to tau/2 (0 to pi)</summary> |
| 1319 | + [MethodImpl( INLINE )] public static float AngleBetweenPreNormalized( Vector2 a, Vector2 b ) => MathF.Acos( Vector2.Dot( a, b ).ClampNeg1to1() ); |
| 1320 | + |
1309 | 1321 | /// <inheritdoc cref="AngleBetween(Vector2,Vector2)"/> |
1310 | 1322 | [MethodImpl( INLINE )] public static float AngleBetween( Vector3 a, Vector3 b ) => MathF.Acos( Vector3.Dot( a.normalized, b.normalized ).ClampNeg1to1() ); |
1311 | 1323 |
|
| 1324 | + /// <inheritdoc cref="AngleBetweenPreNormalized(Vector2,Vector2)"/> |
| 1325 | + [MethodImpl( INLINE )] public static float AngleBetweenPreNormalized( Vector3 a, Vector3 b ) => MathF.Acos( Vector3.Dot( a, b ).ClampNeg1to1() ); |
| 1326 | + |
1312 | 1327 | /// <summary>Returns the clockwise angle between <c>from</c> and <c>to</c>, in the range 0 to tau (0 to 2*pi)</summary> |
1313 | 1328 | [MethodImpl( INLINE )] public static float AngleFromToCW( Vector2 from, Vector2 to ) => Determinant( from, to ) < 0 ? AngleBetween( from, to ) : TAU - AngleBetween( from, to ); |
1314 | 1329 |
|
@@ -1341,6 +1356,25 @@ public static float InverseLerpAngle( float a, float b, float v ) { |
1341 | 1356 | return InverseLerpClamped( a, b, v ); |
1342 | 1357 | } |
1343 | 1358 |
|
| 1359 | + /// <summary>An enumerable sequence of <c>count</c> number of vectors |
| 1360 | + /// on a circle with the given radius, starting from the X axis</summary> |
| 1361 | + /// <param name="count">The number of vectors to arrange on the circle. |
| 1362 | + /// A negative count will enumerate in the negative direction</param> |
| 1363 | + /// <param name="radius">The radius of the circle</param> |
| 1364 | + public static IEnumerable<Vector2> PointsInCircle( int count, float radius = 1 ) { |
| 1365 | + if( count == 0 ) |
| 1366 | + yield break; |
| 1367 | + yield return new Vector2( radius, 0 ); |
| 1368 | + int absCount = Math.Abs( count ); |
| 1369 | + for( int i = 1; i < absCount; i++ ) { |
| 1370 | + float angle = ( TAU * i ) / count; |
| 1371 | + yield return new Vector2( |
| 1372 | + MathF.Cos( angle ) * radius, |
| 1373 | + MathF.Sin( angle ) * radius |
| 1374 | + ); |
| 1375 | + } |
| 1376 | + } |
| 1377 | + |
1344 | 1378 | #endregion |
1345 | 1379 |
|
1346 | 1380 | #region Angular movement helpers |
|
0 commit comments