Java - Point on line

狂风中的少年 提交于 2019-12-12 20:09:52

问题


How can i find out if a Point(x,y) is on a the Line created between two other Points? I tried this but something seems to be wrong, as i don't get the results i should.

public boolean intersects(Point k, Point z, Point p) {

        Line2D line = new Line2D.Float(k.x, k.y, z.x, z.y);

        if (line.ptLineDist(p) == 0) {
            return true;
        } else {
            return false;
        }

    }

回答1:


Try this, taking Hovercraft's note about floating point numbers' imprecision into account.

public boolean intersects(Point k, Point z, Point p) {
       return new Line2D.Float(k, z).ptLineDist(p) <= 0.01;
}


来源:https://stackoverflow.com/questions/10986346/java-point-on-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!