1

这更像是一个数学问题,而不是一个实际的编程问题,但由于我使用的是C++and COCOS2D-X,所以我选择在这里发布它。

我正在使用CCBezierTo创建精灵mySprite运行的贝塞尔运动。该CCBezierConfig结构接受三个点 ( CCPoints):controlPoint_1controlPoint_2endPoint。两个controlPoints 是贝塞尔曲线将弯曲的点。

现在问题来了。我需要创建曲线的controlPoints 是未知的,只能通过做一些数学来获得。这些是已知的变量。请参考下图。

A = The start point of the curve
B = The end point of the curve
Line AB = The line created by connecting A and B together
L = The distance between A and B/The length of Line AB
D = The distance between the line and the unknown points

我正在尝试寻找 X 和 Y。我已经实现了一点,但只有当线条是水平或垂直时:

// From left to right:
ccBezierConfig bezierConfig;
bezierConfig.controlPoint_1 = CCPointMake( A.x + ( L * 0.25f ), A.y + aCertainHeight );
bezierConfig.controlPoint_2 = CCPointMake( A.x + ( L * 0.75f ), A.y - aCertainHeight );
bezierConfig.endPoint = B;

/** CCPointMake( x, y ) is a macro that creates a CCPoint object, which is a point on a plane. 
    It accepts two float values determining the X and Y position of the point.**/

// From top to bottom:
ccBezierConfig bezierConfig;
bezierConfig.controlPoint_1 = CCPointMake( A.x + aCertainWidth, A.y - ( L * 0.25f ) );
bezierConfig.controlPoint_2 = CCPointMake( A.x - aCertainWidth, A.y - ( L * 0.25f ) );
bezierConfig.endPoint = B;

如果线是对角线,我怎样才能得到 X 和 Y?

案例1:线从左到右开始 案例1:线从左到右开始

案例2:线从左上到右下 案例2:线从左上到右下

案例3:线从右上到左下开始 案例3:线从右上到左下开始

提前致谢。

4

1 回答 1

2

第 1 步:计算从 A 到 B 的向量,称为v

第 2 步:计算一个垂直于该向量且具有单位长度的向量。调用它w。一般来说(-y, x)(y, -x)都垂直于(x, y)。前者指向“左侧”,后者指向“右侧”。

第 3 步:计算XA + 0.25 * v + D_1 * w类似Y

我认为这一切都可以解决:

// Using a "point" type for a vector is dodgy, but it works.
w = CCPointMake((B.y - A.y) / L, -(B.x - A.x) / L);
X = CCPointMake(
    0.75 * A.x + 0.25 * B.x + D_1 * w.x,
    0.75 * A.y + 0.25 * B.y + D_1 * w.y,
);
Y = CCPointMake(
    0.25 * A.x + 0.75 * B.x + D_2 * w.x,
    0.25 * A.y + 0.75 * B.y + D_2 * w.y,
);

或类似的。

如果 cocos2d 有一个单独的二维向量类型,使用它,你可能会发现你可以写出类似(B - A)/L的表达式。

于 2012-10-15T10:10:20.410 回答