How to Change Coordinates to Angles in Java?

Rectangular, or Cartesian, coordinates specify a point in terms of its horizontal and vertical components. Polar coordinate systems define a point in terms of its distance from the origin and the angle it subtends with the horizontal axis. Conversion between the two coordinate systems involves the use of the arctangent function and some consideration of quadrant to get a consistent angular measure of the coordinate point's location. Use the "Math" library to get the polar angle from a coordinate pair in Java.

Instructions

    • 1

      Use the arctangent function to get an angle from a coordinate point whose components are stored as doubles in "x" and "y" using the following syntax:

      double angle = Math.atan(y/x);

    • 2

      Use the following conditional block to reference the angle consistently from the positive x-axis:

      if (x < 0 && y >= 0){

      angle += Math.PI;

      }else if (x < 0 && y < 0){

      angle += Math.PI;

      }else if (x >= 0 && y < 0){

      angle += 2*Math.PI;

      }

    • 3

      Convert the angle to degrees, if you wish, with the following line:

      angle = angle*180/Math.PI;

Related Searches:

Comments

Related Ads

Featured