ios - 在UIBezierPath上给定距离的点坐标

标签 ios objective-c cocoa-touch geometry uibezierpath

我有一条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/

相关文章:

ios - 将 nsmutable 数组数据传递给 NSURLRequest : Post data IOS7

ios - `Realm` 需要 CocoaPods 版本 `>= 1.10` ,您当前的版本不满足, `1.8.3`

iphone - 在模拟器上可以,但在iPhone上不行,如何调试?

objective-c - NSMutableAttributedString 在更改字体时崩溃?

iOS 应用程序在不使用 iOS 11 API 的情况下遵守 iPhone X 屏幕限制

ios - Spritekit 添加音效

objective-c - Xcode 5 未检测到运行 iOS 8 的 iPhone

ios - 为什么每次都会返回相同的时间戳,除非我重新构建并运行应用程序?

iphone - NSMinuteCalenderUnit 与 kCFCalenderUnitMinute

iphone - if(obj && obj != nil) 是否正确且必要?