The problem I’m having is not finding the distance but finding the radians with Atan() and converting it to degrees.
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
You want to use
Atan2here.To convert to degrees multiply by
180/Math.PI.This will give you a result in the range -180 to 180. If you want it in the range 0 to 360 then you will have to shift any negative angles by 360.
I also strongly recommend that you do not return your polar coordinates in the same parameters that you used to pass in cartesian coordinates. Make your function like this:
Your
outputCoordsmethod also usesrefparameters incorrectly. Only userefparameters for values that are passed into a method, modified, and then need to be passed back to the caller.