Find the distance between 2D coordinates (x, y) and the origin (0, 0).
System.out .println(Math.hypot(3, 4));System. out .println(Math.hypot(1, 1));System. out .println(Math.hypot(1, 10)); |
The hypot() method computes the length of the hypotenuse of a right triangle, representing the distance between a 2D point (x, y) and the origin (0, 0).
It’s essentially equivalent to Math.sqrt(x * x + y * y), but it’s optimized to avoid potential overflows and underflows that may occur during intermediate calculations like addition and multiplication.
public static double hypot(double x, double y) |
Parameter |
Description |
x |
Mandatory. The x-coordinate of a point. |
y |
Necessary. The y-coordinate of a point. |
Return |
A double value indicating the distance from a point (x, y) to the origin (0, 0). |
Java Version |
1.5+ |