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?

Comments

  • 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.

Sign In or Register to comment.