java 圆的交点_如何检测圆与同一平面中任何其他圆之间的交点?

本文探讨了在JavaFX中检测两个圆是否相交的问题,指出一个普遍采用但存在缺陷的公式,并提供了有效解决方案。通过计算圆心之间的距离与半径的关系,确定两圆的交点情况。当距离大于两半径之和,表示圆分离;小于两半径之差,表示一圆包含另一圆;等于半径之和或圆心距为0且半径相等,表示两圆相切或重合。
摘要由CSDN通过智能技术生成

我尝试了这里给出的公式,这是一个假定的答案,每个人都投票了,虽然它有严重的缺陷 . 我在JavaFX中编写了一个程序,允许用户通过更改每个圆圈centerX,centerY和Radius值来测试两个圆是否相交,这个公式绝对不起作用,除了一种方式......我无法弄清楚为什么但是当我移动圆圈2靠近圆圈1它的工作原理但是当我将圆圈1移动到圆圈2附近的另一侧时它不起作用..... ?????这有点奇怪......认为需要以相反的方式测试公式,所以尝试了它并且它不起作用

if (Math.abs(circle1Radius - circle2Radius) <=

Math.sqrt(Math.pow((circle1X - circle2X), 2)

+ Math.pow((circle1Y - circle2Y), 2)) &&

Math.sqrt(Math.pow((circle1X - circle2X), 2)

+ Math.pow((circle1X - circle2Y), 2)) <=

(circle1Radius + circle2Radius)} {

return true;

} else {

return false;

}

这有效:

// dx and dy are the vertical and horizontal distances

double dx = circle2X - circle1X;

double dy = circle2Y - circle1Y;

// Determine the straight-line distance between centers.

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

// Check Intersections

if (d > (circle1Radius + circle2Radius)) {

// No Solution. Circles do not intersect

return false;

} else if (d < Math.abs(circle1Radius - circle2Radius)) {

// No Solution. one circle is contained in the other

return false;

} else {

return true;

}

使用的公式不是我的公式所有功劳都归功于Paul Bourke(1997年4月)

First calculate the distance d between the center of the circles. d = ||P1 - P0||.

If d > r0 + r1 then there are no solutions, the circles are separate.

If d < |r0 - r1| then there are no solutions because one circle is contained within the other.

If d = 0 and r0 = r1 then the circles are coincident and there are an infinite number of solutions.

Considering the two triangles P0P2P3 and P1P2P3 we can write

a2 + h2 = r02 and b2 + h2 = r12

Using d = a + b we can solve for a,

a = (r02 - r12 + d2 ) / (2 d)

It can be readily shown that this reduces to r0 when the two circles touch at one point, ie: d = r0 + r1

Solve for h by substituting a into the first equation, h2 = r02 - a2

So

P2 = P0 + a ( P1 - P0 ) / d

And finally, P3 = (x3,y3) in terms of P0 = (x0,y0), P1 = (x1,y1) and P2 = (x2,y2), is

x3 = x2 +- h ( y1 - y0 ) / d

y3 = y2 -+ h ( x1 - x0 ) / d

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值