public class f3 { static double linepoint (double x1, double y1, double x2, double y2, double x, double y) { return (y - y2) * (x1 - x2) - (x - x2) * (y1 - y2); } static boolean sameSideOrOnLine (double x1, double y1, double x2, double y2, double x3, double y3, double x, double y) { double side = linepoint(x1, y1, x2, y2, x, y); boolean same = (side==0.); // On Line same = same || (side*linepoint(x1, y1, x2, y2, x3, y3) > 0); // Same side with x3 y3 return same; } public static void main(String[] args) { double x1 = 1, y1 = 1; double x2 = -1, y2 = 1; double x3 = -1, y3 = -2; double x = 0, y = 0; boolean inside = true; inside = inside && sameSideOrOnLine(x1, y1, x2, y2, x3, y3, x, y); inside = inside && sameSideOrOnLine(x1, y1, x3, y3, x2, y2, x, y); inside = inside && sameSideOrOnLine(x2, y2, x3, y3, x1, y1, x, y); if (inside) { System.out.println("(" + x + ", " + y + ") ins of [(" + x1 + ", " + y1 + "), (" + x2 + ", " + y2 + "), (" + x3 + ", " + y3 + ")]"); } else { System.out.println("(" + x + ", " + y + ") out of [(" + x1 + ", " + y1 + "), (" + x2 + ", " + y2 + "), (" + x3 + ", " + y3 + ")]"); } } } |