public class PointDemo { public static void main(String[] args) { //3-4-5 right triangle: Point A = new Point(3.0, 0.0); System.out.println("A == " + A); Point B = new Point(0.0, 4.0); System.out.println("B == " + B); System.out.println("Distance from A to B == " + A.distance(B)); System.out.println("Distance from A to B == " + Point.distance(A, B)); System.out.println("Midpoint of A and B == " + A.midpoint(B)); Point origin = new Point(0.0, 0.0); System.out.println("Area == " + origin.area(A, B)); System.out.println("Area == " + Point.area(origin, A, B)); Point C = new Point(1.0, 1.0); System.out.println("theta(C) == " + C.theta()); System.out.println(Point.contains(origin, A, B, C)); System.exit(0); } } class Point { private double x; private double y; public Point(double x, double y) { this.x = x; this.y = y; } //Distance between this point and another one. public double distance(Point another) { final double dx = x - another.x; //horizontal distance final double dy = y - another.y; //vertical distance return Math.hypot(dx, dy); } public static double distance(Point p1, Point p2) { final double dx = p1.x - p2.x; //horizontal distance final double dy = p1.y - p2.y; //vertical distance return Math.hypot(dx, dy); } public Point midpoint(Point another) { //method returns an object final double mx = (x + another.x) / 2.0; final double my = (y + another.y) / 2.0; return new Point(mx, my); } public double theta() { return Math.toDegrees(Math.atan2(y, x)); } //Area of triangle whose vertices are this point, B, and C. //Instead of x and y, could say this.x and this.y. public double area(Point B, Point C) { return Math.abs(x * B.y + B.x * C.y + C.x * y - y * B.x - B.y * C.x - C.y * x) / 2; } //Area of triangle whose vertices are points A, B, and C. public static double area(Point A, Point B, Point C) { return Math.abs(A.x * B.y + B.x * C.y + C.x * A.y - A.y * B.x - B.y * C.x - C.y * A.x) / 2; } //Return true if triangle ABC contains point D. public static boolean contains(Point A, Point B, Point C, Point D) { return area(A, B, C) == area(D, A, B) + area(D, B, C) + area(D, C, A); } public String toString() { return "(" + x + ", " + y + ")"; } }