我有点麻烦我有一个任务,要求我找到一个第二个圆是否重叠,内部或第二个圈子.但是,我无法检查重叠,如果第二个圆圈在第一个圆圈内.
(使用的变量是x1,x2,y1,y2,r1,r2,distance)
这是我有的:
if (distance > (r1 + r2)) { // No overlap System.out.println("Circle2 does not overlap Circle1"); } else if (distance <= Math.abs(r1 + r2)) { // Overlap System.out.println("Circle2 overlaps Circle1"); } else if ((distance <= Math.abs(r1 - r2)) { // Inside System.out.println("Circle2 is inside Circle1"); }
我担心的问题是重叠和内部检查,但我无法弄清楚如何正确设置它,所以我可以可靠地检查第二个圆是否在第一个.
解决方法
您只需在重叠之前检查内部的距离,则内部的距离为< =重叠距离
if (distance > (r1 + r2)) { // No overlap System.out.println("Circle2 does not overlap Circle1"); } else if ((distance <= Math.abs(r1 - r2)) { // Inside System.out.println("Circle2 is inside Circle1"); } else // if (distance <= r1 + r2) { // Overlap System.out.println("Circle2 overlaps Circle1"); }
根据Chris的意见修改答案