title: Creating a linear gradient in 2D array
body: <p>I have a 2D bitmap-like array of let's say 500*500 values. I'm trying to create a linear gradient on the array, so the resulting bitmap would look something like this (in grayscale):
  <a href="https://i.stack.imgur.com/Zdn9Q.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Zdn9Q.jpg" alt="Example gradient"></a><br>
  <sub>(source: <a href="http://showandtell-graphics.com/images/gradient/gradient_12.jpg" rel="nofollow noreferrer">showandtell-graphics.com</a>)</sub>  </p>
  
  <p>The input would be the array to fill, two points (like the starting and ending point for the Gradient tool in Photoshop/GIMP) and the range of values which would be used.</p>
  
  <p>My current best result is this:</p>
  
  <p><a href="http://img222.imageshack.us/img222/1733/gradientfe3.png" rel="nofollow noreferrer">alt text http://img222.imageshack.us/img222/1733/gradientfe3.png</a></p>
  
  <p>...which is nowhere near what I would like to achieve. It looks more like a radial gradient.</p>
  
  <p>What is the simplest way to create such a gradient? I'm going to implement it in C++, but I would like some general algorithm.</p>