# Rotating a Point in Space, Function of time

## Main Question or Discussion Point

Hi guys, I have a problem that's bugging me. I am writing a subroutine, which basically takes a grid (collection of points in space), and rotates it around the x-axis at some given rotational velocity. I was doing it a very easy way. Basically:

Code:
x0 = 0.0
y0 = 0.0
dx = x - x0
dy = y - y0
theta = atan(dy/dx)
theta = theta + dtheta
y = radius*sin(theta)
This works great, however it's very slow. To compute square roots and inverse trig functions in a computer code is very tedious, and when you need to do it for thousands or millions of gridpoints, it can take forever.

My idea was to just write a function for the gridpoints as a function of time alone. I won't be able to store any additional information, such as original or previous position. I had an idea of some sort of phase angle, and I think that would require that.

Also, I cannot simply find the velocity, and then calculate the displacement, and therefore new position, because apparently there are numerical problems and the grid could start to "walk" away from each other.

Hopefully I've explained this well enough, I appreciate any help.

How much linear algebra do you know?

A bit. I'm finishing up my master's of science in Mech. Engineering. I have math background all the way through non-linear, high-order, transient partial diff. eqs.

Really, What school?

Ok, well hopefully this will make some sense then. Consider a single point, $x_0,y_0,z_0$ that gets rotated to x,y,z. Writing these points in vector notation,
$$\vec{x}_0 = \left(x_0,y_0,z_0\right)$$

$$\vec{x} = \left(x,y,z\right)$$

$\vec{x}$ and $\vec{x}_0$ are related by a rotation, and rotations are just a matrix multiplication. In other words, for a rotation matrix R (scroll down on the link for 3d version),
$$\vec{x} = R \vec{x}_0$$

The key point is that the same rotation matrix will rotate any points by the same angle, so you only need to calculate R once per timestep and just reuse it for all the points. This is nice because all the trig functions and expensive computations are in R.

----

Now if you want to go crazy efficient, you could even express the rotation of all the points in a single matrix multiplication. Call all the points you want to rotate $\vec{x1},\vec{x2},...,\vec{x1000000}$. Then
$$\vec{x1} = R \vec{x1}_0$$
$$\vec{x2} = R \vec{x2}_0$$
...
$$\vec{x1000000} = R \vec{x1000000}_0$$

These can be combined together into a single matrix multiplication:
$$\left[\vec{x1} | \vec{x2} | ... | \vec{x1000000} \right] = R \left[\vec{x1}_0 | \vec{x2}_0 | \vec{x1000000}_0\right]$$

Then you can use a matrix multiplication package to be super efficient (mathematicians have worked very hard to figure out really really efficient ways to multiply matricies).

Last edited: