title: Rotating a 2d matrix clockwise produces odd results
body: <p>Using the rotation matrix:</p>
  
  <pre><code>R(-t) = [cos(t)  sin(t)]
          [-sin(t) cos(t)]
  </code></pre>
  
  <p>where t = angle in radians</p>
  
  <p>functionally expressed as:</p>
  
  <pre><code>[x] = [cos(t)  sin(t)][x]
  [y]   [-sin(t) cos(t)][y]
  </code></pre>
  
  <p>or</p>
  
  <pre><code>x' =  x * cos(t) + y * sin(t)
  y' = -x * sin(t) + y * cos(t)
  </code></pre>
  
  <p>i'm rotating a 2d matrix clockwise by 30 degrees with the dimensions 150x150.</p>
  
  <p>in code it looks like this:</p>
  
  <pre><code>    cv::Mat m = cv::Mat::zeros(150, 150, CV_8UC1);
      cv::Mat nm = cv::Mat::zeros(150, 150, CV_8UC1);
      cv::rectangle(m, cv::Rect(0,0,150,150), cv::Scalar(100,100,100),-1);
  
      double rad = (30.0 * M_PI/180.0);
      for (int c = 0; c &lt; m.cols;c++){
          for (int r = 0; r &lt; m.rows;r++){
              int x = (m.cols*0.5)-c;
              int y = (m.rows*0.5)-r;
              int xn = (x*cos(rad) + y*sin(rad));
              int yn = (-x*sin(rad) + y*cos(rad));
              xn += (m.cols*0.5);
              yn += (m.rows*0.5);
              if (xn&lt;0||xn&gt;=m.cols||yn&lt;0||yn&gt;=m.rows){
                  continue;
              }
              nm.at&lt;uchar&gt;(xn,yn)=m.at&lt;uchar&gt;(c,r);    
          }
      }
  </code></pre>
  
  <p>Mathematically it looks correct but these are the results I'm getting</p>
  
  <p>Unrotated matrix:</p>
  
  <p><img src="https://i.stack.imgur.com/94dLp.png" alt="Unrotated matrix"></p>
  
  <p>Rotated matrix:</p>
  
  <p><img src="https://i.stack.imgur.com/uuuiL.png" alt="Rotated matrix"></p>
  
  <p>the rotated matrix appears grainy. What causes this? How is a it fixed? I'd prefer an explanation mathematically.</p>
  
  <p>PS. Please do not point to opencv's precooked methods, I am aware that they exist.</p>