I am trying to draw n transparent images on top of a canvas. The drawing needs to follow these 2 criteria:
- The images cannot overlap one another.
- The images cannot overflow the canvas' frame. They need to stay on the canvas.
The images are positioned by the x and y values of the top-left corner of the image. To make sure that the images do not leave the canvas' frame while randomly positioning it, I do this simple trick. I simply find random x and y values that are within the range 0 and the canvas' width/height minus the image width/height.
positions = []
image_x = random.randint(0, canvas_width - image_width)
image_y = random.randint(0, canvas_height - image_height)
positions.append((image_x, image_y))
I have to do this for all n images. I am saving their positions in a list to set them apart from the positions that I will randomly generate for the other images. My question is, how can I generate these random positions like above while they not only stay in the frame but also do not overlap one another?
Aucun commentaire:
Enregistrer un commentaire