Skip to main content
GameDev.net gamedev.net
🔒 Locked

Points Distance from Center of Circle

Started by Rhaal Mar 30, 2009 at 7:16 PM 4 replies 1.6k views
Original Post
Rhaal
Rhaal
I'm having a hard time figuring out an equation, and even then figuring out how to use it :( Let's say I have a circle and the diameter is 200 pixels. I render this circle at 0,0 in my window. Thus, the center is at 100, 100. When the mouse is on this circle, I want to know how far away from the center the cursor currently is. I found this equation:
sqrt((x-x1)^2 + (y-y1)^2)
In my c++ code, I write this as follows:
m_fDistanceFromCenter = sqrt(pow(m_fMouseX - fCenterX, 2) + pow(m_fMouseY - fCenterY, 2));
However, sometimes the distance is 0 even when I'm clearly not pointing at the center. Other times, the number is about 10 digits long. First question: What am I doing wrong to get 0 sometimes? Second question: How do I convert the 10 digit string to a ratio from 0.0 to 1.0? Any insight is greatly appreciated!
- A momentary maniac with casual delusions.
BornToCode
BornToCode
This is an quick example of how you can do it


float x = mousex-centerx
float y = mousey-centery;
float distance = sqrtf((x*x)+(y*y));

you centerx and centery should be whatever the position of the sphere+center

so if your sphere is at position 100,50 and you Radius is 100 as well
the centerx would be 200 in that case and centery would be 150.

I hope that helps.
MattWorden
MattWorden
How are you getting your Mouse X & Y? Are you sure it is tied to the client area that you are displaying yoru circle in and that it's using the same scale (pixels with 0,0 in the upper-left)?

To convert your distance to a range of 0.0 (center) to 1.0 (edge of the circle), divide your calculated distance by the radius of your circle.

HTH,
-Matt
www.mwgames.com - my game projects websiteNimble2D Blog - Simple 2D Game Dev with VB.Net
ROBERTREAD1
ROBERTREAD1
the ratio should be ((dx*dx)+(dy*dy))/(r*r)

no need for a sqrt

Bob Janova
Bob Janova
Your equation looks right. Make sure those variables hold what you think they do. The result should be of the same order of magnitude as the inputs (hundreds or thousands in this case), but the intermediate values will be larger (~107) – still not large enough to cause a problem with any sensible data type but make sure you aren't using 16 bit ints.

E: RobertRead, that is not correct, except for the special case of being on the circle. (I believe that your equation will give the proportion of area within the radius of the mouse – but the OP probably wants the proportion of linear distance for which he needs a square root.) If the OP is simply trying to check collisions then indeed, he does not need a square root.
Rhaal
Rhaal
Thanks guys, it works now :) Part of the problem was that I was using this API's (HGE) version of printf, and I used a %d instead of %f to output my distance. Dividing by the radius gave me my ratio.

I'm not doing collision detection, but rather trying to implement a color wheel.
- A momentary maniac with casual delusions.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.