Howdy, Stranger!

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

Supported by

Random Position Generator

I have a grid of 100 images with specific x,y coordinates. I need to offset each image independently by a random amount between 0 and 50. What is the best way to go about this?

• Hi,
First part of the code makes a 10x10 grid of x,y positions. The second part draws circle on those positions.

``````import random
kb = Keyboard()

# make a grid
positions = []
x0,y0= 0,0
dim = 10
jitter = 50
cellwidth = 50
sx = x0-0.5*dim*cellwidth - 0.5*cellwidth
sy = y0-0.5*dim*cellwidth - 0.5*cellwidth
for xI in range(dim):
for yI in range(dim):
x= sx+ xI*cellwidth
y= sy+ yI*cellwidth
positions.append((x,y))

cv = Canvas()
for x,y in positions:
newx= x+random.random()*jitter
newy= y+random.random()*jitter
cv.circle(newx,newy,10,color='red',fill=True)
cv.show()
kb.get_key()
``````
• Hi Eduard, thank you so much, this definitely helps! Instead of drawing circles, what is the best way to achieve this using pictures in the library. I have multiple pictures to use and a target that is sometimes present, sometimes absent.

• Hi,

Does this part of the documentation help:
https://osdoc.cogsci.nl/3.2/manual/python/canvas/#function-canvas46image40fname-centertrue-xnone-ynone-scalenone-rotationnone41

Conceptually, using circles and images are handled the same way.

Best,

Eduard