title: Storing mousePosition more than once a frame/ Aproximating points between two given poins
body: <p>I'm trying to paint &quot;pixels&quot;. I created Conway's game of life in Unity and i want to add a feature where you press mouse button and when it's pressed you &quot;paint&quot; - set cells alive. So my idea was:</p>
  <p>In Update() if mouse button is pressed Start Coroutine.</p>
  <p>In Coroutine you have loop that sets cell pointed by Input.mousePosition to Alive-state then waits for end of frame. Loop, and by that Coroutine ends when that mouse button is released.</p>
  <p>My problem is that if you move mouse rapidly created line will not be continuous, because inputs form mouse from two frames will be different (far apart).</p>
  <p>Since you can take Input.mousePosition only once per frame i tried approximating this by storing mousePosition from previous frame and calculating all points that lie on Edge between CurrentMousePosition and PreviousMousePosition</p>
  <p>However i was not happy with the result.</p>
  <p>My Question is: is there a better way to prevent this un-continuous line than letting it be and then fixing it? And if not is there a better way to approximate points that lie on Edge?</p>
  <pre><code>void Update()
  {
    if (Input.GetMouseButtonDown(1))
    {
      StartCoroutine(BrushPixel());
    }
    UpdatePlane(true);
  }
  
  IEnumerator BrushPixel()
          {
              int prevX = 0;
              int prevY = 0;
              bool firstIteratrion = true;
              while (true)
              {
                  IntVector3 currentPos = new IntVector3(Camera.main.ScreenToWorldPoint(Input.mousePosition));
                  int xPos = currentPos.x + sizeX/2;
                  int yPos = currentPos.y + sizeY/2;
  
                  if (xPos &gt;= 0 &amp;&amp; xPos &lt; sizeX &amp;&amp; yPos &gt;= 0 &amp;&amp; yPos &lt; sizeY)
                  {
                      if (firstIteratrion)
                      {
                          firstIteratrion = false;
                          prevX = xPos;
                          prevY = yPos;
                      }
  
                      mainMatrix[xPos, yPos] = true;
                      Queue&lt;IntVector3&gt; pointsAlong = AproximatePoints(xPos, yPos, prevX, prevY);
                      while(pointsAlong.Count != 0)
                      {
                          IntVector3 temp = pointsAlong.Dequeue();
                          if(temp.x &gt;= 0 &amp;&amp; temp.x &lt; sizeX &amp;&amp;  temp.y &gt;= 0 &amp;&amp;  temp.y &lt; sizeY)
                          {
                              mainMatrix[temp.x, temp.y] = true;
                          }
                      }
                      UpdatePlane();
                      prevX = xPos;
                      prevY = yPos;
                  }
                  if (Input.GetKeyUp(KeyCode.Mouse1))
                  {
                      break;
                  }
                  yield return null;
              }
          }
  
  public Queue&lt;IntVector3&gt; AproximatePoints (int curX, int curY, int prevX, int prevY)
          {
              Queue&lt;IntVector3&gt; additional = new Queue&lt;IntVector3&gt;();
              if(curX == prevX &amp;&amp; curY == prevY)
              {
                  return additional;
              }
              int distance = (int)Math.Sqrt( (double)((curX - prevX) * (curX - prevX))  +  (double)((curY - prevY) * (curY - prevY)) );
              int count = distance;
              int d = distance / count;
              double angle = Math.Atan2(curX - prevX, curY - prevY);
              for (int i = 0; i &lt; count; i++)
              {
                  additional.Enqueue(new IntVector3((int)Math.Floor(curX + i * d * Math.Cos(angle)), (int)Math.Floor(curY + i * d * Math.Cos(angle)), 0));
              }
              return additional;
          }
  
  </code></pre>
  <p>Additional notes:
  MainMatrix is bool[sizeX, sizeY] that holds states of current generation where true is Alive and false is dead.
  In line</p>
  <pre><code>IntVector3 currentPos = new IntVector3(Camera.main.ScreenToWorldPoint(Input.mousePosition));
  int xPos = currentPos.x + sizeX/2;
  int yPos = currentPos.y + sizeY/2;
  </code></pre>
  <p>i translate Input from mouse to addres in MainMatrix.
  IntVector3 is my struct just like Unity Vector3 but holds x, y, z as Integers.
  UpdatePlane() is generating image that will be changed.
  UpdatePlane(bool k) is generating image that will be changed and calculates next generation of cells.</p>
  <p>Current approximation gives pic1 when trying to draw line form UpperLeft corner to LowerRight corner. <a href="https://i.stack.imgur.com/DgTy5.png" rel="nofollow noreferrer">pic1</a></p>