/// <summary> /// Calculate <paramref name="segments"/> number of evenly spaced points on the curve and updates <see cref="EvenlySpacedPoints"/>. /// <br/> /// <paramref name="segments"/> will be cached and will be used to recalculate <see cref="EvenlySpacedPoints"/> when <see cref="UpdateAccuracy(int)"/> or <see cref="CalculatePoints(List{System.Numerics.Vector2}, int)"/> is called. /// </summary> /// <param name="segments">The number of evenly spaced points(including start and end) to be calculated.</param> private void CalculateEvenlySpacedPoints(int segments) { if (segments < 2) { EvenlySpacedPoints.Clear(); return; } EvenlySpacedPoints.Clear(); this.segments = segments; float unitLength = Length / segments; for (int i = 0; i < segments; i++) { float targetInterpolation = 0; for (int j = 0; j < interpolationToSegment.Count; j++) { if (interpolationToSegment.ElementAt(j).Value == unitLength * i) { targetInterpolation = interpolationToSegment.ElementAt(j).Key; break; } else if (interpolationToSegment.ElementAt(j).Value > unitLength * i) { targetInterpolation = Mathf.Lerp(interpolationToSegment.ElementAt(j - 1).Key, interpolationToSegment.ElementAt(j).Key, (unitLength * i - interpolationToSegment.ElementAt(j - 1).Value) / (interpolationToSegment.ElementAt(j).Value - interpolationToSegment.ElementAt(j - 1).Value)); break; } } EvenlySpacedPoints.Add(GetPointByInterpolation(targetInterpolation)); } }
/// <summary> /// Recalculate the curve using the new resolution and update <see cref="ControlPoints"/>, <see cref="Points"/>, and <see cref="EvenlySpacedPoints"/>. /// </summary> /// <param name="accuracy">The number of points on the curve to be calculated including start and end.</param> private void UpdateAccuracy(int accuracy) { if (accuracy <= 0) { throw new ArgumentException("Must be larger than 0", nameof(accuracy)); } Points.Clear(); EvenlySpacedPoints.Clear(); CalculatePoints(accuracy); CalculateEvenlySpacedPoints(segments); CalculateLength(accuracy); }
/// <summary> /// Recalculate the curve and update <see cref="ControlPoints"/>, <see cref="Points"/>, and <see cref="EvenlySpacedPoints"/>. /// </summary> /// <param name="controlPoints">The control points defining the curve.</param> /// <param name="accuracy">The number of points on the curve to be calculated including start and end.</param> private void CalculatePoints(List <System.Numerics.Vector2> controlPoints, int accuracy) { if (accuracy <= 0) { throw new ArgumentException("Must be larger than 0", nameof(accuracy)); } ControlPoints = controlPoints; Points.Clear(); EvenlySpacedPoints.Clear(); CalculatePoints(accuracy); CalculateEvenlySpacedPoints(segments); CalculateLength(accuracy); }
protected void CalculateEvenlySpacedPoints(int segments) { EvenlySpacedPoints.Clear(); EvenlySpacedPoints = CalculatePoints(start, middle, end, segments); }