Is point in polygon (ImageMap)


/ Published in: JavaScript
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. function isPointInPoly(poly, pt)
  2. {
  3. var i = 0;
  4. var j = 0;
  5. var c = false;
  6. for (i = 0, j = poly.length-1; i < poly.length; j = i++) {
  7. if ((((poly[i].y<=pt.y) && (pt.y<poly[j].y)) ||
  8. ((poly[j].y<=pt.y) && (pt.y<poly[i].y))) &&
  9. (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x))
  10.  
  11. c = !c;
  12. }
  13. return c;
  14. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.