Cartesian to Polar & Polar to Cartesian Coordinate
Cartesian / Rectangular to Polar Conversion
The java code converts the Cartesian coordinate values (x,y) into polar coordinate values (r,Θ).The input values for x and y are read from the user using scanner object and these values are converted into corresponding polar coordinate values by following two equations.
Polar Coordinate radius |
Polar Coordinate theta |
import java.util.Scanner;
public class Cartesian2Polar {
public static void main(String[] args)
{
System.out.println("\nCartesian to Polar Conversion");
Scanner sc = new Scanner(System.in);
System.out.println("\n Enter x-axis value : ");
int x =sc.nextInt();
System.out.println("\n Enter y-axis value : ");
int y =sc.nextInt();
double radius = Math.sqrt( Math.pow(x, 2) + Math.pow(y, 2) );
double radiant = Math.atan(y/x);
double angle = radiant * (180/Math.PI);
System.out.println("Radius :" + radius);
System.out.println("Angle in radiant: " + radiant);
System.out.println("Angle in degree: " + angle);
}
}
Output
Cartesian to Polar Conversion Enter x-axis value : 3 Enter y-axis value : 3 Radius :4.242640687119285 Angle in radiant:0.7853981633974483 Angle in degree:45.0
Polar to Cartesian/Rectangular Conversion
The Java code converts the polar coordinate values (r,Θ) into Cartesian coordinate values (x,y).The input values for r and Θ are read from the user using scanner object and these values are converted into corresponding Cartesian/rectangular coordinate values by following two equations.
Cartesian x-axis |
Cartesian y-axis |
import java.util.Scanner;
public class Polar2Cartesian {
public static void main(String[] args)
{
System.out.println("\nPolar to Cartesian Conversion");
Scanner sc = new Scanner(System.in);
System.out.println("\nEnter radius value : ");
double radius =sc.nextDouble();
System.out.println("\nEnter angle in degree : ");
double angle =sc.nextDouble();
double radiant = angle * (Math.PI/180);
double x = radius * Math.cos(radiant);
double y = radius * Math.sin(radiant);
System.out.println("\nx :" + x);
System.out.println("y : " + y);
}
}
Output
Polar to Cartesian Conversion Enter radius value : 12 Enter angle in degree : 195 x :-11.59110991546882 y : -3.1058285412302498
Comments
Post a Comment