2011-10-23 138 views
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 

回答

4

你想在这里使用Atan2

q = Math.Atan2(y, x); 

转换为度数乘以180/Math.PI

这会给你一个结果的范围从-180到180。如果你想它的范围从0到360,那么你将有由360

任何负面的角度转向我也强烈建议您请勿将您的极坐标返回到您用于传递笛卡尔坐标的相同参数中。让你的功能是这样的:

static void CalculatePolarCoords(double x, double y, out double r, out double q) 

outputCoords方法也使用ref参数不正确。只有使用ref参数才能传递给方法的值,已修改,然后需要传回给调用者。

+1

问题解决谢谢我使用180/2 * Math.PI而不是180/Math.PI – Jordan

+0

要将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来消除任何可能的转换开销)。 –

+0

@RichardRobertson不,事实并非如此。确实,加180会让你的值在正确的范围内,但这些值是错误的值。您需要按照我所说的去做,将360添加到任何负值。你只能改变负值,你必须改变它们,否则你会改变它的值。请记住,围绕一个圆的角度进行比较的模数相等为360。 –

相关问题