确定距离线一定距离的点

时间:2012-10-15 09:51:09

标签: c++ math cocos2d-iphone cocos2d-x

这是一个数学问题,而不是一个实际的编程问题,但由于我使用C++COCOS2D-X,我选择在此处发布。

我正在使用CCBezierTo来创建精灵mySprite运行的bezier运动。 CCBezierConfig结构接受三个点(CCPoint s):controlPoint_1controlPoint_2endPoint。两个controlPoint是贝塞尔曲线的曲线。

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

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:行从左到右开始 Case 1: Line starts from left to right

案例2:行从左上角到右下角开始 Case 2: Line starts from upper left to lower right

案例3:行从右上角到左下角开始 Case 3: Line starts from upper right to lower left

提前致谢。

1 个答案:

答案 0 :(得分:2)

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

步骤2:计算垂直于该向量的向量,并使用单位长度。称之为w。一般来说,(-y, x)(y, -x)都与(x, y)垂直。前者指向“左侧”,后者指向“右侧”。

第3步:将X计算为A + 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, );

我认为这一切都是:

(B - A)/L

或类似。

如果cocos2d对于二维向量具有单独的类型,使用它,您可能会发现可以编写{{1}}等表达式。

相关问题