Reputation: 3163
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)]
Upvotes: 0
Views: 45
Reputation: 999
In the first iteration, logo_location_x
(950) and logo_location_y
(916) are compared to range(950, 50)
and range(916, 30)
. Since start
parameter is smaller than stop
, range is empty and program procedes to else
clause. While length of locations_used
is under 3, same values will be added, thus making the array [(950, 916), (950, 916), (950, 916]
.
In the next two iterations, each new x, y
pair get added to locations_used
. While range(img[0], logo_width)
and range(img[1], logo_height)
are still empty, length of locations_used
is greater than n
so no additional element will be added.
This is edited code to create n
not overlaping positions.
# Instead of iterating only 3 times, try until list is full.
while len(locations_used) < n:
logo_location_x, logo_location_y = logo_position(canvas_width, canvas_height, logo_width, logo_height)
# Don't add position until it is checked that it doesn't overlap.
# locations_used.append((logo_location_x, logo_location_y))
# Check if new rectangle candidate overlaps with previous ones.
overlap = False
for img in locations_used:
# Fix range usage. I hope this is what you want.
if logo_location_x in range(img[0] - logo_width, img[0] + logo_width) and logo_location_y in range(img[1] - logo_height, img[1] + logo_height):
overlap = True
break
# Add position to list after you ensure that new position
if not overlap:
locations_used.append((logo_location_x, logo_location_y))
Upvotes: 1