How to get bounding box around objects?

839 Views Asked by At

I have coordinates for different geometric shapes like rhombus, polygon, rectangle and etc. I need exact coordinates for bounding box around them. My coordinates for points 1,2,3,4 & 5 can start from clockwise or anticlockwise. But bounding box should be accurate. How can I get the coordinates for the rectangle around the shapes? enter image description here

Best example I can give is the link below. Draw any shape on the map using polygon tool in this below link and you will get the rectangle around it. So I need coordinates for this rectangle. Link here

Example coordinates for 1 polygon:

(x1,y1) = (8.375, 127.5258)
(x2,y2) = (26.1326, 127.5258)
(x3,y3) = (26.375, 130.5258)
(x4,y4) = (23.6995, 141.2277)
(x5,y5) = (8.375, 137.3966)

1

There are 1 best solutions below

2
On

For an $n$-gone you have $$x_{\text{min}} = \min \{ x_k \mid k = 1, \ldots, n \}$$ and similar for $x_{\text{max}}, y_{\text{min}}, y_{\text{max}}, z_{\text{min}}, z_{\text{max}}.$

In pseudo-code:

x_min := +inf
x_max := -inf
y_min := +inf
y_max := -inf
z_min := +inf
z_max := -inf

for p in corners:
    x_max = max(x_max, p.x)
    x_min = min(x_min, p.x)
    y_max = max(y_max, p.y)
    y_min = min(y_min, p.y)
    z_max = max(z_max, p.z)
    z_min = min(z_min, p.z)
end for