import java.awt.*; import java.util.*; class line { static double TOLERANCE = .001; public static void main(String[]args) { Point A = new Point (0,0); Point B = new Point (5,5); Point C = new Point (10,10); if (isOn(A,B,C)) System.out.println ("They intersect!"); else System.out.println ("no meet."); } //Checks to see if any of the points lies on the line connecting //any of the other two public static boolean isOn (Point A, Point B, Point C) { double TOLERANCE = .001; Point Left = new Point(); Point Right = new Point(); Point Middle = new Point(); //Put the points in order- catagorize A, B, and C as //Left, Right, and Middle if(A.equals(C) || B.equals(C)) return false; if (A.getX() < B.getX()) { if (C.getX() < A.getX()) { Left = C; Middle = A; Right = B; } else if (C.getX() < B.getX()){ Left = A; Middle = C; Right = B; } else { Left = A; Middle = B; Right = C; } } else { //B < A if (C.getX() < B.getX()) { Left = C; Middle = B; Right = A; } else if (C.getX() < A.getX()){ Left = B; Middle = C; Right = A; } else { Left = B; Middle = A; Right = C; } } //System.out.println ("Left: " + Left.getX() + " Middle " + Middle.getX() // + " Right " + Right.getX()); //Now they're ordered properly- special case where Left.X = Middle.X //if ((int)Left.getX() == (int)Middle.getX()) // if ((int)Left.getY() == (int)Middle.getY()) return true; // else return false; //special case where Middle.X = Right.X //if ((int)Middle.getX() == (int)Right.getX()) // if ((int)Middle.getY() == (int)Right.getY()) return true; // else return false; //Special cases done,find if //Middle lies on the line between left and right //Infinite Slope case if (Left.getX() == Middle.getX() && Middle.getX() == Right.getX()) if ((Left.getY() > Middle.getY && Right.getY() < MiddlegetY) || (Left.getY() < Middle.getY && Right.getY() > MiddlegetY)) return true; double slope = (double)(Left.getY() - Right.getY())/ (Left.getX() - Right.getX()); double projectedY = (double) (Left.getY() + slope*(Middle.getX() - Left.getX())); //System.out.println ("Slope " + slope + " projectedY " + projectedY); if (Math.abs(projectedY - Middle.getY()) < TOLERANCE) return true; return false; } }