19,469
社区成员
发帖
与我相关
我的任务
分享
double GetPointDistance(CPoint p1, CPoint p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
float GetNearestDistance(CPoint PA, CPoint PB, CPoint P3)
{
//----------图2--------------------
float a,b,c;
a=GetPointDistance(PB,P3);
if(a<=0.00001)
return 0.0f;
b=GetPointDistance(PA,P3);
if(b<=0.00001)
return 0.0f;
c=GetPointDistance(PA,PB);
if(c<=0.00001)
return a;//如果PA和PB坐标相同,则退出函数,并返回距离
//------------------------------
if(a*a>=b*b+c*c)//--------图3--------
return b;
if(b*b>=a*a+c*c)//--------图4-------
return a;
//图1
float l=(a+b+c)/2; //周长的一半
float s=sqrt(l*(l-a)*(l-b)*(l-c)); //海伦公式求面积
return 2*s/c;
}