mercredi 27 janvier 2021

For loop repeats and generates more values than what is specified

I am trying to paste 3 rectangular images on a canvas. The goal is that the images are within the canvas and they do not overlap. For this, I decided to generate 3 two-tuple values that will serve as the locations of the xy coordinates of the image's top-left corner. Something like this:

locations_used = [(950, 916), (1097, 119), (1290, 526)]

What it instead does is that it repeats the first value 3x and then adds the 2 new values, giving me a total of 5 positions when I've specified 3. Like this:

[(950, 916), (950, 916), (950, 916), (1097, 119), (1290, 526)]

This is an MRE of my code:


    n = 3
    canvas_width = 500
    canvas_height = 300
    logo_width = 50
    logo_height = 30
    locations_used = []
    
    for i in range(0, n):
        
        logo_location_x, logo_location_y = logo_position(canvas_width, canvas_height, logo_width, logo_height)
        locations_used.append((logo_location_x, logo_location_y))

        for img in locations_used:
            if logo_location_x in range(img[0], logo_width) or logo_location_y in range(img[1], logo_height):
                pass
        else:
            while len(locations_used) < n:
                locations_used.append((logo_location_x, logo_location_y))

     print(len(locations_used))
     print(locations_used)
     print('\n')

The output:

5
[(950, 916), (950, 916), (950, 916), (1097, 119), (1290, 526)]



Aucun commentaire:

Enregistrer un commentaire