Java Math.atan
In Java, Math.atan returns the inverse tangent of a slope as an angle in radians. A slope of 1 gives π/4, or 45 degrees.
A simple example
Math.atan
- Start with Slope 0
- Do this Find the angle of a flat line
- Result 0 radians
class Main {
public static void main(String[] args) throws Exception {
System.out.println(Math.atan(0.0));
}
}
0.0
Syntax
Math.atan(slope)
JDK 21 method signatures (1)
public static double atan(double a)Run these examples in Java
Save one example at a time as Main.java. Use JDK 21.
java Main.javaTurn a ramp slope into an angle
A surveyor describes a platform or ramp using its rise and horizontal run. The drawing tool must convert between slope and angle, including the level case where the rise is zero.
class Main {
public static void main(String[] args) throws Exception {
double slope = 1.0;
System.out.printf(java.util.Locale.ROOT, "%.3f%n", (double) (Math.atan(slope)));
}
}
0.785
Equal rise and run give a slope of one and an angle of about 0.785 radians.
Point a map label up a street
On a square-grid map a stop is 4 blocks east and 2 north of the depot. A designer wants the label to follow the route’s angle above the east axis.
Positive horizontal distance allows either the rise/run form or the two-coordinate form.
Aim along a horizontal corridor
A floor-plan arrow points 6 m right and never changes height. The drawing program stores the angle measured upward from the rightward axis.
A point directly to the right has a direction of zero radians.
What to watch for
For a direction that must distinguish opposite quadrants, keep both coordinates and use atan2.