 # Find Mouse Position Within Circle

• 07-30-2008, 07:10 PM
Andology
Find Mouse Position Within Circle
Hi Guys,

K, this is really doing me in, and i know it'll be easy for you geniuses ;)

I have a circle, of which the outer ring is a button, i need to know (as a percentage) where the mouse was clicked within that active area.

Here is an illustration:

http://www.andology.com/remote_image...ion_Circle.gif

The whole circle will be inside a clip and the actual circle may vary in size so ideally i need it to know it's own _width and tell me where the mouse clicked as a percentage between points A & B. Also, it must be able to work when clicking at any point in the circle (top, bottom, left, right etc.)

LOL, after 3 or 4 hours of trial & error trying it with pure Math, i was tempted to just say "F*** It" and make 100 individual progressively larger rings, make them buttons and name them 0-99 ;)

It seems like a really easy thing to do, but i am really struggling with it.

I would really appreciate any help.

Andy
• 07-30-2008, 08:03 PM
widged
The winded solution is about trying to find out the radius that crosses that point and then take the distance from the point to the limit of the circle.

But well, the length of the radius remains the same all over the circle, so all you need to do is compute the distance between your point and the center of the circle and compare it to the length of the radius ;-). (or in your case, the distance that corresponds to the radius of the outer circle - the radius of the inner one)

You will find plenty of information on how to compute the distance between two points on the web.

http://www.flzone.com/ShowDetail.asp?NewsId=3220

http://flaim.wordpress.com/2008/04/0...en-two-points/

http://blog.circlecube.com/2008/02/1...orean-theorem/
• 07-31-2008, 04:44 PM
Andology
Thanks for your response widged.. It helped in a way as the lack of clarity / specifics simply encouraged me to take another look at it to suss it out. ;)

FYI, this is how i did it:

Code:

```MousePos = Math.round(Math.sqrt(_xmouse*_xmouse+_ymouse*_ymouse)); InnerRad = InnerCircle._width/2; OuterRad = OuterCircle._width/2; WhatIWannaKnow = Math.floor(((MousePos-InnerRad)/(OuterRad-InnerRad))*100);```
(i have broken up the equasion for the purposes of clarity to help others trying to read / understand the code)

Thanks again dude...

Cheers

Andy
• 07-31-2008, 05:30 PM
widged
No worries, mate. Glad you got it sorted and thanks for sharing :-).
• 04-20-2010, 03:23 PM
Suthers
The formula for a circle:

(x-h)^2+(y-k)^2=r^2

(h,k) is the center

r is the radius

You want the equation for both circles, and you want to subtract the x and y values of each

The formula for your circles:

[[(x2-h)^2]-[(x-h)^2]] + [[(y2-k)^2]-[(y-k)^2]] = [r2^2-r^2]

(h,k) is the center

r is the radius of small circle
r2 is the radius of large circle
y2 is of large circle
y is of small circle
x2 is of large circle
x is of small circle

This should be the equation for any point between the large and small circle. The brackets will be parenthesis of course. You will have to use Math.pow() also.

h and k are the same for both circles (it is the center for both)