Picture a circle. Now divide the circle vertically and horizontally into four regions. Take the top left region. If you drew a box around it, you'd have a box with a rounded corner heading east.
Given an X and Y coordinate in开发者_C百科 that box of that single top left region, how can I tell whether a point is to the left of the circle's line, or to the right?
You can solve the problem by realizing:
- the center of the circle is the southeast corner of the box
- the circle contains all points within a radius
r
of that point
So you can solve the problem, given the center of the circle at (a,b)
and knowing the dimensions of the square box having side r
...any given point (x,y)
inside the box is also inside the circle if and only if:
((b-y)^2 + (a-x)^2) < r^2
Such a point resides outside or exactly on the circle if and only if this condition is false.
As Derek E. suggests in a comment, when implementing this solution it's better to compare the squared distance to avoid the approximations of the sqrt function.
I was confused after the "draw a box" line.
I visualized your post as
; ______________
; | _ - |
; | . |
; | * | [Ascii art win]
; | . |
; | |
; |: |
; |______________|
;
What do you mean by rounded corner heading east? I'll ignore that line and try to continue...
If you have a point inside the circle and you wish to see if you're x/y is within the bounds, you could simply use the pythagorean theorem and test if the hypotenuse of your triangle is less than or equal to the circle's radius
Assume r = radius of your circle.
mouse positions are relative to center of circle
if(Math.Sqrt(mx^2+my^2) <= r); //in circle
else; //outside circle
Find the distance from the centre of the circle (bottom right corner of the box). If the distance is greater than the radius of the circle (which is equal to the edge size of the box), the point is outside the circle.
Pythagorean theorem. Take dx and dy as the difference between the point and the location of the point at the bottom right corner of the box. Take the square root of the sum of the squares of those values. The point is inside the circle iff that value is smaller than the height (or width) of the box.
well, if you know the point is in the box, its simple. if the point is in the circle, it is on the 'right' side, if it is outside the circle, it is on the 'left' side. at least in the case of the top left box.
this should be fairly obvious, but if you have trouble visualising it, draw the shape, place a point, then draw a line from the centre of the circle to the point, if it passes through the circle - the curved corner - then it is on the outside of the circle.
then your question simply becomes "how do I tell if a point is in a circle or not."
I don't think there is really any other way to go about solving it:
check if the point is in the box, then check if its in the circle (or visa-versa...)
is point in circle is simple enough, and has been answered several times above.
精彩评论