分割贝塞尔曲线 [英] Splitting a bezier curve

查看:211
本文介绍了分割贝塞尔曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款适用于iPhone的游戏,它可以在你的角色移动后创建一条路径(移动类似于蛇,但在转向方面是弯曲的)。我现在这样做的方法就是将玩家所有的顶点保持在一个数组中,然后在每一帧中的每一个上画一个圆圈。

我想转而使用贝塞尔曲线。我已经做了很多关于它们的阅读,我对它们的理解非常好,但我对数学并不是很好。我已经明白我应该使用DeCasteljau的算法在特定的t处分割曲线,但我还没有找到使用哪个公式以及如何在代码中实现它。

I wanna move on to using bezier curves instead. I've done a lot of reading about them and I understand them quite well, but im not really good with math. I've came to an understanding that i should use DeCasteljau's algorithm to split the curve at a specific t but i haven't found out just which formula to use and how to implement this in code.

所以我现在拥有的是t = 1时曲线的所有控制点。现在我只想获得t< 1的所有控制点。有人可以给我一个易于理解的数学公式或者这个实现(最好是在python或objective-c中)。也许甚至有一个对象你可以在iphone sdk中用来分割曲线吗?

So what I currently have is all the controlpoints for a curve at t=1. Now i just want to get all the controlpoints for t<1. Can somebody give me an easy to understand mathematical formula for this or an implementation (preferably in python or objective-c). Maybe there's even a object that you can use in iphone sdk to split curves already?

推荐答案

我设法让它运转起来,实际上非常简单的数学。只需计算贝塞尔曲线的所有切线,就可以获得积分。

I managed to get it working, actually really simple math. Just calculate all the tangents for the bezier and you get the points.

这里有一些蟒蛇:

def sliceBezier(points, t):
    x1, y1 = points[0]
    x2, y2 = points[1]
    x3, y3 = points[2]
    x4, y4 = points[3]

    x12 = (x2-x1)*t+x1
    y12 = (y2-y1)*t+y1

    x23 = (x3-x2)*t+x2
    y23 = (y3-y2)*t+y2

    x34 = (x4-x3)*t+x3
    y34 = (y4-y3)*t+y3

    x123 = (x23-x12)*t+x12
    y123 = (y23-y12)*t+y12

    x234 = (x34-x23)*t+x23
    y234 = (y34-y23)*t+y23

    x1234 = (x234-x123)*t+x123
    y1234 = (y234-y123)*t+y123

    return [(x1, y1), (x12, y12), (x123, y123), (x1234, y1234)]

致电:

sliceBezier([(point1_x, point1_y),(controlpoint1_x, controlpoint1_y),(controlpoint2_x, controlpoint2_y),(point2_x, point2_y)], 0.23);

这篇关于分割贝塞尔曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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