Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Randomly sampling circle diameters with constraints

Hey all,

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
```

cheers,

Bugay

• Hi Bugay ,

Try this:

```import math
import random

prev_diameter = 0
neighbors = {0:[],
1:[0],
2:[1],
3:[2],
4:[3],
5:[0],
6:[1, 5],
7:[2, 6],
8:[3, 7],
9:[4, 8]}
# add the next two rows

diameters = dict()
for i in range(20):
# sample diameter
var.diameter = random.uniform(15, 35)
# check whether distance to
while True:
dia_foo = var.diameter
for dia in neighbors[i]:
if math.fabs(var.diameter - diameters[dia]) <= 5:
var.diameter = random.uniform(15, 35)
if dia_foo == var.diameter:
break

diameters[i] = var.diameter
#var.diameterTotal += var.diameter

print(-480 + (i % 5) * 65, -120 + 60 * (i//5), var.diameter)
#ensembleCanvas.circle(-480 + (i % 5) * 65, -120 + 60 * (i//5), var.diameter,fill=False)

print(i+1,". Circle: ", var.diameter)

```

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.

Good luck,

Eduard

(I haven't tested the code in Openseame, maybe there is a bug or so, but I am sure you get the idea)

• Hi Eduard,

Thanks so much, I think that'll do it!

cheers,

Bugay