/* * [TestATan2.java] * * Summary: Explore how Math.atan2 works. * * Copyright: (c) 2011-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2011-01-14 initial version */ package com.mindprod.test; import java.text.DecimalFormat; import static java.lang.System.*; /** * Explore how Math.atan2 works. *

* s. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2011-01-14 initial version * @since 2011-01-14 */ public final class TestATan2 { private static final DecimalFormat df = new DecimalFormat( "#0.000" ); /** * diplay facts about one multiple of pi/4 * * @param multiple multiple of pi/4 * @param y y to take atan2 of * @param x y to take atan2 of */ private static void show( int multiple, int y, int x ) { double a = Math.atan2( y, x ); out.println( Math.toDegrees( Math.PI * multiple / 4 ) + " " + df.format( Math.tan( Math.PI * multiple / 4 ) ) + " " + df.format( a ) + " " + df.format( Math.toDegrees( a ) ) ); } /** * main program to test average-calculating code. * * @param args command line not used. */ public static void main( String[] args ) { // the result of Math.atan2 is always in the range -pi to + pi in radians. (-180 to 180 degrees). // Several different numbers map to the same tangent. atan2 cleverly selects the one that is in the quadrant // of y then x. // Note the order y, x since tan is y/x. // Note how Math.toDegrees normalise the output to -180 .. +180 degrees. // bottom right quadrant show( 7, 1, -1 ); // bottom left quadrant show( 5, -1, -1 ); // top left quadrant show( 3, -1, 1 ); // y axis show( 2, 0, 1 ); // top right quadrant show( 1, 1, 1 ); // x axis show( 0, 1, 0 ); // bottom right quadrant show( -1, 1, -1 ); // bottom left quadrant show( -3, -1, -1 ); // top left quadrant show( -5, -1, 1 ); // top right quadrant show( -7, 1, 1 ); // prints // 315.0 -1.000 2.356 135.000 // 225.0 1.000 -2.356 -135.000 // 135.0 -1.000 -0.785 -45.000 // 90.0 16331239353195370.000 0.000 0.000 // 45.0 1.000 0.785 45.000 // 0.0 0.000 1.571 90.000 // -45.0 -1.000 2.356 135.000 // -135.0 1.000 -2.356 -135.000 // -225.0 -1.000 -0.785 -45.000 // -315.0 1.000 0.785 45.000 } }