For an animation timing algorithm I need to supply a path as the curve. Probably a bezier curve with control points on both ends.
The problem is that it seems not possible to calculate points on a CGPath because CGPathRef is opaque. Also Apple provides no mechanism to compute points on a path.
Is there a library or utility class which can compute points on a bezier curve or path, for a given location like 0.5 for the middle along the path?
Or let me rephrase it: If CGPath / CGPathRef makes this impossible because it is opaque, and if you only care about bezier curves, is there a way to compute points for locations along the path?
The math behind a Bézier path is actually "just":
This means that if you know, the start, the end and both control points (c1 and c2), then you can calculate the value for any t (from 0 to 1).
It the values are points (like in the image below) then you can do these calculations separately for x and y.
This is form my explanation of Bézier paths here and the code to update the orange circle as the slider changes (in Javascript) is simply this (it shouldn't be too hard to translate into Objective-C or simply C but I was too lazy):
If you already have the control points to the bezier curve you would like to use for the timing function (of what I presume to be
CAAnimation
), then you should use the following function to get the appropriate timing function:However, if what you are trying to do is calculate the Y-locaiton of a bezier curve for a given X-location, you will have to calculate that yourself. Here is a reference for how to do so: Bezier Curves
Point location calculation from CGPath (Swift 4).
Usage: