-
point lies in circle?
How would I best test if a selected point is inside a circle, I have the following three (semi-hackish) ideas:
1) Very bad: Draw circle onto a surface which is painted color A, fill with color B, plot point in color C. Go through entire bitmap looking for C's location, etc.
2) Alright: Decrease radius of circle in for loop and test if point lies on circle.
3) find all points of circle and draw lines to them from center point (is this realistic)
I have code for #2 here, but it doesn't seem to work.
PHP Code:
bool cSChicken::inRadius(int bx, int by)
{
int r = radius;
for(r=radius; r >= 0; r--) //shrink circle down to a single point one point at a time
if( pow(bx-cx,2) + pow(by-cy,2) == pow(r,2) ) //if point lies on circle
return true;
return false;
}
cx and cy are the center coords of the circle, radius is (duh) the predefined radius of the circle.
What else could I do? What is wrong with the code I wrote for #2? Has anyone ever had to do this for something they were writing?
-
Well, i dont think i'd use any of those methods!! :)
If you have a point in the circle, then you know the x and y coordinates. and you obviously know the centre coordinates of the circle, and the radius....
lets call these
mx, my (mouse coordinates where the user clicked)
cx, cy (centre of circle)
radius (radius of the circle)
all you need to do is get the distance of the mouse click from the centre and test if it's greater than the radius, if it is, then it's not in the circle.
distance = sqrt((mx - cx) * (mx - cx) + (my - cy) * (my - cy));
if(distance > radius)
{
not in circle
} else {
in the circle
}
hope this helps
U.
-
thanks so much!
thanks, that is a much better method than i had, it seems to be working real well now : )
-