Nov 19

The boundingbox problem

Posted by arcturus

Again with google maps, now we are going to talk about the bounding box problem.

The google api provides the function containsBounds:

containsPoint(point)
Returns true if the rectangular area (inclusively) contains the pixel coordinates. (Since 2.88)

The problem is very simple, we wanna check points of interest inside the polygon, not the rectangular area that includes it.

boundingbox

We have solved this problem learning how to extend the GPolygon class with a method Contains that does what we want. You can see more examples of extending this class from this link.

Here is the code we used:

GPolygon.prototype.contains = function(point) { var j=0;

var oddNodes = false;

var x = point.lng();

var y = point.lat();

for (var i=0; i < this.getVertexCount(); i++) {

   j++;

   if (j == this.getVertexCount()) {j = 0;}   if (((this.getVertex(i).lat() < y)
         && (this.getVertex(j).lat() >= y))

         || ((this.getVertex(j).lat() < y) && (this.getVertex(i).lat() >= y))) {

              if ( this.getVertex(i).lng() + (y - this.getVertex(i).lat())

                   / (this.getVertex(j).lat()-this.getVertex(i).lat())

                  * (this.getVertex(j).lng() - this.getVertex(i).lng())>x){>

                       oddNodes = !oddNodes;

              }

    }

}

return oddNodes;

}

Of course you can test this code here and download it here.

5 Responses to “The boundingbox problem”

  1. palako Says:

    Hi there folks!

    I’ve known about your blog via Arcturus’ blog, and of course I had to get myself dropped by. Looks it’s going to be great! Congratulations, and I look forward to see also the others writing interesting stuff.

    This post almost drove me insane. There’s no way to understand this bloody algorithim. I’ve been reading it carefully for twenty minutes, trying to trace it by drawing on a piece of paper,… no way!

    So following the link on the article I downloaded the Epolys extension to GPolygon, and I found this funny way of giving references of the source for the algorithim:

    Algorithm shamelessly stolen from http://alienryderflex.com/polygon/

    lol! don’t miss the explanation, worth reading!!!

    Apart from that, is your post about trying to figure out if a point is inside any kind of polygon?, or just related to the circle? Shall this second be the case, wouldn’t it be much easier and faster to check that the euclidian distance between the point and the center of the circle is lesser than the radius?

    Take care!

  2. arcturus Says:

    Hi Pa!

    Hope see you tomorrow in the party!

    The porpouse of the post is find a algorithm that resolves the problem of the boundingbox with any kind of polygon.
    In the next examples we wanna draw a polygon rounding a path, so we need this solution.

    Another question, why don“t you enjoy us exploring the gmaps possibilities?

    See you soon man!

  3. code.nosvamosdetapas.com » Blog Archive » Drawing polygons with your mouse Says:

    […] slight modification to The bounding box problem will let us draw polygons (i.e. circles) centered anywhere on the map with arbitrary radius based […]

  4. code.nosvamosdetapas.com » Blog Archive » How far from my way? Says:

    […] about prototype to understand this) we have a GMap2 object, an array of points (of interest), an extension on the GPolygon object to determine if a Gpoint is contained in a polygon, and a piece of code we run on body load to put everything together and to add the listeners we are […]

  5. Polak Says:

    Hi!

    This function realy works!
    Small optimization, I’v add following at the beginning of function:
    if(!this.getBounds().containsLatLng(point)){
    return false;
    }
    Thanks a lot!

Leave a Reply

*
To prove you're a person (not a spam script), type the answer to the math equation shown in the picture. Click on the picture to hear an audio file of the equation.
Click to hear an audio file of the anti-spam equation