How could I get the angle in Degree from 0 to 360 from Cartesian coordinate such that :
{1,0} = 0 Degree
{0,1} = 90 Degrees
{-1,0} = 180 Degrees
{0,-1} = 270 Degrees
I am having a hard Time with ArcTan to get the angle bet 180 to 359...
How could I get the angle in Degree from 0 to 360 from Cartesian coordinate such that :
{1,0} = 0 Degree
{0,1} = 90 Degrees
{-1,0} = 180 Degrees
{0,-1} = 270 Degrees
I am having a hard Time with ArcTan to get the angle bet 180 to 359...
Try:
f[x_List] := Mod[ArcTan @@ x /Pi 180 Degree, 360 Degree]
f /@ {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
(*
-> {270 \[Degree], 90 \[Degree], 0, 180 \[Degree]}
*)
Edit
As the previous form was criticized upon, here is another way to do the same. Not so easy to follow for my taste:
f = (180 /Pi ArcTan @@ #)~Mod~360 &
Try this:
CoordinateToDegree[x_?NumberQ, y_?NumberQ] :=
Rescale[ArcTan[-x, y], {-Pi, Pi}, {360, 0}]
Using ArcTan[-x,y]
you will be aligning on the branch cut so you get a continuous function for the angle. Then Rescale
maps the range -Pi...Pi
to 0...360
.
Here is simple Manipulate
which demonstrates this solution:
Manipulate[
Graphics[{
Orange, Disk[],
Black, Text[Style[CoordinateToDegree[Cos[t], Sin[t]], "Title"], {Cos[t], Sin[t]}]},
PlotRange -> 1.4], {t, 0, 2 \[Pi]}]
I think this works, despite being ugly:
todeg[x_, y_] := If[# < 0, 360 + #, #] &@(N@ArcTan[x, y]/Degree)