C 如何检测点是否位于线段上

C 如何检测点是否位于线段上,c,geometry,computational-geometry,C,Geometry,Computational Geometry,目前,我可以通过以下代码检测该点是否属于线段: uint8_t point_lies_onSegment(const POINT2D *point, const POINT2D *linea, const POINT2D *lineb) { double slope, intercept; double px, py; double left, top, right, bottom; // Bounding Box For Line Segment double dx, dy; p

目前,我可以通过以下代码检测该点是否属于线段:

uint8_t point_lies_onSegment(const POINT2D *point, const POINT2D *linea, const POINT2D *lineb) {
double slope, intercept;
  double px, py;
  double left, top, right, bottom; // Bounding Box For Line Segment
  double dx, dy;

px = point->x;
py = point->y;

dx = lineb->x - linea->x;
dy = lineb->y - linea->y;

  slope = dy / dx;
  // y = mx + c
  // intercept c = y - mx
  intercept = linea->y - slope * linea->x; // which is same as y2 - slope * x2

  // For Bounding Box
  if(linea->x < lineb->x) {
    left = linea->x;
    right = lineb->x;
  } else {
    left = lineb->x;
    right = linea->x;
  }
  if(linea->y < lineb->y) {
    top = linea->y;
    bottom = lineb->y;
  } else {
    top = linea->y;
    bottom = lineb->y;
  }

  //"Equation of the line: %.2f X %c %.2f\n", slope, ((intercept < 0) ? ' ' : '+'), intercept;

  if( slope * px + intercept > (py - FP_TOLERANCE) &&
    slope * px + intercept < (py + FP_TOLERANCE)) {
      if( px >= left && px <= right && 
          py >= top && py <= bottom ) {
            return VG_TRUE;
      }
      else
        return VG_FALSE;
  }
  else
    return VG_FALSE;
}
uint8点位于分段上(常数点2D*点,常数点2D*线A,常数点2D*线B){
双斜率,截距;
双px,py;
双左、上、右、下;//线段的边界框
双dx,dy;
px=点->x;
py=点->y;
dx=lineb->x-linea->x;
dy=lineb->y-linea->y;
斜率=dy/dx;
//y=mx+c
//截距c=y-mx
截距=linea->y-斜率*linea->x;//与y2-斜率*x2相同
//用于边界框
如果(线性A->x<线性B->x){
左=直线A->x;
右=线条B->x;
}否则{
左=线条B->x;
右=直线A->x;
}
如果(线性A->y<线性B->y){
顶部=线性A->y;
底部=线条B->y;
}否则{
顶部=线性A->y;
底部=线条B->y;
}
//“直线方程:%.2f X%c%.2f\n”,斜率,((截距<0)?“”:“+”),截距;
如果(斜率*px+截距>(py-FP_公差)&&
斜率*px+截距<(py+FP_公差)){

如果(px>=left&&px=top&&py一条垂直线将导致您的程序被零除。我很惊讶您竟然会得到任何输出-我本以为它会崩溃。因为它没有崩溃,所以您很可能会将
NaN
变成
slope
,这将导致您的其余问题。您可能会想使用与您当前使用的算法不同-例如,不需要您计算斜率的算法。

一条垂直线将导致您的程序被零除。我很惊讶您会得到任何输出-我本以为它会崩溃。因为它不会崩溃,您可能会将
NaN
变成
坡度
,这会导致您的其他问题。您可能希望使用与当前使用的算法不同的算法-例如,不需要您计算坡度的算法。

如果直线是垂直的,则需要检查相关点的x坐标。如果该点的x坐标相同,则s垂直线段的x坐标-然后检查该点的y坐标是否在垂直线段的y坐标之间。

如果该线是垂直的,则需要检查该点的x坐标。如果该点的x坐标与垂直线段的x坐标相同-然后检查点的y坐标在垂直线段的y坐标之间。

哪种不同的算法?Kalaro在他的答案中建议了一种,还有很多其他的。我相信如果你谷歌一下,你会发现一些东西。哪种不同的算法?Kalaro在他的答案中建议了一种,还有很多其他的算法。我相信如果你谷歌一下,你会发现一些东西。哪种不同的算法?Kalaro在他的答案中建议了一种,还有很多其他的算法。我相信如果你谷歌一下,你会发现一些其他的打开一些东西。我添加了以下代码:if(FP_等于(dx,0.0)){if(py y&&py>=linea->y)返回VG_TRUE;else返回VG_FALSE;}现在就可以了。谢谢。我添加了以下代码:if(FP_等于(dx,0.0)){if(py y&&py>=linea->y)返回VG_TRUE;else返回VG_FALSE;}现在就可以了。谢谢。