我正在編寫Quick Hull算法,其中包括檢查點是否位於三角形內。爲此,我創建了以下兩個函數,如果點在裏面,則返回true,否則返回false。查找點是否在三角形內(2D)
但是,結果是安靜的意外的意義上的一些點被正確分類,而有些不是,我不知道這個問題。有人可以幫我驗證我寫的代碼是否正確。方法是我使用矢量來確定一個點是否與三角形每個邊的頂點位於同一側。 的代碼是:
public boolean ptInside(Point first, Point last, Point mx, Point cur) {
boolean b1 = pointInside(first, last, mx, cur);
boolean b2 = pointInside(last, mx, first, cur);
boolean b3 = pointInside(first, mx, last, cur);
return b1 && b2 && b3;
}
public boolean pointInside(Point first, Point last, Point mx, Point cur) {
int x1 = last.xCo - first.xCo;
int y1 = last.yCo - first.yCo;
int x2 = mx.xCo - first.xCo;
int y2 = mx.yCo - first.yCo;
int x3 = cur.xCo - first.xCo;
int y3 = cur.yCo - first.yCo;
int cross1 = x1 * y2 - x2 * y1;
int cross2 = x1 * y3 - x3 * y1;
if (cross1 * cross2 > 0)
return true;
else
return false;
}
1.寫測試(特別是在邊界附近)2.確保它們通過。 – assylias