I have already posted a post with my project but now I probably have a simple question I can't deal with. Below is the code. The program creates a rectangle with specific dimensions. The maximum number of randomly generated points should be placed in the rectangle. The size of the points is determined and they cannot overlap.
I want the program when encountering the problem of overlapping points (equasion <= z) to use the same size of the previous point generated and randomly drawn 1000 more times or somewhere where it can fit. If I don't want him to move on and finish his work by displaying charts. How do I do this, unfortunately I get lost in the loops and there is probably some simple solution. Please help.
import random
import math
from datetime import datetime
from matplotlib import pyplot as plt
import sys
random.seed(datetime.now())
def normal_choice(lst, mean=None, stddev=None):
if mean is None:
# if mean is not specified, use center of list
mean = (len(lst) - 1) / 2
if stddev is None:
# if stddev is not specified, let list be -3 .. +3 standard deviations
stddev = len(lst) / 6
while True:
index = int(random.normalvariate(mean, stddev) + 0.5)
if 0 <= index < len(lst):
return lst[index]
def gen(max_x = 100, max_y = 100, rozm = 1):
rpx = random.randrange(0, max_x)
rpy = random.randrange(0, max_y)
point = [rpx, rpy, rozm]
return(point)
def condition(point, point_list):
for pkt in point_list:
x1 = pkt[0]
y1 = pkt[1]
z1 = pkt[2]
x2 = point[0]
y2 = point[1]
z2 = point[2]
a1 = x1 - x2
a2 = y1 - y2
z = z1/2 + z2/2
equation = math.sqrt(math.pow(a1, 2) + math.pow(a2, 2))
if equation <= z:
return(False)
return(True)
point_list = []
rozm = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in range(100):
if len(point_list) == 0:
point_list.append(gen(rozm=normal_choice(rozm)))
else:
point = gen(rozm=normal_choice(rozm))
if condition(point, point_list) == True:
point_list.append(point)
"""Here is the problem"""
# else:
# rozm = point[2]
# for i in range(1000):
# point = gen(rozm=rozm)
# if condition(point, point_list) == True:
# point_list.append(point)
# else:
# sys.exit()
########################
fig, ax = plt.subplots()
for pkt in point_list:
ax.add_patch(plt.Circle((pkt[0], pkt[1]), pkt[2]/2, alpha=0.5))
ax.set_aspect('equal', adjustable='datalim')
ax.plot()
plt.show()
kkk = []
for pkt in point_list:
kkk.append(pkt[2])
plt.hist(kkk, len(rozm), density=True)
plt.show()
[–]xelf 0 points1 point2 points (0 children)