C語言實現點到線段的最小距離
阿新 • • 發佈:2018-12-24
/************************************************* Description: // 判斷目標點pt到線段(PT1,PT2)最小距離是否小於distance Input: // distance:目標距離,pt:目標點;pt1,pt2線段的端點 Return: // 如目標點到線段最小距離小於目標距離則返回TRUE,反之返回 FALSE *************************************************/ BOOL IsPointToLine(const double distance,POINT pt, const POINT pt1, const POINT pt2) { POINT p1 = { pt.x - pt1.x,pt.y - pt1.y }, p2 = { pt2.x - pt1.x,pt2.y - pt1.y }; POINT p3 = { pt1.x - pt2.x,pt1.y - pt2.y }, p4 = { pt.x - pt2.x,pt.y - pt2.y }; double nLen1 =fabs(sqrt(p1.x *p1.x + p1.y*p1.y)); double nLen2 =fabs(sqrt(p2.x *p2.x + p2.y*p2.y)); double nLen3 = fabs(sqrt(p4.x *p4.x + p4.y*p4.y)); double nNum = p1.x*p2.x + p1.y*p2.y; if (nNum <= 0) return min(nLen1,nLen2) <distance ? TRUE : FALSE; if (p3.x*p4.x + p3.y*p4.y <= 0) return min(nLen2, nLen3) <distance ? TRUE : FALSE; nNum = nNum / (nLen1*nLen2); nNum = nLen1*fabs(pow(1 - pow(nNum, 2), 0.5)); return nNum < distance ? TRUE : FALSE; }