📜  将极坐标转换为等效笛卡尔坐标的程序

📅  最后修改于: 2021-10-23 08:45:01             🧑  作者: Mango

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

例子:

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

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

  • θ从度数转换为弧度为θ(in radian) = θ (in degree) * (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
// coordinate 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
  // coordinate 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
# coordinate 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.


Javascript


输出:
1.000, 1.000

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程