bezierpath-length
bezierpath-length copied to clipboard
Calculate percentage at point
Hi @louisdh , I noticed your api has a method for .point(at:)
, but do you have an Idea of how to do the inverse? Calculate the length
/percentage
given the point in a path?
Btw, great job with the library, really useful.
Hi, glad you like this little library. Some thoughts on the "calculate percentage at point" request: That's perhaps more complex than the question makes it seem. First off: a point you provide is not guaranteed to be on the path, so the result would be an optional and require a way of checking if it indeed lies on the path.
Secondly: a point on a path may lie on an intersection, meaning it's impossible to provide a "correct" answer to "What percentage of the path does the point lie on?". A useful API should probably return an array of results. Making it up to the API caller to decide which result is meaningful.
Third: the current implementation of this library uses an approximation for cubic and quad curve subpath lengths. This means the given point may lie on the path, but not on the approximation.
Finally: one possible implementation may be to use the current .point(at:)
API and basically going through the path from 0% to 100%, increasing the percentage a little bit at a time (depending on the complexity of the path). Then, at each interval, check if the result of the .point(at:)
is the point you're looking for. There you would probably also want to maintain a delta (which might be meaningful given line widths, when the path is actually drawn). Whenever the point you're looking for is within a certain delta of the result of .point(at:)
, you've got your (read: a) result. 🙂