A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Find Mouse Position Within Circle

  1. #1
    Challenging Convention Andology's Avatar
    Join Date
    May 2001
    Location
    Cyprus
    Posts
    152

    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:



    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.

    Thanks in advance,

    Andy
    Last edited by Andology; 07-30-2008 at 07:15 PM.
    The Andology Corporation Ltd
    Challenging Convention
    Fusion of Business & Technology

    Joop Media - The Final Link In The Chain

  2. #2
    Member
    Join Date
    May 2008
    Location
    Wellington, NZ
    Posts
    35
    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/
    Last edited by widged; 07-30-2008 at 08:38 PM.

  3. #3
    Challenging Convention Andology's Avatar
    Join Date
    May 2001
    Location
    Cyprus
    Posts
    152
    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
    The Andology Corporation Ltd
    Challenging Convention
    Fusion of Business & Technology

    Joop Media - The Final Link In The Chain

  4. #4
    Member
    Join Date
    May 2008
    Location
    Wellington, NZ
    Posts
    35
    No worries, mate. Glad you got it sorted and thanks for sharing :-).

  5. #5
    Member
    Join Date
    Feb 2010
    Posts
    59
    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)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center