Any one know how to check that wheter a mouse is clicked inside the circle or polygon. My problem is I want to check that if mouse has been clciked inside the circle or polygon. circle or polygon coordinates has been stored inside an array. Any help is really appreciated
5 Answers
As suggested by some other answers, I followed some links and found the c code here. Here is the JavaScript translation for finding whether a point is in a polygon
Copyright (c) 1970-2003, Wm. Randolph Franklin
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimers.
- Redistributions in binary form must reproduce the above copyright notice in the documentation and/or other materials provided with the distribution.
- The name of W. Randolph Franklin may not be used to endorse or promote products derived from this Software without specific prior written permission.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
function pnpoly( nvert, vertx, verty, testx, testy ) {
var i, j, c = false;
for( i = 0, j = nvert-1; i < nvert; j = i++ ) {
if( ( ( verty[i] > testy ) != ( verty[j] > testy ) ) &&
( testx < ( vertx[j] - vertx[i] ) * ( testy - verty[i] ) / ( verty[j] - verty[i] ) + vertx[i] ) ) {
c = !c;
}
}
return c;
}
nvert - Number of vertices in the polygon. Whether to repeat the first vertex at the end is discussed below.
vertx, verty - Arrays containing the x- and y-coordinates of the polygon's vertices.
testx, testy - X- and y-coordinate of the test point.
-
Thanks you so much for sending me this example. But i guess it is not working. Can you please test it & send me the tested code? Also please let me know the sigficance of nvert parameter – Dheeraj Feb 06 '10 at 17:34
-
2It works for me. If you follow my link to the page where I found this code, there is a full explanation of why things are done as they are and how to use this function – meouw Feb 06 '10 at 18:35
-
Hi Again... I am doing something like this // Poly Start var myArray2x = new Array(90, 640, 70,50); // Poly x axis var myArray2y = new Array(20, 25, 190,60) result = false result = pnpoly(myArray2x.length,myArray2x,myArray2y,mX,mY) if(result==true) { alert("Clicked inside the Polygon at x = " + mX + " and y = " + mY); } // Poly End – Dheeraj Feb 07 '10 at 06:47
-
function pnpoly( nvert, vertx, verty, testx, testy ) { alert('Inside Funtion'); var i, j, c = false; for( i = 0, j = nvert-1; i < nvert; j = i++ ) { //alert( 'verty[i] - ' + verty[i] + ' testy - ' + testy + ' verty[j] - ' + verty[j] + ' testx - ' + testx); if( ( ( verty[i] > testy ) != ( verty[j] > testy ) ) && ( testx < ( vertx[j] - vertx[i] ) * ( testy - verty[i] ) / ( verty[j] - verty[i] ) + vertx[i] ) ) { c = !c; alert('Condition true') } } return c; } – Dheeraj Feb 07 '10 at 06:48
-
Request you to please let me kno what I am doing wrong here... I checke the article & explaination but still not able to figure out that what is wrong in my script... – Dheeraj Feb 07 '10 at 06:49
-
1Awesome.. thanks for that converting that code.. works a treat. – Duncan_m Oct 03 '12 at 04:21
-
I know this answer is from 10 years ago, but to point it out the link to the C code is now broken. – Daniel Lavedonio de Lima May 18 '20 at 05:20
For the circle case it is very easy, just just check if the distance from the point to the center is less than the radius:
function inside_circle(x, y, cx, cy, r) {
var dx = x - cx
var dy = y - cy
return dx*dx + dy*dy <= r*r
}
For the polygon, the easiest way is to imagine a line going straight up form the point. If this line crosses an odd number of polygon borders, your point is inside the polygon. (It would just cross one polygon border for a simple convex polygon)
You might also be able to find a third party geometry library, but it will likely take you more time than, than coding it yourself.

- 30,774
- 21
- 92
- 114
I'd have a look at the isPointInPath method.
It will require you to plot the path onto a 'canvas' element, but there's a good chance that you want to be doing that anyway to render it. If you don't need to render your polygon on a canvas you can create an invisible canvas element (create it but never add it to the DOM).
var canvas = document.getElementById('canvas'); // Or document.createElement('canvas');
var ctx = canvas.getContext('2d');
ctx.beginPath();
for (var i = 0; i < coords.length; i++) {
ctx.lineTo(coords[i].x, coords[i].y);
}
ctx.isPointInPath(50,50);
Assuming you have an array of coordinate objects with x and y properties on them the above code should tell you if the point (50, 50) lies within the bounds of your shape.

- 2,038
- 20
- 15
-
Is this soution responsive? If I resize the browser window, will it still work accurately? – Mawg says reinstate Monica Feb 27 '17 at 13:53
-
@Mawg no, this operates on an array of coordinates for the shape and a point. It has no relation to layout. – Rachel K. Westmacott Feb 27 '17 at 20:30
-
Hnmmmm ... create a shadow copy of each path, catch resize events and scale the copy path accordingly, and when the user clicks X,Y, call `isPointInPath()` on the scaled copy? Would that work? – Mawg says reinstate Monica Feb 27 '17 at 20:36
-
1Sounds like it might, but it might not be the most elegant solution. Have you considered SVG - then you can attach listeners to mouse hover events. – Rachel K. Westmacott Feb 28 '17 at 11:25
-
That sounds like a very good idea. Why doesn't everyone use SVG for images which need to handle mouse clicks, on pages which need to be responsive to resizing? The first Google search result I looked at shows how easilly it works - https://www.tutorialspoint.com/svg/svg_interactivity.htm – Mawg says reinstate Monica Feb 28 '17 at 12:55
Circles are easy, just check that the distance from the point to the center of the circle is less than the radius of the circle using the Pythagorean theorem (see also this question).
Polygons are more challenging. That article links to C code to do it, which should be translate-able to JavaScript.

- 1
- 1

- 1,031,962
- 187
- 1,923
- 1,875
I assembled an example with the above function: http://jsfiddle.net/jcspader/Vz6ka/
var gDrawingContext = $("canvas")[0].getContext("2d");
gDrawingContext.beginPath();
gDrawingContext.arc(50, 50, 10, 0, Math.PI*2, false);
gDrawingContext.closePath();
gDrawingContext.strokeStyle = "red";
gDrawingContext.stroke();
gDrawingContext.beginPath();
gDrawingContext.arc(55, 55, 10, 0, Math.PI*2, false);
gDrawingContext.closePath();
gDrawingContext.strokeStyle = "blue";
gDrawingContext.stroke();
function intersects(x, y, cx, cy, r) {
var dx = x-cx
var dy = y-cy
return dx*dx+dy*dy <= r*r
}
console.clear();
$("canvas").on("click", function (e){
if (intersects(e.pageX, e.pageY, 55, 55, 10))
console.info(e.pageX + ", " + e.pageY );
});

- 349
- 4
- 7