我如何转换curveTo(),以点的列表?列表、curveTo

2023-09-08 12:25:43 作者:Calm(从容)

看看下面的AS3将使用绘制曲线 curveTo()

Take the following AS3 that will draw a curved line using curveTo():

var line:Shape = new Shape();

line.x = line.y = 20;
line.graphics.lineStyle(2, 0xFF0000);
line.graphics.curveTo(200, 200, 200, 0);

addChild(line);

由此产生的视觉是:

The resulting visual is:

现在我想要的东西能够遵循这条道路;我怎么可以将这种视觉为坐标表?我挣扎与任何先进的数学,但我假设有一个明显的(一些)公式, curveTo()用来创建上面,我可以复制并修改创建我需要的列表。

Now I want something to be able to follow this path; how can I convert this visual into a list of coordinates? I struggle with any advanced mathematics, but I'm assuming there's an obvious (to some) formula that curveTo() uses to create the above that I can replicate and amend to create my desired list.

的结果可能最终看起来像这样(的假设点之间的偏移约5像素)。

The result may end up looking like this (assuming an offset of about 5px between points).

Vector.<Point> = [
    new Point(20, 20),
    new Point(23, 23),
    new Point(27, 28),
    new Point(33, 32),
    new Point(40, 37)
    /* ...etc... */
];

结果将被用于东西,如创建遵循以下路径的弹丸的雨,例如:

The result will be used for things such as creating a rain of projectiles that follow the following paths, for example:

推荐答案

从阅读的ActionScript文档,据我所知,在行动脚本中的curveTo方法生成二次贝塞尔曲线。

From reading the actionscript documentation, I understand that the curveTo method in action script generates a quadratic Bezier curve.

曲线由您在code指定的三个控制点:

The curve consists of three "control points" that you specified in your code:

control point 1 (p1) = (20,20)
control point 2 (p2) = (200,200)
control point 3 (p3) = (200,0)

要在值内插一个值沿曲线的 U 的范围从0到1(0是起点,1为终点),可以使用所谓的伯恩斯坦多项式。对于二次曲线(你的情况)的多项式是:

To interpolate a value along the curve at value u ranging from 0 to 1 (with 0 being the start point and 1 being the ending point) you can use what are called Bernstein polynomials. For a quadratic curve (your case) the polynomials are:

B1 = (1 - u) * (1 - u)
B2 = 2 * u * (1 - u)
B3 = u * u

简单地计算出这些数字的参数值的 U 的,并加在一起控制点乘以其相应的伯恩斯坦多项式。

Simply calculate these numbers for parameter value u and add together the control points multiplied by their corresponding Bernstein polynomials.

point on curve at parameter *u* = p1 * B1 + p2 * b2 + p3 * B3

因此​​,例如,如果希望得到5分沿曲线,则计算出点沿曲线在参数值0,0.25mmol,0.5%,0.75和1.0

So, for example, if you want to get 5 points along the curve, you calculate the points along the curve at parameter values 0, 0.25, 0.5, 0.75, and 1.0