Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  • #2
    Regular Coder
    Join Date
    May 2005
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool 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 06i++){
      
    addPoint(Math.floor(Math.random()*100), Math.floor(Math.random()*100));
    }

    var 
    min = [nullnull];
    var 
    max = [nullnull];
    for (var 
    0points.lengthi++){
      
    document.write("Point "+i+": ");
      for (var 
    02j++){
        
    document.writepoints[i][j] );
        if (
    == 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.writemax[0]+", "+max[1]+")<br />\n");
    document.write"Width:"+(max[0] - min[0])+"<br />\n" );
    document.write"Height:"+(max[1] - min[1]) );
    </script> 

  • #3
    New Coder
    Join Date
    Oct 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    •