Java-平均线性图图 [英] Java - Average Linear Graph Plots

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

问题描述

我有一段代码检查给定的3个坐标是否彼此线性(如果是,则返回true).但是,有没有办法使代码给出或吸收一些像素/图?

I have a piece of code that checks if the given 3 coordinates are linear to one another (if so, return true). But is there a way to make the code give or take a few pixels/plots?

private boolean collinear(double x1, double y1, double x2, double y2, double x3, double y3) {
    return (y1 - y2) * (x1 - x3) == (y1 - y3) * (x1 - x2);
}

您会看到,坐标必须完全内联才能注册为线性.如何使它看起来在某种范围"内,以便在一个值内进行检查,然后使用新值设置坐标?

You see, the coordinates have to be exactly inline for it to register as linear. How will I be able to make it look within a 'range' of some kind, for it to check within a value, and then to then set the coordinates with the new value?

----已添加----

---- Added ----

我正在尝试查看3个点是否形成一条直线,但我希望它在阈值内进行检查(因为有些点可能会稍微偏离一点).一旦Java发现实际上这些点是线性的(给定或取是),我希望它随后将x1,x2,x3,y1 ect ...替换为Java所检查的值.

I'm trying to see if the 3 points make a linear line, but I want it to check within a threshold (as some points maybe off by a little). Once the Java has found that in fact the points are linear (give or take), I want it to then replace the x1, x2, x3, y1 ect ... with what the value would have been that Java checked against.

推荐答案

使用Hesse Normal Form,您可以计算从点到线的垂直距离.因此,当距离小于网格时,它们就足够近了.

Using the Hesse Normal Form you can calculate the perpendicular distance from a point to a line. So when the distance is smaller than a treshhold, they are sufficiently near.

这样的代码称为distanceFromLine() Java具有内置功能

Such code is called distanceFromLine() Java has that built in

   Line2D line = new Line2D.Double(x0, y0, x1, y1);

    double distance = line.ptLineDist(px, py);

    if (Math.abs(distance) < threshold) {
              // is Near line
    }

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

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