convert bounding box coordinates to x,y pairs

498 Views Asked by At

I have a bounding box coordinates in this format [x, y, width, height],

how can I get all the x and y pairs from it?

the result is going to be in this format [(x1,y1),(x2,y2),...,(xn,yn)]

thanks in advance!

3

There are 3 best solutions below

0
Robert Haas On

I'm not sure if I understand your data description correctly, but here's an example that might fit:

data = [
    [1, 2, 100, 100],
    [3, 4, 100, 100],
    [5, 6, 200, 200],
]

result = [tuple(x[:2]) for x in data]

Result:

[(1, 2), (3, 4), (5, 6)]
0
Rithvik Kandula On

Is this what you mean?

data = [
[1, 2, 100, 100],
[3, 4, 100, 100],
[5, 6, 200, 200],
]

answer = []
for n in data:
  answer.append(n[0:2])

print(answer)
0
Ashutosh Pandey On

As per my understanding of your question here is the answer:

data = [1, 2, 100, 100]    ## x=1, y=2, width=100, height=100
coordinates = [[x, y], [x + width, y], [x + width, y + height], [x, y + height]]

Result with all 4 coordinates of the bounding box:

[[1, 2], [101, 2], [101, 102], [1, 102]]