12345678910111213141516171819202122232425262728293031 |
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- 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));
- }
- }
|