title: Explanation of nearest neighbor interpolation algorithm for image rotation
body: <p>I am trying to understand the implementation of nearest neighbor algorithm for image rotation. The conventional nearest neighbor algorithms I know, calculate some explicit euclidean distances between different points and take the point with the lowest euclidean distance as the best point. But in image interpolation, I do not find any explicit euclidean distance in the implementation. I am referring to <a href="https://stackoverflow.com/a/1843931/10019073">this</a> answer to another similar question. The given solution perfectly rotates an input image by the given angle. But I have many questions about the code (I do not understand what they are doing). </p>
  
  <p>1.) Why does the author multiply by <code>sqrt(2)</code> for the new indices (lines 4 and 5) ?</p>
  
  <p>2.) What is the author doing in the following lines of code (to be precise, I understand he is multiplying the indices by rotation matrix. But why does he have the extra terms like <code>m/2</code>, <code>n/2</code>, <code>t-mm/2</code> and <code>s-nn/2</code> ? What is he doing with <code>if i&gt;0 &amp;&amp; j&gt;0 &amp;&amp; i&lt;=m &amp;&amp; j&lt;=n</code> ?) ? :</p>
  
  <pre><code>for t=1:mm
     for s=1:nn
        i = uint16((t-mm/2)*cos(thet)+(s-nn/2)*sin(thet)+m/2);
        j = uint16(-(t-mm/2)*sin(thet)+(s-nn/2)*cos(thet)+n/2);
        if i&gt;0 &amp;&amp; j&gt;0 &amp;&amp; i&lt;=m &amp;&amp; j&lt;=n           
           im2(t,s,:)=im1(i,j,:);
        end
     end
  end
  </code></pre>
  
  <p>Any help will be much appreciated !</p>