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

This is a discussion on *circle and conditionals* within the **C++ Programming** forums, part of the General Programming Boards category; so I've got a program that defines a circle, well,a lattice of circles.
Now i need to check whether a ...

- 04-26-2005 #1

- Join Date
- Apr 2005
- Posts
- 16

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

- 04-26-2005 #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 itThe OS requirements were Windows Vista Ultimate or better, so we used Linux.

- 04-26-2005 #3

- Join Date
- Jan 2005
- Posts
- 374

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?

- 04-26-2005 #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 mistakenThe OS requirements were Windows Vista Ultimate or better, so we used Linux.

- 04-26-2005 #5

- Join Date
- Apr 2005
- Posts
- 16

Originally Posted by**MathFan**

Thanks

- 04-26-2005 #6

- Join Date
- Jan 2005
- Posts
- 374

correct me if I am mistaken

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

This condition:

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

Code:[1] [3]

Code:{x-1}^2 +{y-3}^2 <=r^2

- 04-26-2005 #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.

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

- 04-27-2005 #8

- Join Date
- Jan 2005
- Posts
- 374

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.

- Exactly how to get started with C++ (or C) today
- C Tutorial
- C++ Tutorial
- 5 ways you can learn to program faster
- The 5 Most Common Problems New Programmers Face
- How to set up a compiler
- 8 Common programming Mistakes
- What is C++11?
- Creating a game, from start to finish

- How to create a shared library on Linux with GCC - December 30, 2011
- Enum classes and nullptr in C++11 - November 27, 2011
- Learn about The Hash Table - November 20, 2011
- Rvalue References and Move Semantics in C++11 - November 13, 2011
- C and C++ for Java Programmers - November 5, 2011
- A Gentle Introduction to C++ IO Streams - October 10, 2011