# Any examples of how to rotate a grid of points?

I found two packages of interest Rotations.jl and ReferenceFraneRotations.jl - does anyone happen to know an example where it is used to rotate for example a grid of points 45 degrees? I have found a function to generate a grid like this:

``````function creategrid(d::Integer, dp::Number)

@assert d >= 1 ("d (number of dimensions) must be a positive integer")

r = range(-1, 1, step = dp)

iter = Iterators.product((r for _ in 1:d)...)

return vec([collect(i) for i in iter])
end

creategrid(2,0.1)
``````

Kind regards

Since you have 2D grids and seemingly simple rotations, it may be more instructive to write the rotation matrix in this case:

``````rotgrid(grd, θ) = [cos(θ) -sin(θ); sin(θ) cos(θ)] * grd

grd = creategrid(2, 0.05)
rgrd = rotgrid.(grd, π/4)

using GLMakie
scene = scatter(first.(grd), last.(grd), color=:blue, markersize=5, axis=(aspect=DataAspect(), ))
scatter!(first.(rgrd), last.(rgrd), color=:red, markersize=5)
``````

3 Likes

Thanks for this suggestion!

If I would want to do the rotation around a specific point, let us say the bottom right corner, how would I do that?

Kind regards

To rotate around any point `P0`:

You can do:

``````grd = creategrid(2, 0.05)
P0 = [-1,-1]    # point of rotation
rgrd = rotgrid.(grd .- Ref(P0), π/4)  .+ Ref(P0)
``````

Thank you very much Rafael Kind regards

1 Like