...

View Full Version : Simple qn abt finding bounding box



laland
10-22-2006, 05:38 AM
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.

VortexCortex
10-22-2006, 06:52 AM
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...


<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>

laland
10-23-2006, 09:49 AM
Thanks for ur explaination :)
I roughly get the idea le ~~



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum