[英]Calculate x2 if we know distance, x1, y1, y2
How can I calculate x1 in Java if I know the distance, x2, y1 and y2 of two points. 如果我知道两点的距离x2,y1和y2,如何在Java中计算x1。
I made a picture to be easier to understand: 我做了一张图片以便于理解:
If we know the x1, x2, y1, y2 is easy to calculate the distance. 如果我们知道x1,x2,y1,y2很容易计算距离。 But if we know the distance?
但是,如果我们知道距离? How can I calculate with delta?
如何计算增量?
Note that there are usually two solutions to this; 请注意,通常有两种解决方案。 corresponding to essentially what is a 'reflected' solution.
基本上对应于“反射”解决方案。
if d
is the distance, then x2
is at 如果
d
是距离,则x2
在
x1 +/- sqrt(d * d - (y2 - y1) * (y2 - y1));
Where +/-
means "plus or minus". 其中
+/-
表示“正负”。 That is to repeat, x1
can be in two different places. 重复一遍,
x1
可以位于两个不同的位置。
d^2 = (x2-x1)^2 + (y2-y1)^2
(x2-x1)^2 = d^2 - (y2-y1)^2
x2-x1 = +- sqrt(d^2 - (y2-y1)^2)
x2 = x1 +- sqrt(d^2 - (y2-y1)^2)
If you know the distance, you can use it's formula: 如果您知道距离,则可以使用以下公式:
d = sqrt(sqr(x2 - x1) + sqr(y2 - y1))
With that, you can re-arrange it to find x1 using simple algebra, but I'll let you figure that out. 这样,您可以使用简单的代数将其重新布置为x1,但我会让您知道。
First, you need to solve for x2: 首先,您需要解决x2:
d = sqrt((x1-x2)^2+(y1-y2)^2)
d^2 = (x1-x2)^2+(y1-y2)^2
d^2-(y1-y2)^2 = (x1-x2)^2
sqrt(d^2-(y1-y2)^2) = (x1-x2)
x2 = x1-sqrt(d^2-(y1-y2)^2)
Now, it's just a matter of expressing this in java: 现在,只需在Java中表达这一点即可:
double x2 = x1 - Math.sqrt(Math.pow(d, 2) - Math.pow(y1 - y2, 2));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.