Mar*_*han 27 java math distance
好的,所以我编写了大部分程序,可以让我确定两个圆是否重叠.
除了一个问题,我的程序没有任何问题:程序不会接受我为两个中心点之间的距离编写的代码.我可以弄清楚if/else逻辑告诉用户取决于稍后的距离值会发生什么,但我想知道现在有什么问题.Eclipse,我编写的程序,告诉我应该将距离解析为数组,但我已经告诉过你它是一个int.
这是我的代码:
package circles;
import java.util.Scanner;
public class MathCircles {
// variable for the distance between the circles' centers
public static int distance;
// variable for the lengths of the radii combined
public static int radii;
public static void main(String[] args) {
// Get the x-value of the center of circle one
System.out.println("What is the x-coordinate for the center of circle one?");
Scanner keyboard = new Scanner(System.in);
int x1 = keyboard.nextInt();
//Get the y-value of the center of circle one
System.out.println("What is the y-coordinate for the center of circle one?");
Scanner keyboard1 = new Scanner(System.in);
int y1 = keyboard1.nextInt();
//Get the radius length of circle one.
System.out.println("How long is circle one's radius?");
Scanner keyboard2 = new Scanner(System.in);
int r1 = keyboard2.nextInt();
// Get the x-value of the center of circle two.
System.out.println("What is the x-coordinate for the center of circle two?");
Scanner keyboard3 = new Scanner(System.in);
int x2 = keyboard3.nextInt();
//Get the y-value of the center of circle two.
System.out.println("What is the y-coordinate for the center of circle two?");
Scanner keyboard4 = new Scanner(System.in);
int y2 = keyboard4.nextInt();
//Get the radius length of circle two.
System.out.println("How long is circle two's radius?");
Scanner keyboard5 = new Scanner(System.in);
int r2 = keyboard5.nextInt();
/*
* OK, so now I have the location of the two circles' centers,
* as well as the lengths of their radii.
* The circles are intersecting IF THE DISTANCE BETWEEN THE TWO CENTERS
* IS EQUAL TO OR LESS THAN THE COMBINED LENGTHS OF THE RADII.
* Now I need to get some math done.
*/
//calculate the combined lengths of the radii
radii = r1 + r2;
//calculate the distance
distance = Math.sqrt((x1-x2)(x1-x2) + (y1-y2)(y1-y2));
}
}
Run Code Online (Sandbox Code Playgroud)
Bob*_*Bob 60
根据@ trashgod的评论,这是计算距离的最简单方法:
double distance = Math.hypot(x1-x2, y1-y2);
Run Code Online (Sandbox Code Playgroud)
从文件中Math.hypot:
返回:
sqrt(x²+ y²)没有中间溢出或下溢.
us2*_*012 34
与maths-on-paper表示法不同,大多数编程语言(包括Java)都需要一个*符号来进行乘法运算.因此,您的距离计算应为:
distance = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
Run Code Online (Sandbox Code Playgroud)
或者:
distance = Math.sqrt(Math.pow((x1-x2), 2) + Math.pow((y1-y2), 2));
Run Code Online (Sandbox Code Playgroud)
小智 9
这可能是旧的,但这是最好的答案:
float dist = (float) Math.sqrt(
Math.pow(x1 - x2, 2) +
Math.pow(y1 - y2, 2) );
Run Code Online (Sandbox Code Playgroud)
小智 5
根据@trashgod的评论,这是计算>距离的最简单方法:
double distance = Math.hypot(x1-x2, y1-y2);从Math.hypot的文档中:返回:
sqrt(x²+ y²)没有中间上溢或下溢。鲍勃
在鲍勃(Bob)的认可评论下方,他说他无法解释
Math.hypot(x1-x2, y1-y2);
Run Code Online (Sandbox Code Playgroud)
做到了。解释一个三角形有三个边。使用两个点,您可以根据x,y每个点的长度找到这些点的长度。Xa=0, Ya=0如果考虑的直角坐标为(0,0),然后再按Xb=5, Yb=9,则直角坐标为(5,9)。因此,如果将它们绘制在网格上,则假设x到另一个x在相同的y轴上的距离为+5。假设它们在同一x轴上,沿Y轴到另一个的距离为+9。(认为数字线)因此三角形长度的一侧是5,另一侧是9。斜边是
(x^2) + (y^2) = Hypotenuse^2
Run Code Online (Sandbox Code Playgroud)
这是三角形剩余边的长度。因此与标准距离公式完全相同
Sqrt of (x1-x2)^2 + (y1-y2)^2 = distance
Run Code Online (Sandbox Code Playgroud)
因为如果您取消了操作左侧的sqrt,而改为使用distance ^ 2,那么您仍然必须从该距离获取sqrt。因此,距离公式是勾股定理,但在某种程度上,教师可以称其为混淆人的东西。
| 归档时间: |
|
| 查看次数: |
117248 次 |
| 最近记录: |