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

**URL:** https://discourse.julialang.org/t/any-examples-of-how-to-rotate-a-grid-of-points/70211
**Category:** New to Julia
**Created:** [October 22, 2021, 1:29pm UTC](https://discourse.julialang.org/t/any-examples-of-how-to-rotate-a-grid-of-points/70211 "2021-10-22T13:29:34Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [October 22, 2021, 1:29pm UTC](https://discourse.julialang.org/t/any-examples-of-how-to-rotate-a-grid-of-points/70211/1 "2021-10-22T13:29:34Z")

</div>

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:

```julia
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

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [October 22, 2021, 3:09pm UTC](https://discourse.julialang.org/t/any-examples-of-how-to-rotate-a-grid-of-points/70211/2 "2021-10-22T15:09:43Z")

</div>

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

```julia
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)

```

 ![Makie_rotated_2Dgrids](https://global.discourse-cdn.com/julialang/original/3X/4/a/4a6ea52deb888740e31257dc77490f303841a65e.png)

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [October 25, 2021, 5:09pm UTC](https://discourse.julialang.org/t/any-examples-of-how-to-rotate-a-grid-of-points/70211/3 "2021-10-25T17:09:32Z")

</div>

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

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [October 25, 2021, 5:19pm UTC](https://discourse.julialang.org/t/any-examples-of-how-to-rotate-a-grid-of-points/70211/4 "2021-10-25T17:19:20Z")

</div>

To rotate around any point `P0`:

 ![Makie_rotated_2Dgrids](https://global.discourse-cdn.com/julialang/original/3X/8/a/8acccf9ca42cb7f6ec18968517fc10f2926a896e.png)

You can do:

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

```

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [October 25, 2021, 5:44pm UTC](https://discourse.julialang.org/t/any-examples-of-how-to-rotate-a-grid-of-points/70211/5 "2021-10-25T17:44:57Z")

</div>

Thank you very much Rafael 🙂

Kind regards
