We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › intersection of two circles
Page Index Toggle Pages: 1
intersection of two circles (Read 1420 times)
intersection of two circles
Oct 8th, 2008, 9:40pm
 

I'm having a hard time making sense out of math websites, and haven't found any code for this.

Basically, what I want to do is draw the two arcs that form the intersection of two circles. Any pointers on how to compute the 2 intersection points would help.
Re: intersection of two circles
Reply #1 - Oct 8th, 2008, 11:58pm
 
// Select four points: center 1, radius 1, center 2, radius 2.
// The circles and their intersections are drawn,
// the coordinates of all points are printed to the console.
// Needs explanation????

int pointCounter;

int c1x,c1y;
int c2x,c2y;
float r1,r2;

void setup(){
 size(600,600);
 background(210,210,170);

 initVars();
}

void draw(){
 if(c1x<0){
   background(210,210,170);
 }
 else {
   if(c1x>=0){
     noStroke();
     fill(255,80,0);
     ellipse(c1x,c1y,5,5);
   }
   if(r1>0){
     stroke(0);
     noFill();
     ellipse(c1x,c1y,2*r1,2*r1);
   }
   if(c2x>=0){
     noStroke();
     fill(255,100,0);
     ellipse(c2x,c2y,5,5);
   }
   if(r2>0){
     stroke(0);
     noFill();
     ellipse(c2x,c2y,2*r2,2*r2);
     println("****");
     println("Center 1 : ("+c1x+", "+c1y+"), radius 1 : "+r1+ "; Center 2 : ("+c2x+", "+c2y+ ") radius 2 :"+r2);
     intersections();
   }    
 }
}


// compute and draw intersections
void intersections(){
 float d=dist(c1x,c1y,c2x,c2y); // distance between centers
 float base,h; // auxiliary distances
 //  p, middle point between q1 and q2
 // q1 dn q2 intersection points
 float px,py,q1x,q1y,q2x,q2y;

 stroke(255,0,0);
 noFill();
 line(c1x,c1y,c2x,c2y);

 if(d<abs(r1-r2) || d>r1+r2)
   println("C1 and C2 do not intersect");

 else
   if(d==abs(r1-r2)){ // one inside the other, intersect in one point
     if(r1>r2) base=r1;
     else base=-r1;

     px=c1x+base*(c2x-c1x)/d;
     py=c1y+base*(c2y-c1y)/d;

     noStroke();
     fill(255,0,100);
     ellipse(px,py,5,5);
     println("intersection: P=("+ px+ ", "+ py+ ")");

   }
   else
     if(d==r1+r2){ // outside each other, intersect in one point
       base=r1;

       px=c1x+base*(c2x-c1x)/d;
       py=c1y+base*(c2y-c1y)/d;

       noStroke();
       fill(255,0,100);
       ellipse(px,py,5,5);
       println("intersection: P=("+ px+ ", "+ py+ ")");

     }
   else{ // intersect in two points

     base=(r1*r1-r2*r2+d*d)/(2*d);
     h=sqrt(r1*r1-base*base);

     px=c1x+base*(c2x-c1x)/d;
     py=c1y+base*(c2y-c1y)/d;


     q1x=px+h*(c2y-c1y)/d;
     q1y=py-h*(c2x-c1x)/d;
     q2x=px-h*(c2y-c1y)/d;
     q2y=py+h*(c2x-c1x)/d;

     noStroke();
     fill(255,0,100);
     ellipse(px,py,5,5);
     ellipse(q1x,q1y,5,5);
     ellipse(q2x,q2y,5,5);
     println("intersections: Q1=("+ q1x+ ", "+ q1y+ ") and  Q2=("+q2x+ ", "+ q2y+")");
   }
 noLoop();
}


void initVars(){
 c1x=-1;
 c1y=-1;
 r1=0;
 c2x=-1;
 c2y=-1;
 r2=0;
 pointCounter=0;
}

void mousePressed(){
 // first point selected is the center of circle 1
 // second point selected is on circle 1
 // third point selected is the center of circle 2
 // fourth point selected is on circle 2
 loop();
 switch(pointCounter){
 case 0:
   c1x=mouseX;
   c1y=mouseY;
   pointCounter++;
   break;
 case 1:
   r1=dist(c1x,c1y,mouseX,mouseY);
   pointCounter++;
   break;
 case 2:
   c2x=mouseX;
   c2y=mouseY;
   pointCounter++;
   break;
 case 3:
   r2=dist(c2x,c2y,mouseX,mouseY);
   pointCounter++;
 }

}

void keyPressed(){
 loop();
 initVars();
}
Re: intersection of two circles
Reply #2 - Oct 9th, 2008, 12:22am
 
wow.. thanks laliki, this is awesome!..

there's not much to add, but here's my two cents, just to say I contributed something Wink

Code:

if(d<abs(r1-r2))
println("one circle is contained inside the other");
else if(d>r1+r2)
println("C1 and C2 do not intersect");
Re: intersection of two circles
Reply #3 - Oct 9th, 2008, 6:56am
 
steph,I actually posted code a waays back on this type of problem that might also be of some use:
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Syntax;action=display;num=1164344467

-ira
Re: intersection of two circles
Reply #4 - Oct 9th, 2008, 6:59pm
 
ira, thanks! I wonder how I didn't find that thread.(It's also good to know the geomerative library does intersections of polygons.)

I was gonna ask of the two algorithms which one is the most efficient, but then I noticed the code you posted at the time doesn't work if the circles have different sizes. I don't know how to fix it as I don't understand the logic behind it, or behind the other for that matter.
Re: intersection of two circles
Reply #5 - Oct 9th, 2008, 7:01pm
 
by the way, here's a snippet to draw the arcs of the intersection, could be useful for someone:

Code:

float i1Angle1 = atan2(q1y-c1y, q1x-c1x);
float i1Angle2 = atan2(q2y-c1y, q2x-c1x);
arc(c1x, c1y, r1*2, r1*2, i1Angle1, i1Angle2);

float i2Angle1 = atan2(q1y-c2y, q1x-c2x);
float i2Angle2 = atan2(q2y-c2y, q2x-c2x);
arc(c2x, c2y, r2*2, r2*2, i2Angle2, i2Angle1);
Page Index Toggle Pages: 1