如何在Java中检查两个给定的圆是否相切或相交?
圆是通过追踪在平面上移动的点而形成的闭合形状,使得它与给定点的距离恒定。在本文中,我们将检查两个给定的圆是否相互接触或相交。
我们将得到两个圆,其中心为 1,即 (x1, y1),中心为 2,即 (x2,y2),半径为 R1 和 R2。我们需要检查给定的圆是否与另一个圆碰撞,因此会出现以下五种可能的情况 -
圆 2 位于圆 1 内
圆 1 位于圆 2 内
圆 1 和圆 2 相交
圆 1 和圆 2 相互接触
圆 1 和圆 2 不重叠
现在为了检查上述条件,我们将找到中心 1 和中心 2 之间的距离,并将其命名为“d”。
现在,
1。如果 d
2.如果 d
3.如果 d
4。如果 d == R1 + R2:圆 1 和圆 2 互相接触
5。否则,圆 1 和圆 2 不重叠
“d”可以使用公式找到 -
$$mathrm{d:=:sqrt((x1:–:x2)^2:+:(y1:–:y2)^2}$$
开始吧!
向您展示一些实例
实例1
“d”的给定输入为 -
中心 1 = (9, 3),中心 2 = (11, 1),R1 = 5,R2 = 4。
求出“d”的值后,结果将是 -
圆 1 和圆 2 相交
实例2
“d”的给定输入为 -
中心 1 = (5, 8),中心 2 = (9, 11),R1 = 20,R2 = 40。
求出“d”的值后,结果将是 -
圆 1 位于圆 2 内
算法
Step-1 - 声明并初始化变量。
Step-2 - 找到圆的中心 1 和中心 2 之间的距离。
Step-3 - 检查距离的五个条件。
第 4 步 - 打印结果。
多种方法
我们通过不同的方式提供了解决方案。
通过使用静态输入
通过使用用户定义的方法
让我们一一看看该程序及其输出。
方法 1:使用静态输入
在此方法中,将分配半径 1 和半径 2、中心 1 和中心 2 的值来查找“d”。然后根据算法我们会发现这条线是否接触、相交或位于圆外。
示例
public class Main { //main method public static void main(String[] args){ //declaring variables int x1 = 9, y1 = 3; int x2 = 11, y2 = 1; int r1 = 5, r2 = 4; //finding d using the formula double d = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); if (d <= r1 - r2) { //print if Circle 2 lie inside circle 1 System.out.println("Circle 2 lie inside circle 1"); } else if (d <= r2 - r1) { //print if Circle 1 lie inside 2 System.out.println("Circle 1 lie inside 2"); } else if (d < r1 + r2) { //print if Circle 1 and 2 intersect each other System.out.println("Circle 1 and 2 intersect each other"); } else if (d == r1 + r2) { //print if Circle 1 and 2 touch each other System.out.println("Circle 1 and 2 touch each other"); } else { //print if Circle 1 and 2 do not touch each other System.out.println("Circle 1 and 2 do not touch each other"); } } }
输出
Circle 1 and 2 intersect each other
方法 2:使用用户定义的方法
在此方法中,将分配半径 1 和半径 2、中心 1 和中心 2 的值来查找“d”。然后通过传递给定值来调用用户定义的方法,根据算法我们将发现直线是否接触、相交或位于圆外。
示例
public class Main { //main method public static void main(String[] args){ //declaring variables int x1 = 5, y1 = 8; int x2 = 9, y2 = 11; int r1 = 20, r2 = 40; //calling user defined method func(x1, y1, x2, y2, r1, r2); } //user defined method static void func(int x1, int y1, int x2, int y2, int r1, int r2){ //finding d using the formula double d = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); if (d <= r1 - r2) { //print if Circle 2 lie inside circle 1 System.out.println("Circle 2 lie inside circle 1"); } else if (d <= r2 - r1) { //print if Circle 1 lie inside 2 System.out.println("Circle 1 lie inside 2"); } else if (d < r1 + r2) { //print if Circle 1 and 2 intersect each other System.out.println("Circle 1 and 2 intersect each other"); } else if (d == r1 + r2) { //print if Circle 1 and 2 touch each other System.out.println("Circle 1 and 2 touch each other"); } else { //print if Circle 1 and 2 do not touch each other System.out.println("Circle 1 and 2 do not touch each other"); } } }
输出
Circle 1 lie inside 2
在本文中,我们探索了使用 Java 编程语言查找两个给定圆是否相互接触或相交的不同方法。
以上是如何在Java中检查两个给定的圆是否相切或相交?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

胶囊是一种三维几何图形,由一个圆柱体和两端各一个半球体组成。胶囊的体积可以通过将圆柱体的体积和两端半球体的体积相加来计算。本教程将讨论如何使用不同的方法在Java中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4

Java是热门编程语言,适合初学者和经验丰富的开发者学习。本教程从基础概念出发,逐步深入讲解高级主题。安装Java开发工具包后,可通过创建简单的“Hello,World!”程序实践编程。理解代码后,使用命令提示符编译并运行程序,控制台上将输出“Hello,World!”。学习Java开启了编程之旅,随着掌握程度加深,可创建更复杂的应用程序。
