2014-02-18 43 views
1

我有一條UIBezier路徑線,我想要在此行的開始位置給定距離處的座標。距離我的意思是沿着這條線的距離。UIBezierPath上指定距離處的點座標

在下面的圖片上,我正在尋找x和y。

enter image description here

完美的解決方案是,是以距離作爲參數的方法和返回的座標。

CGPoint myPoint = [MYLINE pointAtdistance:53.21]

是否類似的東西存在嗎?我認爲這將是一個常見問題,但無法在網上找到任何相關信息。也許我在尋找錯誤的東西?

謝謝。

+0

如果你的意思是從起點的距離,然後導致你尋求實際上可能不是唯一 – mathematician1975

+0

我的意思是沿着線起點的距離。我會編輯我的問題:) – Legisey

+0

您的路線中是否有曲線段?典型用法是什麼 - 一條路徑和很多距離要求? – MBo

回答

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) 
+0

感謝您的回答。我實現了它,它工作正常。對於任何感興趣的人,如果該點在P2和P3之間:t =(dist.req-P2.dist)/(P3.dist-P2.dist),則在這種情況下:t =(5-3)/ 3)= 0.4 – Legisey

+0

是的,你是對的,(P3.dist - P2.dist)=長度2,不需要計算 – MBo

+0

只是你的答案中的一個小類型:'Y = P [2] .Y *(1- t)+ P [3] .X * t'應該是'Y = P [2] .Y *(1-t)+ P [3] .Y * t' ...但是非常感謝你的偉大回答! – Georg