title: Combining loops in algorithm
body: <p>I want to perform a specific-type of search.  I do not know if it has a name, but I can describe it, and have working code for its execution.</p>
  
  <p>For a 2-dimensional matrix, starting at point 0,0 and working down-right, the search generation would look like:</p>
  
  <ul>
  <li>&nbsp;&nbsp;1,&nbsp;&nbsp;4,&nbsp;&nbsp;9, 16, ...  </li>
  <li>&nbsp;&nbsp;2,&nbsp;&nbsp;3,&nbsp;&nbsp;8, 15, ... </li>
  <li>&nbsp;&nbsp;5,&nbsp;&nbsp;6,&nbsp;&nbsp;7, 14, ...</li>
  <li>10, 11, 12, 13, ...</li>
  <li>...</li>
  </ul>
  
  <p>So the first search loop would inspect 1, the second loop inspects 2, 3, 4, the third loop inspects 5, 6, 7, 8, 9, etc.</p>
  
  <p>The code to produce this search is:</p>
  
  <pre><code>$row_search = 0;
  $point_not_found = true;
  
  while ($point_not_found &amp;&amp; $row_search &lt; $matrix_height/2)
  {
      $current = array(0, $row_search);
  
      while ($current[0] &lt; $row_search)
      {
          if (searchForPoint( $matrix, $current ) !== false)
              $point_not_found = false;
  
          ++$current[0];
      }
  
      if (!$anchor_not_found)
          break;
  
      while ($current[1] &gt;= 0)
      {
          if (searchForPoint( $matrix, $current ) !== false)
              $point_not_found = false;
  
          --$current[1];
      }
  
      ++$row_search;
  }
  </code></pre>
  
  <p>I am unhappy with how the search is broken into two loops, as the code within the loops is nearly identical.  Can you suggest a way to either combine or nest the loops and eliminate redundant calls to searchForPoint?</p>