Java如何定义三个圆_java – 以编程方式查找三个圆的交叉点

这篇博客介绍了如何用Java编程计算三个圆的交点。通过移植C代码并进行调整,实现了当给定三个圆的中心坐标和半径时,判断是否存在交点以及找出交点坐标的方法。文章中提供了详细的计算过程和代码实现,并给出了调用示例。
摘要由CSDN通过智能技术生成

你可以从

C code获得帮助.把它移植到JAVA不应该是具有挑战性的.说明是

here.搜索/滚动到:两个圆的交点

使用此方法,找到任意两个圆的交点..让我们说(x,y).现在,只有当中心与点x,y之间的距离等于r时,第三个圆将在点x,y处相交.

情况1)如果距离(中心,点)== r,则x,y是交点.

情况2)如果距离(中心,点)!= r,则不存在这样的点.

代码(从[这里!所有学分转移到原作者):

private boolean calculateThreeCircleIntersection(double x0, double y0, double r0,

double x1, double y1, double r1,

double x2, double y2, double r2)

{

double a, dx, dy, d, h, rx, ry;

double point2_x, point2_y;

/* 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 = Math.sqrt((dy*dy) + (dx*dx));

/* Check for solvability. */

if (d > (r0 + r1))

{

/* no solution. circles do not intersect. */

return false;

}

if (d < Math.abs(r0 - r1))

{

/* no solution. one circle is contained in the other */

return false;

}

/* '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. */

point2_x = x0 + (dx * a/d);

point2_y = y0 + (dy * a/d);

/* 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. */

double intersectionPoint1_x = point2_x + rx;

double intersectionPoint2_x = point2_x - rx;

double intersectionPoint1_y = point2_y + ry;

double intersectionPoint2_y = point2_y - ry;

Log.d("INTERSECTION Circle1 AND Circle2:", "(" + intersectionPoint1_x + "," + intersectionPoint1_y + ")" + " AND (" + intersectionPoint2_x + "," + intersectionPoint2_y + ")");

/* Lets determine if circle 3 intersects at either of the above intersection points. */

dx = intersectionPoint1_x - x2;

dy = intersectionPoint1_y - y2;

double d1 = Math.sqrt((dy*dy) + (dx*dx));

dx = intersectionPoint2_x - x2;

dy = intersectionPoint2_y - y2;

double d2 = Math.sqrt((dy*dy) + (dx*dx));

if(Math.abs(d1 - r2) < EPSILON) {

Log.d("INTERSECTION Circle1 AND Circle2 AND Circle3:", "(" + intersectionPoint1_x + "," + intersectionPoint1_y + ")");

}

else if(Math.abs(d2 - r2) < EPSILON) {

Log.d("INTERSECTION Circle1 AND Circle2 AND Circle3:", "(" + intersectionPoint2_x + "," + intersectionPoint2_y + ")"); //here was an error

}

else {

Log.d("INTERSECTION Circle1 AND Circle2 AND Circle3:", "NONE");

}

return true;

}

调用此方法如下:

calculateThreeCircleIntersection(-2.0, 0.0, 2.0, // circle 1 (center_x, center_y, radius)

1.0, 0.0, 1.0, // circle 2 (center_x, center_y, radius)

0.0, 4.0, 4.0);// circle 3 (center_x, center_y, radius)

此外,将EPSILON定义为适合您的应用程序要求的小值

private static final double EPSILON = 0.000001;

注意:也许有人应该测试并验证结果是否正确.我找不到任何简单的方法.我尝试过的基本案例的工作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值