RandomPointFactory { public static Point getRandomPointWithDistance(Rectangle rectangle, List<Point> reference, int distance) { int i = 0; int max = 100000; if (rectangle == null) throw new NullPointerException("rectangle parameter cannot be null"); if (rectangle.getX() < 0 || rectangle.getY() < 0) { throw new IllegalArgumentException("X or Y coordinates cannot be negative"); } if (rectangle.getWidth() <= 0 || rectangle.getHeight() <= 0) { throw new IllegalArgumentException("Width or height cannot be zero or negative"); } int x; int y; Point temp = null; while (temp == null) { if (i >= Integer.MAX_VALUE / 2048) throw new RuntimeException("Cannot find random point in " + Integer.toString(max) + " attempts"); x = (int) (rectangle.getX() + rand.nextInt(rectangle.width)); y = (int) (rectangle.getY() + rand.nextInt(rectangle.height)); temp = new Point(x, y); for (Point p : reference) { if (p.distance(temp) < distance) { i++; temp = null; break; } } } return temp; } RandomPointFactory();  RandomPointFactory(int xSpacing, int ySpacing); static Point getFirstVChallengePointFromTopInRegion(Rectangle challengeRectangle, int challengeCount); Point getVChallengePointWrtPercentageSizeOfReferenceFont(Rectangle rectangle, Point referencePoint, Font referenceFont, Font nextFont); static Point getFirstHChallengePointFromLeftInRegion(Rectangle challengeRectangle, int challengeCount); Point getHChallengePointWrtPercentageSizeOfReferenceFont(Rectangle rectangle, Point referencePoint, Font referenceFont, Font nextFont); static Point getRandomPoint(Rectangle rect); static Point getRandomPointWithDistance(Rectangle rectangle, List<Point> reference, int distance); static final int DEFAULT_SPACING; static final int MAX_SPACING; }
@Test public void testGetRandomPointWithDistance() throws Exception { java.util.List<Point> lp = new ArrayList<>(); assertNotNull(rpf.getRandomPointWithDistance(testRectangle, lp, MAX * 2)); }
@Test public void testGetRandomPointWithDistanceShortDistance9999Points() throws Exception { java.util.List<Point> lp = new ArrayList<>(); testRectangle = new Rectangle(0, 0, 10, 10); Point p; for (int i = 0; i < 10 * 10 - 1; i++) { p = rpf.getRandomPointWithDistance(testRectangle, lp, 1); lp.add(p); } }
@Test(expected = RuntimeException.class) public void testGetRandomPointWithDistanceShortDistance10001Points() throws Exception { java.util.List<Point> lp = new ArrayList<>(); testRectangle = new Rectangle(0, 0, 10, 10); Point p; for (int i = 0; i < 10 * 10 + 1; i++) { p = rpf.getRandomPointWithDistance(testRectangle, lp, 1); lp.add(p); } }