Util.cs 912 B

12345678910111213141516171819202122232425262728293031
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. public static class Util
  5. {
  6. /// <summary>
  7. /// Extension that converts an array of Vector2 to an array of Vector3
  8. /// </summary>
  9. public static Vector3[] ToVector3(this Vector2[] vectors)
  10. {
  11. return System.Array.ConvertAll<Vector2, Vector3>(vectors, v => v);
  12. }
  13. /// <summary>
  14. /// Extension that, given a collection of vectors, returns a centroid
  15. /// (i.e., an average of all vectors)
  16. /// </summary>
  17. public static Vector2 Centroid(this ICollection<Vector2> vectors)
  18. {
  19. return vectors.Aggregate((agg, next) => agg + next) / vectors.Count();
  20. }
  21. /// <summary>
  22. /// Extension returning the absolute value of a vector
  23. /// </summary>
  24. public static Vector2 Abs(this Vector2 vector)
  25. {
  26. return new Vector2(Mathf.Abs(vector.x), Mathf.Abs(vector.y));
  27. }
  28. }