Program to convert polar co-ordinates to equivalent cartesian co-ordinates
Given two integers r and ? (in degree) representing polar coordinates of a point (r, ?), the task is to find the Cartesian coordinates of the given point.
Examples:
Input: r = 1.4142, ? = 45
Output: 1.000, 1.000
Input: r = 3, ? = 30
Output: 2.598, 1.500
Approach: Let the cartesian coordinates of the point be (x, y). The polar coordinates and the Cartesian coordinates can be related using the following equations:
x = r*cos? and y = r*sin?
Follow the steps below to solve the problem:
- Convert ? from degrees to radian as ?(in radian) = ? (in degrees) * (3.14159 / 180).
- Store the x and y coordinate in a variable X and Y respectively.
- Apply transformation formula and update the value of X = r * cos? and Y = r * sin?.
- Print the value of X and Y as the result.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
double ConvertDegToRad( double degree)
{
double pi = 3.14159;
return (degree * (pi / 180.0));
}
void ConvertToCartesian(
pair< double , double > polar)
{
polar.second = ConvertDegToRad(
polar.second);
pair< double , double > cartesian
= { polar.first * cos (polar.second),
polar.first * sin (polar.second) };
printf ( "%0.3f, %0.3f" ,
cartesian.first,
cartesian.second);
}
int main()
{
pair< double ,
double >
polar = { 1.4142, 45 };
ConvertToCartesian(polar);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static double ConvertDegToRad( double degree)
{
double pi = 3.14159 ;
return (degree * (pi / 180.0 ));
}
static void ConvertToCartesian(
double [] polar)
{
polar[ 1 ] = ConvertDegToRad(
polar[ 1 ]);
double [] cartesian
= { polar[ 0 ] * Math.cos(polar[ 1 ]),
polar[ 0 ] * Math.sin(polar[ 1 ]) };
System.out.print(String.format( "%.3f" , cartesian[ 0 ])+ " " +String.format( "%.3f" , cartesian[ 1 ]));
}
public static void main(String[] args)
{
double [] polar = { 1.4142 , 45 };
ConvertToCartesian(polar);
}
}
|
Python3
import math
def ConvertDegToRad(degree):
pi = 3.14159
return (degree * (pi / 180.0 ))
def ConvertToCartesian(polar):
polar[ 1 ] = ConvertDegToRad(polar[ 1 ])
cartesian = [polar[ 0 ] * math.cos(polar[ 1 ]),
polar[ 0 ] * math.sin(polar[ 1 ])]
print ( '%.3f' % cartesian[ 0 ],
'%.3f' % cartesian[ 1 ])
if __name__ = = "__main__" :
polar = [ 1.4142 , 45 ]
ConvertToCartesian(polar)
|
C#
using System;
class GFG
{
static Double ConvertDegToRad(Double degree)
{
Double pi = 3.14159;
return (degree * (pi / 180.0));
}
static void ConvertToCartesian(
Double[] polar)
{
polar[1] = ConvertDegToRad(
polar[1]);
Double[] cartesian
= { polar[0] * Math.Cos(polar[1]),
polar[0] * Math.Sin(polar[1]) };
Console.Write(String.Format( "{0:0.000}" , cartesian[0])+
", " +String.Format( "{0:0.000}" , cartesian[1]));
}
public static void Main()
{
Double[] polar = { 1.4142, 45 };
ConvertToCartesian(polar);
}
}
|
Javascript
<script>
function ConvertDegToRad(degree)
{
let pi = 3.14159;
return (degree * (pi / 180.0));
}
function ConvertToCartesian(polar)
{
polar[1] = ConvertDegToRad(
polar[1]);
let cartesian
= [ polar[0] * Math.cos(polar[1]),
polar[0] * Math.sin(polar[1]) ];
document.write((cartesian[0]).toFixed(3)+ ", "
+(cartesian[1]).toFixed(3));
}
let polar=[1.4142, 45 ];
ConvertToCartesian(polar);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
21 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...