Здесь a сгенерировал начальную точку и оттуда попытался сгенерировать следующую в евклидовом расстоянии, используя случайную функцию.
import random
import math
import matplotlib.pyplot as plt
euclideanR = 5
NUMPOINTS = 1000
SPACEMIN = -30
SPACEMAX = 30
# Euclidean distance
def distance(p, q):
return math.sqrt((q[0]-p[0])**2 + (q[1]-p[1])**2)
# try new point
def calc_new(points):
x1 = points[0]
y1 = points[1]
# help the function place the next point
x2 = x1-2*abs(euclideanR)*random.random()+euclideanR
y2 = y1-2*abs(euclideanR)*random.random()+euclideanR
return (x2,y2)
# generate first point in this space range
x0 = random.randint(SPACEMIN, SPACEMAX)
y0 = random.randint(SPACEMIN, SPACEMAX)
points2D = [(x0, y0)]
dist_list = []
while len(points2D) < NUMPOINTS:
q = random.choice(points2D)
p = calc_new(q)
d = distance(q, p)
if d < euclideanR:
dist_list.append(d)
points2D.append(p)
print("Num points = {}".format(len(points2D)))
print("Minimal Euclidean Distance = {}".format(min(dist_list)))
print("Maximum Euclidean Distance = {}".format(max(dist_list)))
x, y = list(zip(*points2D))
fig = plt.figure()
plt.scatter(x, y)
plt.title("distanceR = {}, #points = {}, Init_space=[{},{}]".
format(euclideanR, NUMPOINTS, SPACEMIN, SPACEMAX))
plt.show()
Выход
Num points = 1000
Minimal Euclidean Distance = 0.22933741053890785
Maximum Euclidean Distance = 4.999991431695809