Randomly sampling circle diameters with constraints
I am using the following piece of code (not exactly the same but a very similar one) to draw 20 circles, 5 per row and 4 rows in total. What I want to do is to apply a constraint to make sure that any neighbouring circles are not too big or too small. This is in order to prevent any unwanted perceptual groups to occur. The range we are randomly selecting the diameters (15 to 35) will stay the same, but say, the minimum absolute difference between any two neighbouring circles' diameters will never be smaller than 5. For example for the 3rd circle in first row, its diameter should differ at least 5 units from the 2nd, 4th (ones on two sides) and also 8th (the one right below it) circles' diameters. I feel like there's quite an easy way to implement this but I don't know how.
Any suggestions are welcome!
while i<20: var.diameter = random.uniform(15,35) var.diameterTotal += var.diameter if i <5: ensembleCanvas.circle(-480+i*65,-120,var.diameter,fill=False) elif i>=5 and i<10: ensembleCanvas.circle(-480+(i-5)*65,-180,var.diameter,fill=False) elif i>=10 and i<15: ensembleCanvas.circle(-480+(i-10)*65,-240,var.diameter,fill=False) elif i>=15 and i<20: ensembleCanvas.circle(-480+(i-15)*65,-300,var.diameter,fill=False) print(i+1,". Circle",var.diameter) i+=1
Hi Bugay ,
Essentially, keep a dictionary with the neighbouring indices of the previous circles, and a dictionary of their diameters, and keep on sampling new diameters until requirements are met.
I also shortened the code a bit for efficiency's sake. Hope you don't mind.
(I haven't tested the code in Openseame, maybe there is a bug or so, but I am sure you get the idea)
Thanks so much, I think that'll do it!