def place_robots_fast(n): data = np.zeros((n,2), int) data[0,0], data[0,1] = generate_first_robot(n) for i in range(n): pivot = choice(data) while True: xmin, ymin = pivot[0] - MAX_DISTANCE, pivot[1] - MAX_DISTANCE xmax, ymax = pivot[0] + MAX_DISTANCE, pivot[1] + MAX_DISTANCE p = generate_point(xmin,ymin,xmax,ymax) if distance(p, pivot) <= MAX_DISTANCE: break data[i,0], data[i,1] = p return data
def place_robots_slow(n): """ Slower, but gives a more spread distribution """ data = np.zeros((n,2), int) data[0,0], data[0,1] = generate_first_robot(n) # Keeps the coordinates of the box # in which potential points may lie min_x, min_y = np.amin(data, axis=0) max_x, max_y = np.amax(data, axis=0) for i in range(1, n): while True: p = generate_point_boxed(min_x, min_y, max_x, max_y) nearest = nearest_neighbour(data[:i], p) if distance(p, nearest) <= MAX_DISTANCE: break data[i,0], data[i,1] = p min_x, min_y = np.amin(data, axis=0) max_x, max_y = np.amax(data, axis=0) return data
def nearest_neighbour(points, p): """ Returns the nearest number of p in from aset of points """ index = np.argmin([distance(p,x) for x in points]) return points[index]