Java-点对点 [英] Java - Point on line

查看:108
本文介绍了Java-点对点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何确定一个点(x,y)是否在其他两个点之间创建的直线上? 我尝试了此操作,但似乎有些问题,因为我没有得到应有的结果.

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;
        }

    }

推荐答案

尝试此操作,要考虑到Hovercraft关于浮点数不精确度的注意事项.

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;
}

这篇关于Java-点对点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆