diff --git a/Runtime/Scripts/Primitives/UILineRenderer.cs b/Runtime/Scripts/Primitives/UILineRenderer.cs index d3b5d3d..7225145 100644 --- a/Runtime/Scripts/Primitives/UILineRenderer.cs +++ b/Runtime/Scripts/Primitives/UILineRenderer.cs @@ -419,7 +419,7 @@ namespace UnityEngine.UI.Extensions { return Segments[segmentIndex - 1][index - 1]; } - else if (Segments.Count > 0) + else if (Segments?.Count > 0) { var segmentIndexCount = 0; var indexCount = index; @@ -443,6 +443,29 @@ namespace UnityEngine.UI.Extensions } } + /// + /// Calculates the position of a point on the curve, given t (0-1), start point, control points and end point. + /// + /// Required Percentage between start and end point, in the range 0 to 1 + /// Required Starting point + /// Required Control point 1 + /// Required Control point 2 + /// Required End point + /// Vector2 position of point on curve at t percentage between p1 and p4 + public Vector2 CalculatePointOnCurve(float t, Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4) + { + var t2 = t * t; + var t3 = t2 * t; + + var x = p1.x + (-p1.x * 3 + t * (3 * p1.x - p1.x * t)) * t + (3 * p2.x + t * (-6 * p2.x + p2.x * 3 * t)) * t + + (p3.x * 3 - p3.x * 3 * t) * t2 + p4.x * t3; + + var y = p1.y + (-p1.y * 3 + t * (3 * p1.y - p1.y * t)) * t + (3 * p2.y + t * (-6 * p2.y + p2.y * 3 * t)) * t + + (p3.y * 3 - p3.y * 3 * t) * t2 + p4.y * t3; + + return new Vector2(x, y); + } + /// /// Get the Vector2 position of a line within a specific segment /// @@ -480,4 +503,4 @@ namespace UnityEngine.UI.Extensions } } } -} \ No newline at end of file +}