0
我遇到的问题不是找到距离,而是使用Atan()找到弧度并将其转换为度数。将矩形点转换为极坐标
using System;
class Program
{
static void Main()
{
double xCoord =0, yCoord=0 ;
//accessing methods
getUserInput(ref xCoord, ref yCoord);
CalulatePolarCoords(ref xCoord, ref yCoord);
outputCords(ref xCoord, ref yCoord);
Console.ReadLine();
}//End Main()
static void getUserInput(ref double xc, ref double yc)
{
//validating input
do
{
Console.WriteLine(" please enter the x cororidnate must not equal 0 ");
xc = double.Parse(Console.ReadLine());
Console.WriteLine("please inter the y coordinate");
yc = double.Parse(Console.ReadLine());
if(xc <= 0)
Console.WriteLine(" invalid input");
}
while (xc <= 0);
Console.WriteLine(" thank you");
}
//calculating coords
static void CalulatePolarCoords(ref double x , ref double y)
{
double r;
double q;
r = x;
q = y;
r = Math.Sqrt((x*x) + (y*y));
q = Math.Atan(x/y);
x = r;
y = q;
}
static void outputCords(ref double x, ref double y)
{
Console.WriteLine(" The polar cordinates are...");
Console.WriteLine("distance from the Origin {0}",x);
Console.WriteLine(" Angle (in degrees) {0}",y);
Console.WriteLine(" press enter to continute");
}
}//End class Program
问题解决谢谢我使用180/2 * Math.PI而不是180/Math.PI – Jordan
要将Atan2结果从-180 ... 180转换为0 ... 360,您只需要移动180,而不是360. 360度的转变会给你180 ... 540的范围。您只需要移动180度: q =(Math.Atan2(y,x)* 180.0/Math.PI + 180.0); (使用180.0 vs 180来消除任何可能的转换开销)。 –
@RichardRobertson不,事实并非如此。确实,加180会让你的值在正确的范围内,但这些值是错误的值。您需要按照我所说的去做,将360添加到任何负值。你只能改变负值,你必须改变它们,否则你会改变它的值。请记住,围绕一个圆的角度进行比较的模数相等为360。 –