title: Vector projection/rejection in C#?
body: <p>I'm trying to implement a function for <a href="http://en.wikipedia.org/wiki/Vector_projection" rel="nofollow noreferrer">vector rejection</a> in C#. That is:</p>
  
  <p><img src="https://i.stack.imgur.com/HMivO.png" alt="enter image description here"></p>
  
  <p>I have been trying to code this formula in C# however for some reason it's always returning zero. This is what I have until now:</p>
  
  <pre><code>private Vector3 Projection(Vector3 vectorA, Vector3 vectorB) {
      Vector3 a2 = Vector3.Scale ( Divide (Vector3.Scale (vectorA, vectorB), Vector3.Scale (vectorB, vectorB)), vectorB);
      return a2;
  }
  
  private Vector3 Rejection(Vector3 vectorA, Vector3 vectorB) {
      Vector3 a2 = vectorA - Projection(vectorA, vectorB);
      return a2;
  }
  
  private Vector3 Divide(Vector3 a, Vector3 b) {
      Vector3 c = new Vector3 ();
      c.x = a.x / b.x;
      c.y = a.y / b.y;
      c.z = a.z / b.z;
      return c;
  }
  
  private void Example() {
      Vector3 a = new Vector3 (5, 5, 0);
      Vector3 b = new Vector3 (0, 10, 0);
      Vector3 c = Rejection(a, b); // Returns (NaN, 0, NaN)
  }
  
  // The Vector3 class represents a 3D vector and it's x, y and z components are of float type. It's [scale method][3] multiplies two vectors component wise.
  </code></pre>
  
  <p>Example:</p>
  
  <p>Imagine Vector A is a force applied to an object that moves along the ground and Vector B is the gravity (which is perpendicular to the ground). When Vector A is applied to the object the trajectory it moves should be a third vector perpendicular to the gravity, which can be thought of as A travelling perpendicular to B. That's is the rejection of A from B.</p>
  
  <p><img src="https://i.stack.imgur.com/iqjTQ.png" alt="enter image description here"></p>