Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Intersection points of two circle

807591Mar 16 2008 — edited Mar 16 2008
HI there I have written this following code to find out the intersection points of two circles. It is working fine for the parameters like
circle_circle_intersection(1.0, 1.0, 1.0, 3.0, 3.0, 2.0);
however if I am providing parameters like
circle_circle_intersection(33641.0, 48759.0, 700.0, 38606.0, 49234.0, 550.0); it is just not working. It is not giving any error. it is not printing anything.
//**********************************************************************	
public	double calcHypot( double s1, double s2 ) {
		return Math.sqrt( s1 * s1 + s2 * s2 );
	}
	
//**********************************************************************

public	int circle_circle_intersection(double x0, double y0, double r0,
        double x1, double y1, double r1)
{
try{
double a, dx, dy, d, h, rx, ry;
double x2, y2;
double x3, y3, x3_prime, y3_prime;
System.out.println("From circle circle intersection");
/* dx and dy are the vertical and horizontal distances between
* the circle centers.
*/
dx = x1 - x0;
dy = y1 - y0;

/* Determine the straight-line distance between the centers. */
//d = sqrt((dy*dy) + (dx*dx));
d = calcHypot(dx,dy); // Suggested by Keith Briggs

/* Check for solvability. */
if (d > (r0 + r1))
{
/* no solution. circles do not intersect. */
return 0;
}
if (d < Math.abs(r0 - r1))
{
/* no solution. one circle is contained in the other */
return 0;
}

/* 'point 2' is the point where the line through the circle
* intersection points crosses the line between the circle
* centers.  
*/

/* Determine the distance from point 0 to point 2. */
a = ((r0*r0) - (r1*r1) + (d*d)) / (2.0 * d) ;

/* Determine the coordinates of point 2. */
x2 = x0 + (dx * a/d);
y2 = y0 + (dy * a/d);
System.out.println(x2);
/* Determine the distance from point 2 to either of the
* intersection points.
*/
h = Math.sqrt((r0*r0) - (a*a));

/* Now determine the offsets of the intersection points from
* point 2.
*/
rx = -dy * (h/d);
ry = dx * (h/d);

/* Determine the absolute intersection points. */
x3 = x2 + rx;
x3_prime = x2 - rx;
y3 = y2 + ry;
y3_prime = y2 - ry;

System.out.println(x3+"  "+ y3+"  "+  x3_prime+"  "+  y3_prime);

return 1;
}catch(Exception e){e.printStackTrace(); return 1000;}


}	
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 13 2008
Added on Mar 16 2008
3 comments
200 views