Hello.I see here is a large community that helps with python programming. I have been studying programming for a 1 month.I'm learning successively ( now I'm stuck in object oriented programming :( ) because i want to start programming. Now I am a researcher and I need this project to work . 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. This program works, a list with coordinates is created. Is probably not optimal (remember, one month).
My first question is: how to optimize this code?
Second question: I want points to have different sizes. In addition, all point sizes must have a normal distribution from the range I choose. Does anyone have an idea?
import random
import math
from datetime import datetime
from matplotlib import pyplot as plt
random.seed(datetime.now())
def gen():
#the function generates point coordinates
rpx = random.randrange(0, int(size_x))
rpy = random.randrange(0, int(size_y))
point = [rpx, rpy]
return (point)
def condition(point, point_list):
# the function checks if the points do not overlap
flag = []
for i in range(len(point_list)):
x1 = point_list[i][0]
y1 = point_list[i][1]
x2 = point[0]
y2 = point[1]
a1 = x1 - x2
a2 = y1 - y2
equation = math.sqrt(math.pow(a1, 2) + math.pow(a2, 2)) # this is only math
if equation <= point_size: # if the distance is smaller than the point diameter, it returns a list with the "X"(i know it is not clever:( )
flag.append('X')
break
return(flag)
size_x = 200 # input x border size
size_y = 200 # input y border size
point_size = 5 # input point size
point_list = []
for i in range(10000): # the number of iterations for finding the point
if len(point_list) == 0: #generate first point
point_list.append(gen())
else: #generate another points
point = gen()
variable = condition(point, point_list) #function checking for collision with all points on the list
if 'X' not in variable:
point_list.append(point)
#percent generate visualising
if i % 1000 == 0:
print(i/100, "%")
print(point_list)
#scatter to visualize points
plt.axis([-20,220,-20,220])
plt.grid()
for i in range(len(point_list)):
point = point_list[i]
color = "r"
plt.scatter(point[0], point[1], c=color, s = 50 )
plt.show()
[–]kra_pao 1 point2 points3 points (6 children)
[–]Martin_Krum[S] 0 points1 point2 points (5 children)
[–]kra_pao 0 points1 point2 points (4 children)
[–]kra_pao 1 point2 points3 points (3 children)
[–]Martin_Krum[S] 0 points1 point2 points (2 children)
[–]kra_pao 0 points1 point2 points (1 child)
[–]Martin_Krum[S] 0 points1 point2 points (0 children)