Find the degree of a slope using Java

By xngo on February 28, 2019

/**
 * Show how to find the degree of a slope.
 */
public class Trigonometry
{
 
  public static void main(String[] args)
  {
    /**
     * All trigonometric functions in java.lang.Math return values in radians.
     * Therefore, you can use the Math.toDegrees() to convert to degree.
     * The inverse of sin, cosine and tangent are arcsine, arccosine and arctangent.
     */
    double dSlope = 1; // Let the slope be 1: (0,0), (1,1)
    double dRadian = Math.atan(dSlope);
    double dDegree = Math.toDegrees(dRadian);
 
    System.out.println(dDegree); // The expected output is: 45 degrees.
  }
 
}

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.