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.000Input: 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> |
1.000, 1.000
Â
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!