Skip to content

Instantly share code, notes, and snippets.

@Hyperparticle
Last active November 24, 2017 13:02
Show Gist options
  • Save Hyperparticle/0629b17b6a7ceecec4e40764890058be to your computer and use it in GitHub Desktop.
Save Hyperparticle/0629b17b6a7ceecec4e40764890058be to your computer and use it in GitHub Desktop.
Utility Class for Vectors
public static class Util
{
/// <summary>
/// Extension that converts an array of Vector2 to an array of Vector3
/// </summary>
public static Vector3[] ToVector3(this Vector2[] vectors)
{
return System.Array.ConvertAll<Vector2, Vector3>(vectors, v => v);
}
/// <summary>
/// Extension that, given a collection of vectors, returns a centroid
/// (i.e., an average of all vectors)
/// </summary>
public static Vector2 Centroid(this ICollection<Vector2> vectors)
{
return vectors.Aggregate((agg, next) => agg + next) / vectors.Count();
}
/// <summary>
/// Extension returning the absolute value of a vector
/// </summary>
public static Vector2 Abs(this Vector2 vector)
{
return new Vector2(Mathf.Abs(vector.x), Mathf.Abs(vector.y));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment