沿着简单的三次贝塞尔曲线找到一个给定距离的点.(在 iPhone 上!) [英] Find a point, a given distance, along a simple cubic bezier curve. (On an iPhone!)

查看:28
本文介绍了沿着简单的三次贝塞尔曲线找到一个给定距离的点.(在 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

推荐答案

计算位置背后有一些简单的数学运算,您可以在每篇讨论贝塞尔曲线的论文中阅读它,甚至在维基百科上.无论如何,我可以与在代码中实际实现它有困难的每个人联系起来,所以我编写了这个示例 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆