Skip to content

Commit

Permalink
Updated vector distance method
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornado-Technology committed Aug 29, 2024
1 parent 52cd8d3 commit a6e4571
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 26 deletions.
6 changes: 3 additions & 3 deletions Hypercube.Mathematics/Vectors/Vector2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ public Vector2 WithY(float value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float DistanceSquared(Vector2 value)
{
return (this - value).LengthSquared;
return DistanceSquared(this, value);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float Distance(Vector2 value)
{
return MathF.Sqrt(DistanceSquared(value));
return Distance(this, value);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -495,7 +495,7 @@ public static float DistanceSquared(Vector2 valueA, Vector2 valueB)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Distance(Vector2 valueA, Vector2 valueB)
{
return MathF.Sqrt(DistanceSquared(valueA, valueB));
return (valueA - valueB).Length;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
6 changes: 3 additions & 3 deletions Hypercube.Mathematics/Vectors/Vector2i.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ public Vector2i WithY(int value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float DistanceSquared(Vector2i value)
{
return (this - value).LengthSquared;
return DistanceSquared(this, value);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float Distance(Vector2i value)
{
return MathF.Sqrt(DistanceSquared(this, value));
return Distance(this, value);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -388,7 +388,7 @@ public static float DistanceSquared(Vector2i valueA, Vector2i valueB)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Distance(Vector2i valueA, Vector2i valueB)
{
return MathF.Sqrt(DistanceSquared(valueA, valueB));
return (valueA - valueB).Length;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
18 changes: 15 additions & 3 deletions Hypercube.Mathematics/Vectors/Vector3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,13 @@ public Vector3 WithZ(float value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float DistanceSquared(Vector3 value)
{
return (this - value).LengthSquared;
return DistanceSquared(this, value);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float Distance(Vector3 value)
{
return MathF.Sqrt(DistanceSquared(value));
return Distance(this, value);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -379,6 +379,12 @@ public override string ToString()
return new Vector3(a.X + b, a.Y + b, a.Z + b);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator +(float a, Vector3 b)
{
return new Vector3(a + b.X, a + b.Y, a + b.Z);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator -(Vector3 a)
{
Expand All @@ -403,6 +409,12 @@ public override string ToString()
return new Vector3(a.X - b, a.Y - b, a.Z - b);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator -(float a, Vector3 b)
{
return new Vector3(a - b.X, a - b.Y, a - b.Z);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator *(Vector3 a, Vector3 b)
{
Expand Down Expand Up @@ -472,7 +484,7 @@ public static float DistanceSquared(Vector2 valueA, Vector2 valueB)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Distance(Vector2 valueA, Vector2 valueB)
{
return MathF.Sqrt(DistanceSquared(valueA, valueB));
return (valueA - valueB).Length;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
10 changes: 5 additions & 5 deletions Hypercube.Mathematics/Vectors/Vector3i.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,13 @@ public Vector3i WithZ(int value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float DistanceSquared(Vector3i value)
{
return (this - value).LengthSquared;
return DistanceSquared(this, value);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float Distance(Vector3i value)
{
return MathF.Sqrt(DistanceSquared(value));
return Distance(this, value);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -400,15 +400,15 @@ public override string ToString()
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float DistanceSquared(Vector2 valueA, Vector2 valueB)
public static float DistanceSquared(Vector3i valueA, Vector3i valueB)
{
return (valueA - valueB).LengthSquared;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Distance(Vector2 valueA, Vector2 valueB)
public static float Distance(Vector3i valueA, Vector3i valueB)
{
return MathF.Sqrt(DistanceSquared(valueA, valueB));
return (valueA - valueB).Length;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
Loading

0 comments on commit a6e4571

Please sign in to comment.