沿着简单的三次贝塞尔曲线找到一个给定距离的点。 (在iPhone上!) [英] Find a point, a given distance, along a simple cubic bezier curve. (On an iPhone!)
问题描述
想象一下,您使用curveToPoint创建了一个完全正常的四点贝塞尔曲线(两点和两个控制点):controlPoint1:controlPoint2:在您的可可应用程序中:
Imagine you have a completely normal four-point bezier curve (two points and two control points) created using curveToPoint:controlPoint1:controlPoint2: in your cocoa application:
如何找到积分(和沿着曲线划分?
How do you find points (and the tangents), along the curve?
稍后:基于完整,简化,解决方案在Michal的答案下面,点击:
在立方贝塞尔曲线上找到一个点的正切(在iPhone上)
Later: for a complete, simplified, solution based on Michal's answer below, click to:
Find the tangent of a point on a cubic bezier curve (on an iPhone)
只需复制并粘贴以下代码: https:/ /stackoverflow.com/a/31317254/294884
And just copy and paste the code from: https://stackoverflow.com/a/31317254/294884
推荐答案
计算位置背后有一些简单的数学运算,你可以在讨论Bézier曲线的每篇论文中都会读到它,甚至在维基百科上也是如此。无论如何,我可以告诉所有遇到麻烦的人在代码中实际实现它,所以我写了这个样本UIView,因为这可能是让你入门的最简单方法。
There's some simple math behind calculating the positions, you can read about it in every paper discussing Bézier curves, even on wikipedia. Anyway, I can relate to everybody who's in trouble to actually implement it in code, so I wrote this sample UIView as it's probably the easiest way to get you started.
#import "MBBezierView.h"
CGFloat bezierInterpolation(CGFloat t, CGFloat a, CGFloat b, CGFloat c, CGFloat d) {
CGFloat t2 = t * t;
CGFloat t3 = t2 * t;
return a + (-a * 3 + t * (3 * a - a * t)) * t
+ (3 * b + t * (-6 * b + b * 3 * t)) * t
+ (c * 3 - c * 3 * t) * t2
+ d * t3;
}
@implementation MBBezierView
- (void)drawRect:(CGRect)rect {
CGPoint p1, p2, p3, p4;
p1 = CGPointMake(30, rect.size.height * 0.33);
p2 = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
p3 = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
p4 = CGPointMake(-30 + CGRectGetMaxX(rect), rect.size.height * 0.66);
[[UIColor blackColor] set];
[[UIBezierPath bezierPathWithRect:rect] fill];
[[UIColor redColor] setStroke];
UIBezierPath *bezierPath = [[[UIBezierPath alloc] init] autorelease];
[bezierPath moveToPoint:p1];
[bezierPath addCurveToPoint:p4 controlPoint1:p2 controlPoint2:p3];
[bezierPath stroke];
[[UIColor brownColor] setStroke];
for (CGFloat t = 0.0; t <= 1.00001; t += 0.05) {
CGPoint point = CGPointMake(bezierInterpolation(t, p1.x, p2.x, p3.x, p4.x), bezierInterpolation(t, p1.y, p2.y, p3.y, p4.y));
UIBezierPath *pointPath = [UIBezierPath bezierPathWithArcCenter:point radius:5 startAngle:0 endAngle:2*M_PI clockwise:YES];
[pointPath stroke];
}
}
@end
这个是我得到的:
这篇关于沿着简单的三次贝塞尔曲线找到一个给定距离的点。 (在iPhone上!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!