Domanda

I have a UIBezierPath line and I would like to get the coordinates of a point that is on this line at a given distance of the beginning of this line. By distance I mean distance ALONG the line.

On the following picture, I am looking for x and y.

enter image description here

The perfect solution would be a method that takes the distance as an argument and return the coordinates.

CGPoint myPoint = [myLine pointAtdistance:53.21]

Does something similar exist ? I thought it would be a common problem, but was not able to find any relevant info on the web. Maybe I am searching for the wrong thing ?

Thank you.

È stato utile?

Soluzione

If path contains no curve segments, only linear ones, and there is a lot of distance requests for one path, then you can use some preprocessing (1st item):

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.

Simple example: 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)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top