ポイントが三角形の内側にあるかどうかをチェックすることを含むクイックハルアルゴリズムを書いています。この目的のために、ポイントが内側にある場合はtrueを返し、そうでない場合はfalseを返す次の2つの関数を作成しました。
しかし、一部のポイントは正しく分類されているのに対し、一部は正しく分類されておらず、問題を理解できないという意味で、結果は予想外の静かなものです。誰かが私が書いたコードが正しいかどうかを確認するのを手伝ってくれませんか。アプローチは、ベクトルを使用して、三角形の各エッジの頂点と同じ側に点があるかどうかを確認することです。コードは:
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;
}