If I use the function VectorAngle[{1,0},{1,-1}], is it possible to obtain the angle generated by rotating around the axis counter clock wise? In other words, I would move from the first vector to the second in the positive direction. My output would be (7/4)*Pi instead of Pi/4.
3 Answers
Not with VectorAngle
alone. One way to go about this:
directedangle[a_, b_] :=
If[Sign@Det[{a, b}] >= 0, VectorAngle[a, b], 2 π - VectorAngle[a, b]]
directedangle[{1, 0}, {1, 1}]
directedangle[{1, 0}, {-1, 1}]
directedangle[{1, 0}, {1, -1}]
π/4
(3 π)/4
(7 π)/4
-
$\begingroup$ very nice, but the format of the output is not correct (confusing) :) $\endgroup$– eldoJun 20, 2014 at 20:26
-
$\begingroup$ @eldo sorry my symbol toolbar is not working right now. $\endgroup$ Jun 20, 2014 at 20:28
-
$\begingroup$ @Öskå - thanks for the edit (mine was only a quick fix) $\endgroup$– eldoJun 20, 2014 at 20:40
-
$\begingroup$ @Öska thx - editing from a mobile phone is a royal pain :-) $\endgroup$ Jun 20, 2014 at 20:41
-
ArcTan
version:
(If[# < 0, # + 2 Pi , #] &@(-Subtract @@ (ArcTan @@ # & /@ #))) & /@
{{{1, 0}, {1, 1}}, {{1, 0}, {-1, 1}}, {{1, 0}, {1, -1}}}
{Pi/4, (3 Pi)/4, (7 Pi)/4}
or to put in the function form of the other answer:
directedangle[a_, b_] :=
(If[# < 0, # + 2 Pi, #] &@(-Subtract @@ (ArcTan @@ # & /@ {a, b})))
-
$\begingroup$ would it be possible to find an algorithm for the second line of your answer? $\endgroup$– eldoJun 20, 2014 at 21:19
-
$\begingroup$ Would someone be able to parse this last solution for me? I think I know what the various operators do, but I can't tell how to group them. $\endgroup$ Jul 17, 2015 at 23:36
For the directional angle I am using a simplified version. To get -1/4 Pi instead of 7/4 Pi is OK!
directedangle[a_, b_]:= Sign@Det[{a, b}] VectorAngle[a, b]
directedangle[{1, 0}, {1, 1}]
directedangle[{1, 0}, {-1, 1}]
directedangle[{1, 0}, {1, -1}]
Pi/4
(3 Pi)/4
-Pi/4
2 Pi - VectorAngle[{1, 0}, {1, -1}]
? $\endgroup$