title: Add midpoints into array and use them in the next iteration
body: <p>It sounds very basic but I can't get my head around this:<br>
  I have two points representing X:Y coordinates<br>
  In the first iteration i want to add the midpoint of the two into the array...<br>
  In the next iteration i want to add the midpoint of the first and the second and then the midpoint between the second and the third one into the array... and so on...<br>
  (Im adding random amounts to the y axis to do midpoint displacement)
  E.g.<br>
  0:10, 10:10<br>
  first iteration...<br>
  0:10, 5:13, 10:10<br>
  Second iteration...<br>
  0:10, 2.5:12, 5:13, 7.5:15, 10:10<br>
  etc. etc.  </p>
  
  <p>this is the code i ended up with after trying to get at least something to work:</p>
  
  <pre><code>&lt;?php
   header('Content-type: image/png');
    $png_image = imagecreate(1024, 1024);
    imagecolorallocate($png_image, 15, 142, 210);
    $black = imagecolorallocate($png_image, 0, 0, 0);
      imagesetthickness($png_image, 10);
  
  $iterations = 5;
  $noise = 10;
  
  $points = array("0:512","1023:512");
  
  for($iteration=0; $iteration &lt; $iterations; $iteration++){
  
          $new_array = array();
          ksort($points);
      for($i = 0; $i &lt; sizeof($points)-1 ;$i++){
          $previous = array("X" =&gt; split(":", $points[$i])[0], "Y" =&gt; split(":", $points[$i])[1] );
          $next  = array("X" =&gt; split(":", $points[$i+1])[0], "Y" =&gt; split(":", $points[$i+1])[1] );
  
          $midpoint = ($previous["X"] + $next["X"])/2;
          $midheight = (($previous["Y"] + $next["Y"])/2)+rand(0,$noise);
  
          $npoint="$midpoint:$midheight";
  
  
          array_push($new_array, $npoint);
      }
      $points =  array_merge($points, $new_array);
  
  }
  
  
  ksort($points);
  for($i=0;$i &lt; sizeof($points)-1;$i++){
          $previous = array("X" =&gt; split(":", $points[$i])[0], "Y" =&gt; split(":", $points[$i])[1] );
          $current  = array("X" =&gt; split(":", $points[$i+1])[0], "Y" =&gt; split(":", $points[$i+1])[1] );
       // imageline($png_image, (int)$previous["X"], (int)$previous["Y"], (int)$current["X"], (int)$current["Y"], $black);
      // imagefilledellipse ( $png_image ,(int)$previous["X"], (int)$previous["Y"] , 8, 8 , $black );
      imagestring ($png_image , 4 , (int)$previous["X"], (int)$previous["Y"] , $i , $black);
  
  }
  
  
   imagepng($png_image);
   imagedestroy($png_image);
  ?&gt;
  </code></pre>
  
  <p>for some reason it adds points at the same x sometimes but with different y value.</p>
  
  <p>EDIT:attempt with a function  </p>
  
  <pre><code>$iterations = 5;
  $noise = 10;
  
  $points = array("0:512","1023:512");
  
  $cit = 0;
  function divide($pointArray){
      global $noise, $cit, $iterations;  
      $arrayLength = sizeof($pointArray);
      $tempArray = $pointArray;
      for($i = 0; $i &lt; $arrayLength-1 ;$i++){
          $currentPoint = array("X" =&gt; split(":", $pointArray[$i])[0], "Y" =&gt; split(":", $pointArray[$i])[1] );
          $nextPoint  = array("X" =&gt; split(":", $pointArray[$i+1])[0], "Y" =&gt; split(":", $pointArray[$i+1])[1] );
  
          $midpoint = ($currentPoint["X"] + $nextPoint["X"])/2;
          $midheight = (($currentPoint["Y"] + $nextPoint["Y"])/2)+rand(0,$noise);
  
          $npoint="$midpoint:$midheight";
          array_splice( $tempArray, $i+$i, 0, $npoint );
          $cit++;
          if($cit &lt; $iterations){
              divide($tempArray);
          }else{
              return $tempArray;
          }
  </code></pre>