3

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

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.

Oliver Jones
  • 1,420
  • 7
  • 27
  • 43

2 Answers2

1

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.

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
    }
AlexWien
  • 28,470
  • 6
  • 53
  • 83
  • This is close, but I still need to check against my three points (whether the 3 points make a linear line together). How will I address this situation? Thanks – Oliver Jones Nov 27 '12 at 20:02
  • Point1 = x0, y0 ; Point2 = (x1, y1); Point3 (px, py) as shown above; you must setthe treshold to a feasible value, that only you can know – AlexWien Nov 27 '12 at 20:11
  • add "import java.awt.geom.Line2D" to th ehead of your file – AlexWien Nov 27 '12 at 20:23
  • I do "import java.awt.geom.Line2D;" O_o? – Oliver Jones Nov 27 '12 at 20:35
  • Ok Thanks, So how will I go about actually changing my x1,x2 ect into values that will make then 100% inline (based on the values they already are (so it basically snaps them in line) – Oliver Jones Nov 27 '12 at 20:47
  • You want to project them onto that line? This is another question, Please vote up and accept my answer. I can answer the next question, too. – AlexWien Nov 27 '12 at 20:49
  • Okay: http://stackoverflow.com/questions/13593082/java-make-plot-points-near-linear-exactly-linear – Oliver Jones Nov 27 '12 at 21:08
0

Are you looking for a tolerance meeting some specific formula, or just in general, "close"?

If you're just looking for a general "close", then just check for difference within some useful range instead of equal. e.g.

return Math.abs((x1-x2)*(y1-y3)-(y1-y2)*(x1-x3))<tolerance;

If you have some specific rule, like "the third point must be within 2 pixels of the line formed by the first two points" or some such, then of course you'd have to study whatever rule and write code that explicitly implements it.

Jay
  • 26,876
  • 10
  • 61
  • 112
  • 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. – Oliver Jones Nov 27 '12 at 20:11
  • What I was trying to say is that the formula I gave in my answer works for "some amount of give", vaguely defined. If that's what you're looking for, then I think this should do it for you. I was hedging a bit in case you have some rigid definition of how much give you want to allow. – Jay Nov 28 '12 at 20:56