Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Simple qn abt finding bounding box

1. ## Simple qn abt finding bounding box

Does anyone know the algorithm of finding the bounding box of a polygon?

The bounding box is the smallest rectangle whose sides are parallel to the x or y axis, and can completely contain the polygon.

Thanks.

• ## There are several ways to solve this problem...

If this is for a class, you teacher will know you've cheated if you use this...

Given that "points" is an array of arrays with 2 elements (X and Y), such that:
points[4][1] would yeild the Y coordinate of the 5th point in the polygon...
PHP Code:
``` <script type="text/javascript"> var points = new Array(); function addPoint(x,y){   points[points.length] = [x,y]; } // Call addPoint function repeatedly to populate the points array. for (var i = 0; i < 6; i++){   addPoint(Math.floor(Math.random()*100), Math.floor(Math.random()*100)); } var min = [null, null]; var max = [null, null]; for (var i = 0; i < points.length; i++){   document.write("Point "+i+": ");   for (var j = 0; j < 2; j++){     document.write( points[i][j] );     if (j == 0 ) document.write(", ");     if ((min[j] == null)||(points[i][j] < min[j])) min[j] = points[i][j];     if ((max[j] == null)||(points[i][j] > max[j])) max[j] = points[i][j];   }   document.write("<br />"); } document.write( "<hr />Bounding box coordinates:  ("+min[0]+", "+min[1]+")-(" ); document.write( max[0]+", "+max[1]+")<br />\n"); document.write( "Width:"+(max[0] - min[0])+"<br />\n" ); document.write( "Height:"+(max[1] - min[1]) ); </script>  ```

• Thanks for ur explaination
I roughly get the idea le ~~

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•