//直线过P1,P2两点,P0为直线外一点,求P0到直线的距离和P0在直线上的垂足
void HelloWorld::initPoint(cocos2d::CCPoint point0,cocos2d::CCPoint point1,cocos2d::CCPoint point2)
{
P1 = point1; //直线上的第一个点
P2 = point2; //直线上的第二个点
P0 = point0; //直线外的一点
}
//得到点到直线的距离
float HelloWorld::getLenthPointToLine()
{
A = P2.y - P1.y;
B = P1.x - P2.x;
C = P1.y*(P2.x - P1.x) - P1.x*(P2.y - P1.y);
//直线为Ax+By+C=0
CCLog("A = %f,B = %f,C = %f",A,B,C); d = fabsf(A*P0.x+B*P0.y+C)/sqrt(A*A+B*B);
CCLog("d = %f",d);
return d;
}
//得到点在直线上的垂足
cocos2d::CCPoint HelloWorld::getPointToLineChuiZu()
{
float D = A*P0.y - B*P0.x;
float xd = -(B*D + A*C)/(B*B + A*A); float yd;
if (B == 0)
{
yd = P0.y;
}else{
yd = -(A*xd+C)/B;
}
CCLog("c(%f,%f)",xd,yd); return ccp(xd,yd);
}