线段的拾取算法

时间:2023-01-29 17:10:57
点到直线的距离
  拾取线段
  过两点的直线公式:  ( y - y1 ) / ( y2 - y1 ) = ( x - y1 ) / ( x2 - x1 )
  整理得: AX + BY + C = 0
  点 x0,y0 到该直线的距离 = | A * x0 + B * x0 + C | / sqrt( A * A + B * B )

double PointToLine(CPoint pt1, CPoint pt2, int x, int y)
{
double dx, dy, dc, dd;
dx = pt2.x - pt1.x;    // B
dy = pt2.y - pt1.y;    // A
dc = dx * pt1.y - dy * pt1.x; // C
dd = abs( dy * x - dx * y + dc ) / sqrt( dy * dy + dx * dx ); // d = | Ax0 + Bx0 + C | / sqrt( A2 + B2 );
  return dd;
}