title: Rotate normal vector onto axis plane
body: <p>I have a set of data points in 3D space which apparently all fall onto a specific plane. I use PCA to compute the plane parameters. The 3rd component of PCA gives me the normal vector of the plane (weakest component).</p>
  
  <p>What I want to do next is to transform all the points onto said plane and look at it in 2D. </p>
  
  <p>My idea was to do the following:</p>
  
  <ul>
  <li>Find a center point (average point) on the plane</li>
  <li>Substract it from all data points to arrange them around the origin</li>
  <li>Rotate the normal so that it becomes (0,0,-1)</li>
  <li>Apply this rotation to all data points</li>
  <li>Use orthogonal projection (basically, skip z axis)</li>
  </ul>
  
  <p>Now I'm stuck at finding the right rotation operation. I tried working with acos or atan and setting up two rotation matrices. Seems both methods (using acos, using atan) give me the wrong result. Perhaps you can help me out here!</p>
  
  <p>Matlab code follows:</p>
  
  <pre><code>b = atan(n(1) / n(2));
  rotb = [cos(b) -sin(b) 0; sin(b) cos(b) 0; 0 0 1];
  n2 = n * rotb;
  a = atan(n(1) / n(3));
  rota = [cos(a) 0 sin(a); 0 1 0; -sin(a) 0 cos(a)];
  n3 = n2 * rotaows:
  </code></pre>
  
  <p>I expect <code>n2</code> to have y component of zero. However that fails already for the vector (-0.6367, 0.7697, 0.0467).</p>