# Vector rotation using CoordinateTransformations.jl

**URL:** <https://discourse.julialang.org/t/vector-rotation-using-coordinatetransformations-jl/111792>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 18, 2024, 8:36pm UTC](https://discourse.julialang.org/t/vector-rotation-using-coordinatetransformations-jl/111792 "2024-03-18T20:36:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KajWiik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kajwiik/32/199_2.png) [@KajWiik](https://discourse.julialang.org/u/KajWiik)\
**Post date:** [March 18, 2024, 8:36pm UTC](https://discourse.julialang.org/t/vector-rotation-using-coordinatetransformations-jl/111792/1 "2024-03-18T20:36:23Z")

</div>

It looks like vector rotation does not work or I am missing something:

```julia
using CoordinateTransformations, Rotations, StaticArrays, AngleBetweenVectors
julia> vec = [1.0,2.0,3.0]
3-element Vector{Float64}:
 1.0
 2.0
 3.0

julia> rotated = LinearMap(RotZ(0))(vec)
3-element SVector{3, Float64} with indices SOneTo(3):
 1.0
 2.0
 3.0

julia> angle(vec, Vector(rotated)) # OK
0.0

julia> rotated = LinearMap(RotZ(2pi))(vec)
3-element SVector{3, Float64} with indices SOneTo(3):
 1.0000000000000004
 1.9999999999999998
 3.0

julia> angle(vec, Vector(rotated)) # OK
1.5700924586837752e-16

julia> rotated = LinearMap(RotZ(0.42))(vec)
3-element SVector{3, Float64} with indices SOneTo(3):
 0.09756803419316795
 2.2339383336841867
 3.0

julia> angle(vec, Vector(rotated)) # Not OK
0.24980625896217093

julia> rotated = LinearMap(RotZ(0.7))(vec)
3-element SVector{3, Float64} with indices SOneTo(3):
 -0.5235931871908935
  2.1739020618066682
  3.0

julia> angle(vec, Vector(rotated)) # Not OK
0.4127652411766173

```

I.e. rotating by 0 or 2pi results a correct angle but rotation angles in between do not.

Any suggestions?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [March 18, 2024, 9:43pm UTC](https://discourse.julialang.org/t/vector-rotation-using-coordinatetransformations-jl/111792/2 "2024-03-18T21:43:04Z")

</div>

> [@KajWiik](#):
>
> `LinearMap(RotZ(0.42))(vec)`

`RotZ` is a rotation around the Z-axis. The angle between your vector and the rotated vector may not be the angle rotated if your vector has a Z-component.

To make this obvious, note that a rotation around the Z-axis has no effect on a unit vector pointing in the Z-direction.

```julia-repl
julia> v = [1., 0., 0]
3-element Vector{Float64}:
 1.0
 0.0
 0.0

julia> v2 = LinearMap(RotZ(0.42))(v)
3-element SVector{3, Float64} with indices SOneTo(3):
 0.9130889403123083
 0.40776045305957015
 0.0

julia> angle(v, Vector(v2))
0.42

julia> v3 = Float64[0,1,0]
3-element Vector{Float64}:
 0.0
 1.0
 0.0

julia> v4 = LinearMap(RotZ(0.42))(v3)
3-element SVector{3, Float64} with indices SOneTo(3):
 -0.40776045305957015
  0.9130889403123083
  0.0

julia> angle(v3, Vector(v4))
0.42

julia> v5 = Float64[0,0,1]
3-element Vector{Float64}:
 0.0
 0.0
 1.0

julia> v6 = LinearMap(RotZ(0.42))(v5)
3-element SVector{3, Float64} with indices SOneTo(3):
 0.0
 0.0
 1.0

julia> angle(v5, Vector(v6))
0.0

```

---

<div class="post-metadata">

**Author:** ![KajWiik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kajwiik/32/199_2.png) [@KajWiik](https://discourse.julialang.org/u/KajWiik)\
**Post date:** [March 18, 2024, 10:35pm UTC](https://discourse.julialang.org/t/vector-rotation-using-coordinatetransformations-jl/111792/3 "2024-03-18T22:35:15Z")

</div>

That is of course true, I was somehow thinking in 2D 😳. Many thanks your answer!
