2

If we have a pair of coordinates $(x,y)$, let's say

pt = {1,2}

then we can easily rotate the coordinates, by an angle $\theta$, by using the rotation matrix

R = {{Cos[\[Theta]], -Sin[\[Theta]]}, {Sin[\[Theta]], Cos[\[Theta]]}};

as

pt2 = pt.R;

Now let's assume that we have a collection of points in the form

data = {{1}, {-0.3, 1}, {2, -0.2}, {2}, {-2, 1}, {4,-2}, {3}, {1, 1}, {-0.2, -0.3}}

where the integers 1, 2 and 3 count the subsets of the list data.

My question: how can we rotate the $(x,y)$ coordinates of the list data by and angle, let's say $2\pi/3$ and create a new list, data2 of the form

data2 = {{1}, {rotated x, rotated y}, {rotated x, rotated y}, {2}, {rotated x, rotated y}, {roatetd x, rotated y}, ...}

Any suggestions?

Vaggelis_Z
  • 8,740
  • 6
  • 34
  • 79

3 Answers3

4

This should give what you want:

data2=data/.{x_?NumericQ,y_?NumericQ}:>RotationMatrix[\[Theta]].{x,y}
Alx
  • 3,632
  • 11
  • 15
3

Maybe this way?

data = {{1}, {-0.3, 1}, {2, -0.2}, {2}, {-2, 1}, {4, -2}, {3}, {1, 1}, {-0.2, -0.3}};
R = {{Cos[\[Theta]], -Sin[\[Theta]]}, {Sin[\[Theta]], Cos[\[Theta]]}};
data2 = data;
data2[[2 ;; ;; 3]] = data[[2 ;; ;; 3]].Transpose[R];
data2[[3 ;; ;; 3]] = data[[3 ;; ;; 3]].Transpose[R];

However, I advice not to store your data this way because, as a ragged list, it cannot be packed.

Henrik Schumacher
  • 106,770
  • 7
  • 179
  • 309
1

Using MapAt:

MapAt[
 RotationTransform[2π/3][#] &
 , data
 , Position[data, {_, _}]
 ]

$$\left\{\{1\},\{-0.716025,-0.759808\},\{-0.826795,1.83205\},\{2\},\left\{1-\frac{\sqrt{3}}{2},-\sqrt{3}-\frac{1}{2}\right\},\left\{\sqrt{3}-2,2 \sqrt{3}+1\right\},\{3\},\left\{-\frac{\sqrt{3}}{2}-\frac{1}{2},\frac{\sqrt{3}}{2}-\frac{1}{2}\right\},\{0.359808,-0.0232051\}\right\}$$

Syed
  • 52,495
  • 4
  • 30
  • 85