I can't figure out how to find a distance between the co-ordinations.
Code: (it won't allow me to share it...)
package com.codegym.task.task06.task0609;
/*
Distance between two points
*/
public class Util {
public static double getDistance(int x1, int y1, int x2, int y2) {
//write your code here
return double Math.sqrt(double a);
}
public static void main(String[] args) {
}
}
How do I do this?!
Under discussion
Comments (5)
- Popular
- New
- Old
You must be signed in to leave a comment
Anonymous #11263139
9 April, 16:46

+2
Alan
31 December 2021, 19:35
explanation for the formula is here: https://www.mathsisfun.com/algebra/distance-2-points.html
+1
Nouser
25 September 2020, 17:25
a = x1 - x2 (order doesn't matter)
b = guess what?
c = sqrt(a*a + b*b)
now just put things together :)
+2
Maryem Vickers
28 September 2020, 16:18
Can you explain please?! Sorry.
0
Marex333
3 August 2021, 10:23
You operate on X and Y axis.
You need to calculate line between x1, y1 and x2, y2. Try to take paper and draw that and make such a points.
What you can do is to calulate horizontal line between points x1, x2 and vertical y1, y2. You will get "L" shape. If you would like to create triangle, the missing line ("L" shape") would be the line that you need for this task. All you need to do right now is to use pattern to find lenght of side of triangle.
It's a^2 + b^2 = c^
c = c^2 / c^2
SOLUTION
xDistance = x1 - x2
yDistance = y1 - y2
(you dont need to worry about getting minus, because you gonna make it square later = always positive)
resultSquare = xDistance * xDistance + yDistance + yDistance
result = resultSquare / resultSquare (in task you need to use method)
result = Math.sqrt(resultSquare)
+1