so I've got a program that defines a circle, well,a lattice of circles.
Now i need to check whether a particular position is inside the circle or not. this is part of a bigger program.
Any ideas/suggestions on how to do this?
TIA
so I've got a program that defines a circle, well,a lattice of circles.
Now i need to check whether a particular position is inside the circle or not. this is part of a bigger program.
Any ideas/suggestions on how to do this?
TIA
well, something like this could work:
1. calculate vector between the given point and circle's center
2. if the length of that vector is less than circle's radius, it's inside it
The OS requirements were Windows Vista Ultimate or better, so we used Linux.
Recall the formula for a circle:
x^2+y^2=r^2...
'x' and 'y' and coordinates, 'r' is the radius?
so
if (x)2 + (y)2 <= r2 then coordinates lie within circle perhaps?
hmmm.... let's see:
first, your circle has a center in point (x0, y0) and has a radius of R; the mathimatical formula is then (x-x0)^2+(x-y0)^2=r^2
then you have a given point at (x1, y1); now if we want to test if it lies inside the circle, we do the following:
1. calculate the vector between the circle center and the point; it will look like: v=[x1-x0, y1-y0]
2. then we should calculate the length of our vector; that is: |v|=sqrt((x1-x0)^2+(y1-y0)^2)
3. now if |v| <= R then we have the point (x1, y1) inside the circle, otherwise it's outside
correct me if I am mistaken
The OS requirements were Windows Vista Ultimate or better, so we used Linux.
I think that might work, now i have to think how to implement it, but that gives me an idea.Originally Posted by MathFan
Thanks
You're almost right.correct me if I am mistaken
You don't necessarily need to calculate the vector though.
This condition:
Where 'x' and 'y' represents the coordinates of a point is sufficientCode:if (x)^2+(y)^2<= (r)^2
to test whether or not the point lies within the circle.
One other thing, the formula above is for a circle where the origin is at zero.
This means the coordinates can take on negative values and still lie within the circle-a trait which may not be desirable in your program.
In that case you may need to translate the circle onto the positive axis.Moving it to the right and upwards. Move it one unit to the right
and three units up....
This can be obtained by:Code:[1] [3]
or something like that. I'm not exactly sure since it was a while since I have done this. You can always google if you're unsure though.Code:{x-1}^2 +{y-3}^2 <=r^2
MathFan's explanation is complete and accurate. It' s general. This test, (x1-x0)^2+(y1-y0)^2 <= R^2, for special case of a circle with center in origin becoming x1^2+y1^2 <= R^2 which treenef suggested.
- Micko
Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem
Yes this is true, however, I was pointing out the fact that the same can be achieved without actually calculating the length of the vector.MathFan's explanation is complete and accurate
Nevertheless, however, you wish to continue is up to you. Both methods work fine. It's just that mine has slightly fewer steps.