/* * @author Valentin Simonov / http://va.lent.in/ */ using System.Collections.Generic; using System.Text; using TouchScript.Pointers; using UnityEngine; namespace TouchScript.Utils { /// /// Utils to manipulate clusters of points. /// public static class ClusterUtils { private static StringBuilder hashString = new StringBuilder(); /// /// Calculates the centroid of pointers' positions. /// /// List of pointers. /// Centroid of pointers' positions or if cluster contains no points. public static Vector2 Get2DCenterPosition(IList pointers) { var count = pointers.Count; if (count == 0) return TouchManager.INVALID_POSITION; if (count == 1) return pointers[0].Position; var position = new Vector2(); for (var i = 0; i < count; i++) position += pointers[i].Position; return position / count; } /// /// Calculates the centroid of pointers' previous positions. /// /// List of pointers. /// Centroid of pointers' previous positions or if cluster contains no points. public static Vector2 GetPrevious2DCenterPosition(IList pointers) { var count = pointers.Count; if (count == 0) return TouchManager.INVALID_POSITION; if (count == 1) return pointers[0].PreviousPosition; var position = new Vector2(); for (var i = 0; i < count; i++) position += pointers[i].PreviousPosition; return position / count; } /// /// Computes a unique hash for a list of pointers. /// /// List of pointers. /// A unique string for a list of pointers. public static string GetPointsHash(IList pointers) { hashString.Remove(0, hashString.Length); for (var i = 0; i < pointers.Count; i++) { hashString.Append("#"); hashString.Append(pointers[i].Id); } return hashString.ToString(); } } }