Hi,
I'm trying to create a 2-Dimensional box in vpython and fill it with circles placed in random positions but the circles cannot overlap.

This is how far I have got, but I can't work out how to make the positions not overlap. I'm sure most of what I've done is wrong, I'm pretty much a complete beginner.

from visual import * #import libraries
from random import * 
#########################create box
thk = 0.3
side = 4.0
s2 = 2*side - thk
s3 = 2*side + thk
disks_radius = 0.3
maxpos=side-.5*thk-disks_radius
wallR = box (pos=vector( side, 0, 0), length=thk, height=s2, width=0, color = color.red)
wallL = box (pos=vector(-side, 0, 0), length=thk, height=s2, width=0, color = color.blue)
wallB = box (pos=vector(0, -side, 0), length=s3, height=thk, width=0, color = color.green)
wallT = box (pos=vector(0, side, 0), length=s3, height=thk, width=0, color = color.yellow)
##########################################################################################
scene.autoscale = 0 #disable auto zoom
no_particles=30 # choose number of particles
step = 0 # start number of trials
disks_list=[]
for i in range (1, no_particles): # create a list of disks
	disks = sphere(color=color.yellow, radius=disks_radius) # define attributes of the disks
	disks.pos=maxpos*vector(uniform(-1,1),uniform(-1,1)) # randomise disks initial positions
	disks_list.append(disks)
for j in disks_list:                                	#check for collisions with another disk
	if not i == j:				#
		distance=mag([j].pos-[i].pos)		#define distance from otherdisks
	while (distance<2*disks_radius):
		disks.x=disks.x + (random() -0.5) #Regenerate untill disks don't overlap
		disks.y=disks.y + (random() -0.5)

My advice, start out simple and make it work, then go more complex.

and remember, the use of functions to divide task eases troubleshooting i.e getting rid of bugs

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.