private void CalculatePath() { calculatedPath.Clear(); // Sliders may consist of various subpaths separated by two consecutive vertices // with the same position. The following loop parses these subpaths and computes // their shape independently, consecutively appending them to calculatedPath. int start = 0; int end = 0; for (int i = 0; i < ControlPoints.Length(); ++i) { end++; if (i == ControlPoints.Length() - 1 || ControlPoints[i] == ControlPoints[i + 1] && i != ControlPoints.Length() - 2) { List <Vector2> cpSpan = ControlPoints.GetRange(start, end - start); foreach (Vector2 t in CalculateSubpath(cpSpan)) { if (calculatedPath.Count == 0 || calculatedPath.Last() != t) { calculatedPath.Add(t); } } start = end; } } }
/// <summary> /// Get a point on the curve using interpolation. /// </summary> /// <param name="interpolation">A value from 0 to 1.</param> /// <returns></returns> public System.Numerics.Vector2 GetPointByInterpolation(float interpolation) { if (interpolation < 0 || interpolation > 1) { throw new ArgumentException("Value can only range from 0 to 1", nameof(interpolation)); } if (ControlPoints.Count == 0) { throw new ArgumentException("Doesn't contain any points", nameof(ControlPoints)); } if (ControlPoints.Count == 1) { return(ControlPoints[0]); } else { System.Numerics.Vector2 p1 = GetPointByInterpolationIndependent(ControlPoints.GetRange(0, ControlPoints.Count - 1), interpolation); System.Numerics.Vector2 p2 = GetPointByInterpolationIndependent(ControlPoints.GetRange(1, ControlPoints.Count - 1), interpolation); return(System.Numerics.Vector2.Lerp(p1, p2, interpolation)); } }