private Coord nearestPoint(Coord point) { if (distribution.isEmpty()) { Coord center = Coord.get(width / 2, height / 2); distribution.add(center); return center; } Coord nearestPoint = distribution.get(0); double nearestDistance = point.distance(nearestPoint); for (Coord candidatePoint : distribution) { double candidateDistance = point.distance(candidatePoint); if (candidateDistance > 0 && candidateDistance <= maxDistance) { return candidatePoint; } if (candidateDistance < nearestDistance) { nearestPoint = candidatePoint; nearestDistance = candidateDistance; } } return nearestPoint; }
/** * Creates a pip that falls within the required distance from the current * distribution. Does not add the pip to the distribution. * * @return the created pip */ public Coord createPoint() { Coord randomPoint = randomPoint(); Coord nearestPoint = nearestPoint(randomPoint); double pointDistance = randomPoint.distance(nearestPoint); // Too close, toss while (pointDistance < minDistance) { randomPoint = randomPoint(); nearestPoint = nearestPoint(randomPoint); pointDistance = randomPoint.distance(nearestPoint); } // Adjust if we're too far if (pointDistance > maxDistance) { // Calculate unit vector double unitX = (randomPoint.x - nearestPoint.x) / pointDistance; double unitY = (randomPoint.y - nearestPoint.y) / pointDistance; randomPoint = Coord.get( (int) (rng.between(minDistance, maxDistance + 1) * unitX + nearestPoint.x) , (int) (rng.between(minDistance, maxDistance + 1) * unitY + nearestPoint.y)); } return randomPoint; }
double dist = lc.distance(rc2); if(dist < 0.001) continue;