title: Interpolation of a triangle
body: <p>I have a unit right triangle and a value at each of the 3 vertices.
  I need to interpolate to find the value at a point inside the triangle.
  Hours of searching have turned up nothing that actually tells me how to do this.
  Here is my closest attempt, which is actually pretty close but not quite right -</p>
  
  <pre><code>                result = 
                  v1 * (1 - x) * (1 - y) +
                  v2 * x * (1 - y) +
                  v3 * x * y;
  </code></pre>
  
  <p>v1, v2, and v3 are the values at the 3 vertices of the triangle.
  (x, y) is the point in the triangle that you are trying to find the value of.</p>
  
  <p>Any kind of method would help me here. It doesn't necessarily need to be a unit/right triangle.</p>
  
  <p>Updated info:
  I have a grid of evenly spaced points and a value at each point.
  I make a triangle out of the nearest 3 points on the grid.
  Here is a picture to illustrate it - 
  <img src="https://i.stack.imgur.com/4nXkr.jpg" alt="enter image description here"><br>
  So I have to interpolate between 5, 3, and 7 to find the value of x.
  The point could also be inside the other triangle, meaning you would interpolate between 5, 7, and the value of the bottom left corner of the square.   </p>
  
  <p>In the code I showed, v1 = 5, v2 = 3, v3 = 7.<br>
  x is the fractional distance (range [0-1]) in the "x" direction, and y is the fractional distance in the "y" direction.<br>
  In the picture's example, x would probably be about 0.75 and y would be about 0.2</p>
  
  <p>Here are my closest attempts -<br>
  <img src="https://i.stack.imgur.com/Ah7fm.png" alt="enter image description here"><br>
  Created using -  </p>
  
  <pre><code>        if (x &gt; y) //if x &gt; y then the point is in the upper right triangle
              return
                  v1 * (1 - x) * (1 - y) +
                  v2 * x * (1 - y) +
                  v3 * x * y;
          else //bottom left triangle
              return
                  v1 * (1 - x) * (1 - y) +
                  v4 * (1 - x) * y +
                  v3 * x * y;
  </code></pre>
  
  <p>And another attempt -<br>
  <img src="https://i.stack.imgur.com/XLQJg.png" alt="enter image description here"><br>
  Created using -  </p>
  
  <pre><code>if (x &gt; y)
              return
                  (1 - x) * v1 + (x - y) * v2 + y * v3;
          else
              return
                  (1 - y) * v1 + (y - x) * v4 + x * v3;
  </code></pre>
  
  <p>They're both close to what I need but obviously not quite right.</p>