You handle this by calculating the distance between the two centers, D say. There is an intersection if
abs(R1-R2) < D < R1+R2
where R1 and R2 are the radii of two circles.
The first abs(R1-R2) < D test abs(R1-R2) < D handles the case when one center of the circle is inside another. And the second test D < R1+R2 handles the case when no circle contains another center.
So, adapting our code, we have:
private bool IsCircleCollision( int x1, int y1, int radius1, int x2, int y2, int radius2) { int dx = x2 - x1; int dy = y2 - y1; double D = Math.Sqrt(dx*dx + dy*dy); return Math.Abs(radius1-radius2)<D && D<radius1+radius2; }
If performance is important here, you can do without calling Math.Sqrt as follows:
private bool IsCircleCollision( int x1, int y1, int radius1, int x2, int y2, int radius2) { int dx = x2 - x1; int dy = y2 - y1; int Dsqr = dx*dx + dy*dy; int rdiff = Math.Abs(radius1-radius2); int rsum = radius1+radius2 return rdiff*rdiff<Dsqr && D<rsum*rsum; }
source share