title: Formula of rotating matrix/image 90 degree C++
body: <p>Let's say I have image, which I need to rotate 90 degrees in any direction and I just can't understand how to do that clear. I need to work with matrix, where Width - it's X, and Height - it's Y. I've already done rotating an image 180 degrees, but can't figure out 90 degrees.</p>
  
  <p>Here are the examples. Let's say I have an image 3x4 pixels. Width = 3, Height = 4, the amount of data in each cell - <code>N = Width * Height = 3 * 4 = 12</code>. Let's make the matrix:</p>
  
  <p><img src="https://i.stack.imgur.com/f5kvN.jpg" alt="Here it is"></p>
  
  <p>The formula to easily go through the matrix is <code>y*Width + x</code>. And the formula for our rotating 180 degrees is:</p>
  
  <pre><code>N - Width + x - y * Width
  </code></pre>
  
  <p>So we have:</p>
  
  <pre><code>DataOut [y * Width + x] = DataIn [N - Width + x - y * Width]
  </code></pre>
  
  <p>Using this formula we get:</p>
  
  <p><img src="https://i.stack.imgur.com/MnCNM.jpg" alt="this matrix"></p>
  
  <p>But I can't come up with the formula of rotating 90 degrees. Can you help me, please?</p>