我有一条UIBezierPath线,我想获得该线在该线起点给定距离处的点的坐标。距离是指沿线的距离。
在下面的图片中,我正在寻找x和y。
完美的解决方案是将距离作为参数并返回坐标的方法。
CGPoint myPoint = [myLine pointAtdistance:53.21]
是否存在类似的东西?我认为这将是一个常见的问题,但无法在网络上找到任何相关信息。也许我在寻找错误的东西?
谢谢。
最佳答案
如果路径不包含曲线段,仅包含线性段,并且一条路径存在很多距离请求,则可以使用一些预处理(第一项):
1. Calculate length of every segment, and cumulative path length till this segment's end
2. With distance request, find proper segment by binary search
(or linear search, if the number of segments is small)
3. Find parameter (0..1) of relative position of point in this segment
4. Calculate coordinates as linear combination of segment end points.
简单的例子:
Points (0,0), (1,0), (1,2), (4,-2), (6,-2)
Lengths [1, 2, 5, 2]
Path cumul. lengths: [0, 1, 3, 8, 10]
Distance req.: 5
Binary search finds 3rd segment (5 is between 3 and 8)
3*(1-t)+8*t=5 (equation to find a parameter)
t = 0.4
X = P[2].X * (1-t) + P[3].X * t
Y = P[2].Y * (1-t) + P[3].Y * t
use (1,2) and (4,-2) coordinates
(X,Y)= (2.2, 0.4)
关于ios - 在UIBezierPath上给定距离的点坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21855158/