UIBezierPath上给定距离处的点坐标

时间:2014-02-18 13:21:55

标签: ios objective-c cocoa-touch geometry uibezierpath

我有一个UIBezierPath线,我想获得该线开始处给定距离处该点上的点的坐标。按距离我的意思是距离线的距离。

在下图中,我正在寻找x和y。

enter image description here

完美的解决方案是将距离作为参数并返回坐标的方法。

  

CGPoint myPoint = [myLine pointAtdistance:53.21]

是否存在类似的东西?我认为这将是一个常见问题,但无法在网络上找到任何相关信息。也许我在寻找错误的东西?

谢谢。

1 个答案:

答案 0 :(得分:3)

如果路径不包含曲线段,只包含线性段,并且一条路径有很多距离请求,那么您可以使用一些预处理(第1项):

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.

简单示例: enter image description here

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)