public class CircleMath { PVector inter0 = new PVector(); PVector inter1 = new PVector(); public CircleMath() { } public boolean intersection( Circle c0, Circle c1 ) { float h; //height from common center line to intersection float a; //length from circle0 center to float d; //distance between centers float dx; float dy; float rx; float ry; float rDiff; PVector p0 = c0.getCenter(); PVector p1 = c1.getCenter(); float r0 = c0.getRadius(); float r1 = c1.getRadius(); PVector p2 = new PVector(); dx = p1.x - p0.x; dy = p1.y - p0.y; d = sqrt(pow(( dx ), 2) + pow(( dy ), 2)); rDiff = r0 - r1; // println(sqrt(pow(r0 - r1, 2)) + " " + (r1 + r0) + " " + d); if( d > ( r1 + r0) || d < sqrt(pow(r0 - r1, 2)) || (d == 0 && r0 == r1 ) ) { return false; } a = (( r0 * r0 ) - ( r1 * r1) + ( d * d)) / ( d * 2 ); p2.x = p0.x + (a * dx) / d; p2.y = p0.y + (a * dy) / d; h = sqrt( (r0 * r0) - ( a * a) ); rx = -dy * ( h / d); ry = dx * ( h / d); inter0.x = p2.x + rx; inter0.y = p2.y + ry; inter1.x = p2.x - rx; inter1.y = p2.y - ry; return true; } public PVector getFirstIntersection() { return inter0; } public PVector getSecondIntersection() { return inter1; } }