Return to Snippet

Revision: 50155
at August 12, 2011 19:41 by ichnoweb


Initial Code
function isPointInPoly(poly, pt)
		{
		    var i = 0;
			var j = 0;
			var c = false;
			for (i = 0, j = poly.length-1; i < poly.length; j = i++) {
			if ((((poly[i].y<=pt.y) && (pt.y<poly[j].y)) ||
				 ((poly[j].y<=pt.y) && (pt.y<poly[i].y))) &&
				(pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x))
			
			  c = !c;
			}
			return c;
		}

Initial URL
http://www.faqs.org/faqs/graphics/algorithms-faq/

Initial Description
Author: Wm. Randolph Franklin, rewritten for JS
poly {Array}: [{x1,y1},{x2,y2},...]
pt {Object}: {click.x,click.y}

Initial Title
Is point in polygon (ImageMap)

Initial Tags


Initial Language
JavaScript