目前我可以檢測點是否屬於用下面的代碼線段:如何檢測,如果點位於段
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;
}
但是,如果該行是垂直預期這是行不通的。 例如:
線段=(10,10) - (10,30) 點=(10,20)
此返回FALSE。
如何解決?
我添加以下代碼: \t如果(FP_EQUALS(DX,0.0)){ \t \t如果(PY <= lineb->ý&& PY> = linea-> y)等 \t \t \t返回VG_TRUE; \t \t else \t \t \t return VG_FALSE; \t}它現在有效。謝謝。 – 2013-03-06 02:38:09