# Thread: circle and conditionals

1. ## circle and conditionals

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

2. 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

3. 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?

4. 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

5. Originally Posted by MathFan
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
I think that might work, now i have to think how to implement it, but that gives me an idea.

Thanks

6. correct me if I am mistaken
You're almost right.

You don't necessarily need to calculate the vector though.

This condition:

Code:
`if (x)^2+(y)^2<= (r)^2`
Where 'x' and 'y' represents the coordinates of a point is sufficient
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....
Code:
```[1]
[3]```
This can be obtained by:
Code:
`{x-1}^2 +{y-3}^2 <=r^2`
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.

7. 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

8. MathFan's explanation is complete and accurate
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.

Nevertheless, however, you wish to continue is up to you. Both methods work fine. It's just that mine has slightly fewer steps.

Popular pages Recent additions