Objective c 在cocos2d中间垂直于直线的打印点
我在两点之间画了一条线。比如说A和B,我可以很容易地抓住Cocos2d中直线的中点,我也可以很容易地计算向量和垂直于这条直线的向量。然而,我的数学技能非常生疏,我不知道如何做到以下几点 假设A和B之间的距离是50,那么中点是25。我想画一个点,垂直于这条线,距离它10Objective c 在cocos2d中间垂直于直线的打印点,objective-c,cocos2d-iphone,Objective C,Cocos2d Iphone,我在两点之间画了一条线。比如说A和B,我可以很容易地抓住Cocos2d中直线的中点,我也可以很容易地计算向量和垂直于这条直线的向量。然而,我的数学技能非常生疏,我不知道如何做到以下几点 假设A和B之间的距离是50,那么中点是25。我想画一个点,垂直于这条线,距离它10 C / \ / \ / \ / \ / \ A------------B 很抱歉这个糟糕的例子,但我不知道怎么做。此外,AB线始终处于某
C
/ \
/ \
/ \
/ \
/ \
A------------B
很抱歉这个糟糕的例子,但我不知道怎么做。此外,AB线始终处于某个角度,它从来不像这里那样笔直。给定中点
m
和垂直向量v
,您需要标准化v
,然后从m
向v
方向移动。比如说:
Vector2d nv = v / v.length(); // Assuming Vector2d is your vector class and length gives the length of v
Point2d newPoint = m + (nv * 10.0); // Assumes you can multiply a vector by a scalar
Vector2d nv;
nv.x = v.x / v.length();
nv.y = v.y / v.length();
newPoint.x = m.x + nv.x * 10.0;
newPoint.y = m.y + nv.y * 10.0;
如果您不在C++中工作,您可能需要手动编写它:
Vector2d nv = v / v.length(); // Assuming Vector2d is your vector class and length gives the length of v
Point2d newPoint = m + (nv * 10.0); // Assumes you can multiply a vector by a scalar
Vector2d nv;
nv.x = v.x / v.length();
nv.y = v.y / v.length();
newPoint.x = m.x + nv.x * 10.0;
newPoint.y = m.y + nv.y * 10.0;