title: MATLAB - Smooth heat map from (x, y, z) points within a triangle?
body: <p>I have many 3D scatter points <code>(x, y, z)</code> that are guaranteed to be within a triangle. I now wish to visualize <code>z</code> as one <strong>smooth</strong> 2D heat map, where positions are given by <code>(x, y)</code>.</p>
  
  <p>I can easily do it with <code>meshgrid</code> and <code>mesh</code>, <em>if</em> <code>(x, y)</code> together form a rectangle. Because I don't want anything falling outside of my triangle, I can't use <code>griddate</code> either. </p>
  
  <p>Then how?</p>
  
  <p><strong>MWE</strong></p>
  
  <pre><code>P = [0 1/sqrt(3); 0.5 -0.5/sqrt(3); -0.5 -0.5/sqrt(3)];
  % Vertices
  scatter(P(:, 1), P(:, 2), 100, 'ro');
  hold on;
  % Edges
  for idx = 1:size(P, 1)-1
      plot([P(idx, 1) P(idx+1, 1)], [P(idx, 2) P(idx+1, 2)], 'r');
  end
  plot([P(end, 1) P(1, 1)], [P(end, 2) P(1, 2)], 'r');
  % Sample points within the triangle
  N = 1000; % Number of points
  t = sqrt(rand(N, 1));
  s = rand(N, 1);
  sample_pts = (1-t)*P(1, :)+bsxfun(@times, ((1-s)*P(2, :)+s*P(3, :)), t);
  % Colors for demo
  C = ones(size(sample_pts, 1), 1).*sample_pts(:, 1);
  % Scatter sample points
  scatter(sample_pts(:, 1), sample_pts(:, 2), [], C, 'filled');
  colorbar;
  </code></pre>
  
  <p>produces</p>
  
  <p><img src="https://i.stack.imgur.com/REcIp.png" alt="enter image description here"></p>
  
  <p><strong>PS</strong></p>
  
  <p>As suggested by Nitish, increasing number of points will do the trick. But is there a more computationally cheap way of doing so?</p>