using System; using System.Collections.Generic; using System.Drawing; using System.Linq; namespace PleoLab { public class Curve { public List points; public Curve() { points = new List(); } public Curve(Curve curve) { points = new List(curve.points); } public Curve(List _normalizedPoints, int _screenWidth, int _screenHeight) { points = new List(); foreach (Vector2 normalizedPoint in _normalizedPoints) { Point screenPoint = new Point( (int)(normalizedPoint.x * _screenWidth), (int)(_screenHeight - normalizedPoint.y * _screenHeight)); points.Add(screenPoint); } } public int Count { get { return points.Count; } } public Point[] ToArray() { return points.ToArray(); } public Point this[int key] { get { return points[key]; } set { points[key] = value; } } public void RemoveAt(int index) { points.RemoveAt(index); } public void Add(Point point) { points.Add(point); } public void Insert(int index, Point point) { points.Insert(index, point); } public Point First() { return points.First(); } public Point Last() { return points.Last(); } public void Clear() { points.Clear(); } public int FindClosestPointIndex(Point _point) { int closestIndex = Int32.MaxValue; double minDistance = Int32.MaxValue; for (int i = 0; i < points.Count; ++i) { double distance = EditorUtils.GetDistance(points[i], _point); if (distance < minDistance) { closestIndex = i; minDistance = distance; } } return closestIndex; } public List GetNormalizedCurve(int _screenWidth, int _screenHeight) { List normalizedPoints = new List(); foreach (Point screenPoint in points) { Vector2 normalizedPoint = new Vector2( (double)screenPoint.X / (double)_screenWidth, ((double)_screenHeight - (double)(screenPoint.Y)) / (double)_screenHeight); normalizedPoints.Add(normalizedPoint); } return normalizedPoints; } public void Sort() { points = points.OrderBy(p => p.X).ToList(); } public void AddRange(IEnumerable _points) { points.AddRange(_points); } public List FindAll(Predicate _predicate) { List pointsFound = points.FindAll(_predicate); return pointsFound; } /// /// Prevents list of points from containing points with the same X value /// Finds points with the same X value and adds, to each duplicate, one to their X value for each duplicate /// public void PreventXDuplicates() { List pointsCopy = new List(points); IEnumerable> groupsByX = pointsCopy.GroupBy(point => point.X); points.Clear(); foreach (IGrouping grp in groupsByX) { IEnumerable groupedPoints = grp.AsEnumerable(); for (int i = 0; i < groupedPoints.Count(); i++) { points.Add(new Point(groupedPoints.ElementAt(i).X + i, groupedPoints.ElementAt(i).Y)); } } } } }