📌  相关文章
📜  程序将极坐标转换为等效的直角坐标

📅  最后修改于: 2021-04-17 15:58:23             🧑  作者: Mango

给定两个表示点(r,θ)极坐标的整数rθ (度),任务是找到给定点的笛卡尔坐标。

例子:

方法:让该点的笛卡尔坐标为(x,y)。极坐标和笛卡尔坐标可以使用以下公式关联:

请按照以下步骤解决问题:

  • θ从度转换为弧度为θ(以弧度为单位)=θ(以度为单位)*(3.14159 / 180)
  • xy坐标分别存储在变量XY中
  • 应用变换公式并更新X = r *cosθY = r *sinθ的值
  • 打印XY的值作为结果。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
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
// cordinate to cartesian
void ConvertToCartesian(
    pair polar)
{
 
    // Convert degerees to radian
    polar.second = ConvertDegToRad(
        polar.second);
 
    // Applying the formula:
    // x = rcos(theata), y = rsin(theta)
    pair 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
        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
  // cordinate to cartesian
  static void ConvertToCartesian(
    double[] polar)
  {
 
    // Convert degerees to radian
    polar[1] = ConvertDegToRad(
      polar[1]);
 
    // Applying the formula:
    // x = rcos(theata), 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
# cordinate to cartesian
def ConvertToCartesian(polar):
 
    # Convert degerees to radian
    polar[1] = ConvertDegToRad(polar[1])
 
    # Applying the formula:
    # x = rcos(theata), 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.


输出:
1.000, 1.000

时间复杂度: O(1)
辅助空间: O(1)