从Bezier路径计算点 [英] Compute points from Bezier Path
本文介绍了从Bezier路径计算点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个随机的贝塞尔曲线,如下所示:
Let's say I have a random bezier path, like this:
let bezierPath = UIBezierPath()
bezierPath.move(to: CGPoint(x: 3, y: 0.84))
bezierPath.addCurve(to: CGPoint(x: 11, y: 8.84), controlPoint1: CGPoint(x: 3, y: 1.84), controlPoint2: CGPoint(x: 9, y: 4.59))
// [...]
bezierPath.addCurve(to: CGPoint(x: 3, y: 0.84), controlPoint1: CGPoint(x: 7, y: 4.84), controlPoint2: CGPoint(x: 3, y: -0.16))
bezierPath.close()
我想创建一个为给定百分比计算CGPoint的函数,其中bezierPath的第一个点为0%,最后一个点为100%:
I would like to create a function that compute a CGPoint for a given percentage, where 0% is the first point and 100% the last point of the bezierPath:
extension UIBezierPath {
func getPointFor(percentage: Float) -> CGPoint {
//Computation logic
return result
}
}
我发现了此帖子但是解决方案不允许我获得所有点(例如,位于路径的15.5%处).
I have found this post but the solution doesn't allow me to get all points (position at 15.5% of the path for example).
有没有办法做到这一点?
Is there a way to do this?
推荐答案
我找到了用Objective-C编写的解决方案.您可以在此处找到源代码.
I have found a solution written in objective-c. You can find the source code here.
我通过使用桥接来设法使用它标头:
#ifndef bridging_header_h
#define bridging_header_h
#import "UIBezierPath+Length.h"
#endif /* bridge_header_h */
您可以使用以下两个功能:
And you can use the two functions like this:
print("length=\(bezierPath.length())")
for i in 0...100 {
let percent:CGFloat = CGFloat(i) / 100.0
print("point at [\(percent)]=\(bezierPath.point(atPercentOfLength: percent))")
}
输出:
length=143.316117804497
point at [0.0]=(3.0, 0.839999973773956)
point at [0.01]=(3.26246070861816, 1.29733419418335)
point at [0.02]=(3.97137236595154, 1.91627132892609)
point at [0.03]=(5.00902938842773, 2.69386911392212)
[...]
point at [0.99]=(3.27210903167725, 0.765813827514648)
point at [1.0]=(3.0, 0.839999973773956)
这篇关于从Bezier路径计算点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文