Skip to content

Commit

Permalink
Fixes some methods in Vector2 not having readonly/static counterparts…
Browse files Browse the repository at this point in the history
…, adds more documentation, adds Vector2.TryParse(), and Vector2.toString() can be read by the parse functions.
  • Loading branch information
ThatGuy-GEWP committed Jun 28, 2024
1 parent 8ae18c6 commit f8dc07a
Showing 1 changed file with 89 additions and 10 deletions.
99 changes: 89 additions & 10 deletions SFMLGE Local deps/Engine/Vector2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ public static Vector2 Lerp(Vector2 A, Vector2 B, float T)
return A + (B - A) * T;
}

/// <summary>
/// Lerps from this <see cref="Vector2"/> to <paramref name="B"/> lineraly using <paramref name="T"/>
/// </summary>
/// <param name="B">The vector to lerp to</param>
/// <param name="T">Time where 0.0f is A, and 1.0f is B</param>
/// <returns></returns>
public readonly Vector2 Lerp(Vector2 B, float T)
{
return this + (B - this) * T;
}

/// <summary>
/// Gets the distance between <paramref name="A"/> and <paramref name="B"/>
/// </summary>
Expand All @@ -162,17 +173,60 @@ public static float Distance(Vector2 A, Vector2 B)
return MathF.Sqrt(MathF.Pow(B.x - A.x, 2) + MathF.Pow(B.y - A.y, 2));
}

/// <summary>
/// Gets the distance between this <see cref="Vector2"/> and <paramref name="B"/>
/// </summary>
/// <param name="B"></param>
/// <returns></returns>
public readonly float Distance(Vector2 B)
{
return MathF.Sqrt(MathF.Pow(B.x - x, 2) + MathF.Pow(B.y - y, 2));
}

/// <summary>
/// Clamps a vector between two vectors, <paramref name="min"/> and <paramref name="max"/>
/// </summary>
/// <param name="min"></param>
/// <param name="max"></param>
/// <returns></returns>
public readonly Vector2 Clamp(Vector2 min, Vector2 max)
{
return new Vector2(MathF.Min(MathF.Max(min.x, x), max.x), MathF.Min(MathF.Max(min.x, y), max.y));
}

/// <summary>
/// Clamps a vector between two floats, <paramref name="min"/> and <paramref name="max"/>
/// </summary>
/// <param name="min"></param>
/// <param name="max"></param>
/// <returns></returns>
public readonly Vector2 Clamp(float min, float max)
{
return new Vector2(MathF.Min(MathF.Max(min, x), max), MathF.Min(MathF.Max(min, y), max));
}

/// <summary>
/// Clamps a vector with four floats, clamps X between <paramref name="xMin"/> and <paramref name="xMax"/>, then clamps
/// Y between <paramref name="yMin"/> and <paramref name="yMax"/>
/// </summary>
/// <param name="xMin"></param>
/// <param name="xMax"></param>
/// <param name="yMin"></param>
/// <param name="yMax"></param>
/// <returns></returns>
public readonly Vector2 Clamp(float xMin, float xMax, float yMin, float yMax)
{
return new Vector2(MathF.Min(MathF.Max(xMin, x), xMax), MathF.Min(MathF.Max(yMin, y), yMax));
}

//https://stackoverflow.com/questions/10163083/parse-method-for-the-custom-class-c-sharp
/// <summary>
/// Parses a string where <c>"5,6"</c> would create a vector where X is 5 and Y is 6.
/// Throws an exception if it cant parse the string.
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static Vector2 Parse(string input)
{
if (string.IsNullOrWhiteSpace(input)) throw new ArgumentException(input);
Expand All @@ -183,18 +237,47 @@ public static Vector2 Parse(string input)
return new Vector2(float.Parse(vars[0]), float.Parse(vars[1]));
}

//https://stackoverflow.com/questions/10163083/parse-method-for-the-custom-class-c-sharp
/// <summary>
/// Tries Parses a string where <c>"5,6"</c> would create a vector where X is 5 and Y is 6, then stores it in <paramref name="output"/>.
/// Output defaults to 0,0.
/// </summary>
/// <param name="input"></param>
/// <param name="output"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static bool TryParse(string input, out Vector2 output)
{
output = new Vector2(0, 0);
if (string.IsNullOrWhiteSpace(input)) return false;

string[] vars = input.Replace(" ", string.Empty).Split(',');
if (vars.Length != 2) { return false; }

bool gotX = float.TryParse(vars[0], out float x);
bool gotY = float.TryParse(vars[0], out float y);
if (!gotX && !gotY) { return false; }
output = new Vector2 (x, y);

return true;
}

/// <summary>
/// Gets the Magnitude of a vector
/// </summary>
/// <returns></returns>
public readonly float Magnitude(Vector2 vec)
public readonly float Magnitude()
{
return MathF.Sqrt(vec.x * vec.x + vec.y * vec.y);
return MathF.Sqrt(x * x + y * y);
}

public readonly Vector2 Clamp(float xMin, float xMax, float yMin, float yMax)
/// <summary>
/// Gets the Magnitude of a vector
/// </summary>
/// <returns></returns>
public static float Magnitude(Vector2 vec)
{
return new Vector2(MathF.Min(MathF.Max(xMin, x), xMax), MathF.Min(MathF.Max(yMin, y), yMax));
return MathF.Sqrt(vec.x * vec.x + vec.y * vec.y);
}

/// <summary>
Expand Down Expand Up @@ -256,13 +339,9 @@ public readonly Vector2 Round()
return new Vector2(MathF.Round(x), MathF.Round(y));
}





public override string ToString()
public override readonly string ToString()
{
return $"X[{x}] Y:[{y}]";
return x.ToString() + "," + y.ToString();
}

public override bool Equals(object? obj)
Expand Down

0 comments on commit f8dc07a

Please sign in to comment.