How can I calculate x1 in Java if I know the distance, x2, y1 and y2 of two points.
I made a picture to be easier to understand:
If we know the x1, x2, y1, y2 is easy to calculate the distance. 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
x1 +/- sqrt(d * d - (y2 - y1) * (y2 - y1));
Where +/-
means "plus or minus". That is to repeat, x1
can be in two different places.
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.
First, you need to solve for 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:
double x2 = x1 - Math.sqrt(Math.pow(d, 2) - Math.pow(y1 - y2, 2));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.