# how to rotate a 3D vector around arbitrary axis

I have three vectors red, blue, and green and their origins are at (x,y,z) where red and blue are perpendicular and can be rotated together in the x-y plane by tunning angle \phi. This part is done, now I would like to add a green vector (must always be perpendicular to red) that can be rotated by angle \theta around the red vector. my code is working fine only with (x,y,z)=(0,0,0) but with,e.g., (1,1,0) green vector does not rotate around the red one properly. How can I fix this, please?

steps to generate this are:

• I start with a vector aA where its origin located at arbitrary point (x,y,z) (the blue one)
• then to get a perpendicular vector to the blue one I just rotated aA by angle \pi/2 and get the red vector.
• last step is to create a vector that can be rotated around the red one and perpendicular to it (green one).

here is the code

aA = {-2, 0, 0};

rot4[x_, y_, z_, \[Phi]x_, \[Theta]z_] := {Black,
Sphere[{x, y, z}, 0.1], Blue,
Arrow[RotationTransform[\[Phi]x, {0, 0, 1}, {x, y,
z}][{{({x, y, z}), ({x, y, z} + aA)}}]], Red,
Arrow[{{x, y, z},
RotationTransform[\[Pi]/2, {0, 0, 1}, {x, y, z}][
RotationTransform[\[Phi]x, {0, 0, 1}, {x, y, z}][
aA + {x, y, z}]]}], Green,
Arrow[RotationTransform[\[Theta]z,
RotationTransform[\[Pi]/2, {0, 0, 1}, {x, y, z}][
RotationTransform[\[Phi]x, {0, 0, 1}, {x, y, z}][
aA + {x, y, z}]], {x, y, z}][
RotationTransform[\[Phi]x, {0, 0, 1}, {x, y,
z}][{{({x, y, z}), ({x, y, z} + aA)}}]]]}

Manipulate[
Graphics3D[{rot4[0, 0, 0, \[Phi]x, \[Theta]z]}, Boxed -> False,
ImageSize -> 300, FaceGrids -> {Left, Back, Bottom},
FaceGridsStyle -> GrayLevel[0.5],
PlotRange -> {{-4, 4}, {-4, 4}, {-4, 4}}], {\[Phi]x, 0,
2 \[Pi], \[Pi]/20}, {\[Theta]z, 0, 2 \[Pi], \[Pi]/20}]


• Can you state more clearly how the green vector is not doing what you want? It seems to be rotating in the plane for which red would be a normal vector, which is how I would interpret what you're trying to do. Commented Sep 20, 2021 at 15:54

## 1 Answer

Here is a corrected and somewhat simplified version:

rot4[x_, y_, z_, ϕx_, θz_] :=
Module[{orig = {x, y, z}, red0 = 4 {1, 0, 0}, blue0 = 4 {0, 1, 0},
green0 = 4 {0, 0, 1}}, {red, blue} =
RotationTransform[ϕx, {0, 0, 1}] /@ {red0, blue0};
green = RotationTransform[θz, red][green0];
{Black, Sphere[orig, 0.1], Red, Arrow[{orig, orig + red}], Blue,
Arrow[{orig, orig + blue}], Green, Arrow[{orig, orig + green}]}];

Manipulate[
Graphics3D[{rot4[-1, -1, 0, ϕx, θz]}, ImageSize -> 300,
FaceGrids -> {Left, Back, Bottom}, FaceGridsStyle -> GrayLevel[0.5],
Axes -> True, AxesLabel -> {"x", "y", "z"},
PlotRange -> {{-4, 4}, {-4, 4}, {-4, 4}}], {ϕx, 0,
2 π, π/20}, {θz, 0, 2 π, π/20}]


• thanks for the answer but still not working unless (x,y,z)=(0,0,0) as you posted and even you need to change the rotation of the green vector to be around red not red-aA to get it correct and this just working fine in my code. the problem arises when (x,y,z)=(1,1,0) this problem persists in your code too and even the angle between red and blue is not \pi\2 anymore. Commented Sep 20, 2021 at 8:01
• Sorry, I should work now. Commented Sep 20, 2021 at 15:38
• Awesome, I see how it should be done. doing everything at the origin (0,0,0) and then move it to the desired one. Thanks! Commented Sep 20, 2021 at 17:12