Point implements Serializable { public static double distance(Point p1, Point p2) { final double dx = p1.x - p2.x; final double dy = p1.y - p2.y; return Math.sqrt(dx * dx + dy * dy); } Point(double pX, double pY); static double distance(Point p1, Point p2); static Point add(Point p1, Point p2); static Point diff(Point p1, Point p2); static Point multiply(Point p, double d); static Point divide(Point p, double d); static Point centroid(Iterable<Point> points); static Point parsePoint(String pointString); @Override int hashCode(); @Override boolean equals(@Nullable Object other); @Override String toString(); final double x; final double y; }
@Test public void distanceTest() { final Point p1 = new Point(0, 0); final Point p2 = new Point(10, 0); final Point p3 = new Point(10, 10); final Point p4 = new Point(0, 10); assertEquals(10, Point.distance(p1, p2), EPSILON); assertEquals(14.14, Point.distance(p1, p3), 0.01); assertEquals(14.14, Point.distance(p3, p1), 0.01); assertEquals(10, Point.distance(p1, p4), EPSILON); assertEquals(14.14, Point.distance(p2, p4), 0.01); assertEquals(14.14, Point.distance(p4, p2), 0.01); }