hanna
hanna

Reputation: 33

Circle to circle intersection

I have searched Google and could find circle to circle intersection, but I did not find any way to tell if the intersection is from left, right, top, or bottom.

Can you please show me how is that done in C or C++?

I did try this, but the sides it output is wrong..

if ( ((dx*dx) + (dy*dy)) < radii * radii) {



    if (Circle2Position.x < Circle1Position.x) {
       // code |= LEFT;



    } else if (Circle2Position.x > Circle1Position.x+Circle1.width) {
       // code |= RIGHT;


    }

    if (Circle2Position.y < Circle1Position.y) {
       // code |= BOTTOM;


    } else if (Circle2Position.y  > Circle1Position.y  + Circle1.height) {

       // code |= TOP;


    }

        }

Upvotes: 1

Views: 478

Answers (1)

TheOperator
TheOperator

Reputation: 6516

This is rather a mathematical than a C++ specific problem. You can determine the quadrant using the vector v = c1 - c2 which is the difference between the two circle centers c1 and c2. Then, depending on the signs of v's X and Y coordinates, you can say in which direction a circle is relative to another.

In C++, it can make sense to use existing vector classes depending on the amount of geometry-related code. What to suggest depends a bit on your project and its field...

Upvotes: 1

Related Questions