6
$\begingroup$

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.

$\endgroup$
2
  • 1
    $\begingroup$ How about 2 Pi - VectorAngle[{1, 0}, {1, -1}]? $\endgroup$
    – bill s
    Jun 20, 2014 at 17:28
  • $\begingroup$ Is there a way to specify what direction I moved with respect to the first vector? $\endgroup$
    – Kaisey
    Jun 20, 2014 at 17:40

3 Answers 3

7
$\begingroup$

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

$\endgroup$
5
  • $\begingroup$ very nice, but the format of the output is not correct (confusing) :) $\endgroup$
    – eldo
    Jun 20, 2014 at 20:26
  • $\begingroup$ @eldo sorry my symbol toolbar is not working right now. $\endgroup$
    – Yves Klett
    Jun 20, 2014 at 20:28
  • $\begingroup$ @Öskå - thanks for the edit (mine was only a quick fix) $\endgroup$
    – eldo
    Jun 20, 2014 at 20:40
  • $\begingroup$ @Öska thx - editing from a mobile phone is a royal pain :-) $\endgroup$
    – Yves Klett
    Jun 20, 2014 at 20:41
  • $\begingroup$ @eldo this helps a lot :) $\endgroup$
    – Öskå
    Jun 20, 2014 at 20:42
4
$\begingroup$

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})))
$\endgroup$
2
  • $\begingroup$ would it be possible to find an algorithm for the second line of your answer? $\endgroup$
    – eldo
    Jun 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
2
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.