samedi 27 février 2021

Generating random dots within an outer circle that have a certain distance

I'm currently really stuck with some of my code and I can't seem to find the issue. Here is what I am trying to do: I have a big outer circle in which I want to display smaller dots. These dots should be randomly distributed but should not overlap, thus they should have a minimum distance to each other. What I have tried is to first randomly generate a point, check wether it is in the outer circle and if it is, append it to the final list of dot positions. Then another point is created, checked if in circle and then it should be checked if the dot has a minimum distance to the other dot(s) in the final list. However, I seem to have some issues with my code as it will not run through whenever I set the required distances higher than 1. I have changed multiple things, but I cannot make it work.

If anyone has an idea about what the problem might be or how I could better code this, I would be very very grateful (it is driving me nuts at this point).

Thank you so much in advance!

Here's what I have been trying:

import random
import numpy as np
import math

#Variables
radiusOC = 57
size_obj = 7
required_dist = 5
no_stimuli = 3


def CreatePos(radiusOC, size_obj, required_dist, no_stimuli):
    final_list = []
    def GenRandPos(radiusOC,size_obj):
        """
        Takes the radius of the outer circle and generates random dots within this radius. Then checks if the the dots are located 
        within the outer circle.
        """
        while True:
            xPos = random.randint(-radiusOC,radiusOC)
            yPos = random.randint(-radiusOC,radiusOC)
        
            # check if in Circle 
            on_circle = (xPos- 0)**2 + (yPos-0)**2
            if (radiusOC-size_obj)**2 >= on_circle:
                print("Still in circle",on_circle, xPos, yPos )
                position = [xPos, yPos]
                break
            else:
                print("Not in circle",on_circle, xPos, yPos )
                continue
                
        return position


    def CheckSurrounding(position, final_list, required_dist): 
        """
        Takes dot positions that are in the visual field, the list of positions, and the distances dots are required to have from each other. 
        It is checked if there are dots close by or not.  
        """
        X1 = position[0]
        Y1 = position[1]
        dist_list = []
        for elem in final_list:
            for i in elem: 
                X2 = elem[0]
                Y2 = elem[1]
                dist = math.sqrt((X1-X2)**2 + (Y1-Y2)**2)
                dist_list.append(dist)
                
        if all(dist_list) >= required_dist: 
            return position
            
        else:
            return None

    # append the first dot to the list
    position = GenRandPos(radiusOC, size_obj)
    final_list.append(position)

    # now append the rest of the dots if they have a certain distance to each other
    while len(final_list) < no_stimuli: 
        position = GenRandPos(radiusOC, size_obj)

        if CheckSurrounding(position, final_list, required_dist)  != None: 
            position = CheckSurrounding(position, final_list, required_dist)
            final_list.append(position)

        else: 
            continue
    
    return final_list


´´´



Aucun commentaire:

Enregistrer un commentaire