package geometry.java;
import static org.junit.Assert.*;
import geometry.java.Pair;
import geometry.java.Point;
import geometry.java.Points;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import org.junit.Test;
public class PlaneGeometryTest {
// [{0.9404276009849271,0.4637809707158117}, {0.1343379470719266,0.856418745571239}, {0.41495871815701435,0.787983871063451}, {0.37085043512217486,0.7525604213257733}, {0.09576133429466949,0.8725551197532135}]
@Test
public void testDivideAndConquer() {
List<Point> points = new ArrayList<>();
for (int i = 0; i < 100; i++) {
points.add(new Point(Math.random(), Math.random()));
}
Point[] inputA = points.toArray(new Point[points.size()]);
Arrays.sort(inputA, Point.COMPARE_X);
Point[] inputB = Arrays.copyOf(inputA, inputA.length);
Pair divide = Points.divideAndConquer(inputA);
Pair force = bruteForce(inputB);
assertEquals(force, divide);
}
/**
* Return the closest pair of points using the brute force method of comparing each
* pair of points
* @param points unordered points
*/
private Pair bruteForce(Point... points) {
Pair closest = new Pair(null, null);
for (int i = 0; i < points.length - 1; i++)
for (int j = i + 1; j < points.length; j++) {
Pair pair = new Pair(points[i], points[j]);
if (pair.dist < closest.dist) closest = pair;
}
return closest;
}
@Test
public void testOptimizedBruteForce() {
Point a = new Point(0.0, 2.0);
Point b = new Point(2, 0);
Point c = new Point(0.0, 2.0);
Pair closest = Points.optimizedBruteForce(new Point[]{a, b, c});
assertEquals(new Pair(a,c), closest);
}
@Test
public void testClosestInStrip() {
Point[] strip = { new Point(0, 1.2), new Point(0, 0.8), new Point(1, 0.9),
new Point(1, 1.1), new Point(2, 1.3), new Point(2, 0.9) };
Pair res = Points.closestInStrip(strip, 1.0);
Pair exp = new Pair(new Point(1, 0.9), new Point(1, 1.1));
assertEquals(exp, res);
}
private boolean same(Point[] as, Point[] bs) {
return new HashSet<>(Arrays.asList(as)).equals(new HashSet<>(Arrays.asList(bs)));
}
@Test
public void testPointEquals() {
Point a = new Point(0.0, 2.0);
Point b = new Point(2, 0);
Point c = new Point(0.0, 2.0);
assertTrue(a.equals(c));
assertTrue(c.equals(a));
assertFalse(a.equals(b));
assertEquals(a, c);
assertEquals(c, a);
assertNotEquals(a, b);
}
@Test
public void testDistFromX() {
Point[] points = { new Point(-1, 0), new Point(0,0), new Point (-1, 1), new Point(0.5, 1),
new Point(1, 0), new Point(1, -1) };
Arrays.sort(points, Point.COMPARE_X);
Point[] expected1 = { new Point(0,0), new Point(0.5, 1),
new Point(1, 0), new Point(1, -1) };
// mid is (0.5,1); xdist strictly less than
Point[] result = Points.midStrip(points, 3, 1.0);
assertTrue(same(expected1, result));
// mid is (0.5,1)
Point[] expected2 = { new Point(0.5, 1) };
result = Points.midStrip(points, 3, 0.5);
assertTrue(same(expected2, result));
}
@Test
public void testClosestPairSimple() {
Point a = new Point(0, 0);
Point b = new Point(0, 3);
Pair res = Points.divideAndConquer(a, b);
assertEquals(new Pair(a, b), res);
Point c = new Point(3, 4);
res = Points.divideAndConquer(a, b, c);
assertEquals(new Pair(a, b), res);
}
}