查找点是否在三角形内(2D)

Roh*_*wal 1 java geometry convex-hull computational-geometry

我正在编写Quick Hull算法,其中包括检查点是否位于三角形内.为此,我创建了以下两个函数,如果该点在内部则返回true,否则返回false.

然而,结果是安静意外的意义上的一些点被正确分类,而一些不是,我无法弄清楚问题.有人可以帮我验证我写的代码是否正确.方法是我使用向量来确定一个点是否与三角形的每个边缘的顶点位于同一侧.代码是:

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;

    }
Run Code Online (Sandbox Code Playgroud)

JB *_*zet 6

我只想创建一个Polygon,并使用它的contains(Point)方法.为什么重新发明轮子?