I'm currently trying to teach myself Python and decided to write a dot placement function with some conditions. However, I'm facing some issues that I do not know how to solve yet since I'm still a bloody beginner.
The function is iterating through multiple steps and shall essentially distribute a certain number of points in a circle. But the points have a predetermined minimum distance to each other. My logic was to write a function that creates random coordinates in the range of the radius of the outer circle. In the next step, I then check whether the created dot lies in the circle at all. If it does not, a new dot should be generated. In case the dot is located within the circle, another function should then check if there are other dots close by. Therefore I create a list with all the "accepted" dots and check the distances of the new dot and the old dots. If there is no dot in the predetermined distance, the function should then append the dot coordinates to the list. Otherwise, a new dot has to be generated. This is supposed to create a list of 50 dots with X and Y coordinates.
This is how my code looks so far. It does not work yet and I also think it unnecessarily complicated, but I'm just learning!
import random, math
import numpy as np
#Define important variables
xPos = 0.0
yPos = 0.0
position_list = []
radiusVF = 57
size_obj = 1
required_dist = 5
#Function generating a random dot
def GenerateRandomPosition(radiusVF):
xPos = random.randint(-radiusVF,radiusVF)
yPos = random.randint(-radiusVF,radiusVF)
return xPos, yPos
#Function checking if dot is in outer circle
def InCircle(xPos, yPos, radiusVF, size_obj):
on_circle = (xPos- 0)**2 + (yPos-0)**2
if (radiusVF-size_obj)**2 >= on_circle:
print("Still in circle",on_circle, xPos, yPos )
position = [xPos, yPos]
return position
else:
print("Not in circle",on_circle, xPos, yPos )
return False
#Function checking if there are other dots around
def CheckSurrounding(position, size_obj, position_list):
X1 = position[0]
Y1 = position[1]
for elem in position_list:
X2 = elem[elem]
Y2 = elem[elem+1]
dist = (math.sqrt(X1-X2)**2 + (Y1-Y2)**2)
if dist <= required_dist:
return True
else:
return False
#Function appending the dot's coordinates to the list
def PlaceStimuli(position, position_list):
position_list.append(position)
####MAIN LOOP####
#create a first dot, so the list is not empty
GenerateRandomPosition(radiusVF)
xPos, yPos = GenerateRandomPosition(radiusVF)
if InCircle != False:
position = InCircle(xPos, yPos, radiusVF, size_obj)
PlaceStimuli(position, position_list)
#repeat the process until 50 dots are created
position_list = PlaceStimuli(position, position_list)
while len(position_list) <= 50:
GenerateRandomPosition(radiusVF)
xPos, yPos = GenerateRandomPosition(radiusVF)
InCircle(xPos, yPos, radiusVF, size_obj)
if InCircle != False:
position = InCircle(xPos, yPos, radiusVF, size_obj)
CheckSurrounding(position, size_obj, position_list)
if CheckSurrounding != False:
PlaceStimuli(position, position_list)
else:
continue
position_list = PlaceStimuli(position, position_list)
print(position_list)
Any feedback or help on how I could make this work is highly appreciated! I'm also open to new ideas of implementation if my ideas are not the best to solve this.
Thank you so so much in advance! I'm looking forward to reading your responses and improving my coding!
Aucun commentaire:
Enregistrer un commentaire