The Wayback Machine - https://web.archive.org/web/20240709181846/https://www.geeksforgeeks.org/program-to-convert-polar-co-ordinates-to-equivalent-cartesian-co-ordinates/
Open In App

Program to convert polar co-ordinates to equivalent cartesian co-ordinates

Last Updated : 21 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to convert degree to radian
double ConvertDegToRad(double degree)
{
    double pi = 3.14159;
    return (degree * (pi / 180.0));
}
 
// Function to convert the polar
// coordinate to cartesian
void ConvertToCartesian(
    pair<double, double> polar)
{
 
    // Convert degrees to radian
    polar.second = ConvertDegToRad(
        polar.second);
 
    // Applying the formula:
    // x = rcos(theta), y = rsin(theta)
    pair<double, double> cartesian
        = { polar.first * cos(polar.second),
            polar.first * sin(polar.second) };
 
    // Print cartesian coordinates
    printf("%0.3f, %0.3f",
           cartesian.first,
           cartesian.second);
}
 
// Driver Code
int main()
{
    // Given polar coordinates
    pair<double,
         double>
        polar = { 1.4142, 45 };
 
    // Function to convert polar
    // coordinates to equivalent
    // cartesian coordinates
    ConvertToCartesian(polar);
 
    return 0;
}


Java




// Java code of above approach
import java.util.*;
 
class GFG
{
 
  // Function to convert degree to radian
  static double ConvertDegToRad(double degree)
  {
    double pi = 3.14159;
    return (degree * (pi / 180.0));
  }
 
  // Function to convert the polar
  // coordinate to cartesian
  static void ConvertToCartesian(
    double[] polar)
  {
 
    // Convert degrees to radian
    polar[1] = ConvertDegToRad(
      polar[1]);
 
    // Applying the formula:
    // x = rcos(theta), y = rsin(theta)
    double[] cartesian
      = { polar[0] * Math.cos(polar[1]),
         polar[0] * Math.sin(polar[1]) };
 
    // Print cartesian coordinates
    System.out.print(String.format("%.3f", cartesian[0])+" "+String.format("%.3f", cartesian[1]));
 
  }
 
  // Driver code
  public static void main(String[] args)
  {
    // Given polar coordinates
 
    double[] polar = { 1.4142, 45 };
 
    // Function to convert polar
    // coordinates to equivalent
    // cartesian coordinates
    ConvertToCartesian(polar);
 
  }
}
 
// This code is contributed by offbeat


Python3




# Python 3 program for the above approach
import math
 
# Function to convert degree to radian
def ConvertDegToRad(degree):
    pi = 3.14159
    return (degree * (pi / 180.0))
 
# Function to convert the polar
# coordinate to cartesian
def ConvertToCartesian(polar):
 
    # Convert degrees to radian
    polar[1] = ConvertDegToRad(polar[1])
 
    # Applying the formula:
    # x = rcos(theta), y = rsin(theta)
    cartesian = [polar[0] * math.cos(polar[1]),
                 polar[0] * math.sin(polar[1])]
 
    # Print cartesian coordinates
    print('%.3f' % cartesian[0],
          '%.3f' % cartesian[1])
 
# Driver Code
if __name__ == "__main__":
 
    # Given polar coordinates
    polar = [1.4142, 45]
 
    # Function to convert polar
    # coordinates to equivalent
    # cartesian coordinates
    ConvertToCartesian(polar)
 
    # This code is contributed by chitranayal.


C#




// C# program for the above approach
using System;
 
class GFG
{
 
  // Function to convert degree to radian
  static Double ConvertDegToRad(Double degree)
  {
    Double pi = 3.14159;
    return (degree * (pi / 180.0));
  }
 
  // Function to convert the polar
  // coordinate to cartesian
  static void ConvertToCartesian(
    Double[] polar)
  {
 
    // Convert degrees to radian
    polar[1] = ConvertDegToRad(
      polar[1]);
 
    // Applying the formula:
    // x = rCos(theta), y = rSin(theta)
    Double[] cartesian
      = { polar[0] * Math.Cos(polar[1]),
         polar[0] * Math.Sin(polar[1]) };
 
    // Print cartesian coordinates
    Console.Write(String.Format("{0:0.000}", cartesian[0])+
                  ", "+String.Format("{0:0.000}", cartesian[1]));
 
  }
 
  // Driver code
  public static void Main()
  {
 
    // Given polar coordinates
    Double[] polar = { 1.4142, 45 };
 
    // Function to convert polar
    // coordinates to equivalent
    // cartesian coordinates
    ConvertToCartesian(polar);
 
  }
}
 
// This code is contributed by Shubham Singh


Javascript




<script>
 
// JavaScript code of above approach
 
// Function to convert degree to radian
function ConvertDegToRad(degree)
{
    let pi = 3.14159;
    return (degree * (pi / 180.0));
}
 
// Function to convert the polar
  // coordinate to cartesian
function ConvertToCartesian(polar)
{
    // Convert degrees to radian
    polar[1] = ConvertDegToRad(
      polar[1]);
  
    // Applying the formula:
    // x = rcos(theta), y = rsin(theta)
    let cartesian
      = [ polar[0] * Math.cos(polar[1]),
         polar[0] * Math.sin(polar[1]) ];
  
    // Print cartesian coordinates
    document.write((cartesian[0]).toFixed(3)+", "
    +(cartesian[1]).toFixed(3));
}
 
// Driver code
let polar=[1.4142, 45 ];
// Function to convert polar
// coordinates to equivalent
// cartesian coordinates
ConvertToCartesian(polar);
 
// This code is contributed by avanitrachhadiya2155
 
</script>


Output: 

1.000, 1.000

 

Time Complexity: O(1)
Auxiliary Space: O(1)



Similar Reads

Convert given Complex Numbers into polar form and perform all arithmetic operations
Given two Complex Numbers Z1 and Z2 in the Cartesian form, the task is to convert the given complex number into polar form and perform all the arithmetic operations ( addition, subtraction, multiplication, and division ) on them. Examples: Input: Z1 = (2, 3), Z2 = (4, 6)Output:Polar form of the first Complex Number: (3.605551275463989, 0.9827937232
15 min read
Program to convert Hexa-Decimal Number to its equivalent BCD
Given a HexaDecimal Number N, the task is to convert the number to its equivalent Binary Coded Decimal.Examples: Input: 11F Output: 0001 0001 1111 Explanation: Binary of 1 - 0001 Binary of F - 1111 Thus, Equivalent BCD is 0001 0001 1111Input: A D Output: 1010 1101 Explanation: Binary of A - 1010 Binary of D - 1101 Thus, Equivalent BCD is 1010 1101
10 min read
Program to convert given Binary to its equivalent ASCII character string
Given a binary string str, the task is to find its equivalent ASCII (American Standard Code for Information Interchange) character string. Examples: Input: str = "0110000101100010"Output: abExplanation: Dividing str into set of 8 bits as follows: 01100001 = 97, ASCII value of 97 is 'a'.01100010 = 98, ASCII value of 98 is 'b'.Therefore, the required
9 min read
Program to determine the quadrant of the cartesian plane
Given co-ordinates (x, y), determine the quadrant of the cartesian plane. Image_source : wikipedia.org Examples : Input : x = 1, y = 1 Output : lies in 1st quadrant Input : x = 0, y = 0 Output : lies at origin There are 9 conditions that needs to be checked to determine where does the points lies - If in first quadrant then, x &gt; 0 and y &gt; 0 I
6 min read
Convert the given BCD to its equivalent Binary form
Given a BCD (Binary Coded Decimal), the task is to convert this to its equivalent Binary number. Examples: Input: 1001000 Output: 110000 Explanation: Integer value of the given BCD is 48(0100 -&gt; 4, 1000 -&gt; 8). (48)10 = (110000)2 Input: 1001001 Output: 110000 Approach: In order to solve this problem we need to split the given BCD number into b
6 min read
Convert given Float value to equivalent Fraction
Given a floating-point number in the form of a string N, the task is to convert the given floating-point number into fractions. Enclosed sequence of digits in "()" in the floating-point representation expresses recurrence in the decimal representation. For example, 1.(6) represents 1.666.... Examples: Input: N = "1.5"Output: 3 / 2Explanation:The va
11 min read
Convert a Mobile Numeric Keypad sequence to equivalent sentence
Given a string S of size N, consisting of digits [0 - 9] and character '.', the task is to print the string that can be obtained by pressing the mobile keypad in the given sequence. Note: '.' represents a break while typing. Below is the image to represent the characters associated with each number in the keypad. Examples: Input: S = "234"Output: A
9 min read
Convert a sentence into its equivalent mobile numeric keypad sequence
Given a sentence in the form of a string, convert it into its equivalent mobile numeric keypad sequence.  Examples :  Input: GEEKSFORGEEKSOutput: 4333355777733366677743333557777Explanation: For obtaining a number, we need to press a number corresponding to that character for a number of times equal to the position of the character. For example, for
6 min read
Convert the ASCII value sentence to its equivalent string
Given a string str which represents the ASCII (American Standard Code for Information Interchange) Sentence, the task is to convert this string into its equivalent character sequence. Examples: Input: str = "71101101107115" Output: Geeks 71, 101, 101, 107 are 115 are the unicode values of the characters 'G', 'e', 'e', 'k' and 's' respectively.Input
5 min read
Maximum integral co-ordinates with non-integer distances
Given a maximum limit of x - coordinate and y - coordinate, we want to calculate a set of coordinates such that the distance between any two points is a non-integer number. The coordinates (i, j) chosen should be of range 0&lt;=i&lt;=x and 0&lt;=j&lt;=y. Also, we have to maximize the set. Examples: Input : 4 4 Output : 0 4 1 3 2 2 3 1 4 0 Explanati
5 min read
three90RightbarBannerImg