Ease data in array to smooth a curve in PHP -


i'm using api elevation data gpx points, , try create graphic representation of it. problem each point in api separated 90meters, , gpx points separated 5meters, causing several points in row have same altitude before changing abruptly new altitude.

basically, obtain array one:

[0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 15, 15, 15, 15, 15...]

how draw png image representation of altitudes while easing curves? need able change size of output picture.

i'm trying change array i'm not sure how , if best solution:

[0, 0, 0, 0, 5, 5, 10, 10, 10, 12, 13, 15, 15, 15, 15...]

thanks hints, i'm not used work on pictures , data easing.

here's basic way smooth points on "average" basis:

<?php $points = [0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 15, 15, 15, 15, 15]; $refined = [];  foreach($points $index => $point) {   // make sure don't divide 0   $prev = isset($points[$index - 1]) ? $points[$index - 1] : false;   $next = isset($points[$index + 1]) ? $points[$index + 1] : false;    if($point > 0 || ($prev && $prev > 0) || ($next && $next > 0)) {     $total = $point;     if($prev) {       $total += $prev;       $total = $total / 2;     }       if($next) {       $total += $next;       $total = $total / 2;     }      $refined[] = round($total, 0);   } else {     $refined[] = $point;   } }  echo implode(" ", $points); echo "<hr>"; echo implode(" ", $refined); 

results in:

0 0 0 0 0 10 10 10 10 10 15 15 15 15 15 --------------------------------------- 0 0 0 0 5 10 10 10 10 13 14 15 15 15 15 

to increase smoothing, you'll need more elaborate method has look-ahead's, look-behind's, , higher amount of sampling... interpolate between points -- excluded in sample above. interpolation, below:

<?php $points = [0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 15, 15, 15, 15, 15]; $refined = [];  foreach($points $index => $point) {   $prev = isset($points[$index - 1]) ? $points[$index - 1] : false;   $next = isset($points[$index + 1]) ? $points[$index + 1] : false;    if($point > 0 || ($prev && $prev > 0) || ($next && $next > 0)) {     $refined[] = $point;      while($next && $point < $next) {       $point++;       $refined[] = $point;     }   } else {     $refined[] = $point;   } }  echo implode(" ", $points); echo "<hr>"; echo implode(" ", $refined); 

which yield:

0 0 0 0 0 10 10 10 10 10 15 15 15 15 15 --------------------------------------------------------------------------- 0 0 0 0 0 1 2 3 4 5 6 7 8 9 10 10 10 10 10 10 11 12 13 14 15 15 15 15 15 15 

to draw image, we'll need more info. points in array not 2d... meaning there's no x or y, unless assume each point increases x axis 1 pixel? if so, here's rough shot:

$width = count($refined); $height = max($refined);  $gd = imagecreatetruecolor($width, $height);  // allocate color $red = imagecolorallocate($gd, 255, 0, 0);   foreach($refined $x => $y) {   imagesetpixel($gd, $x, $height-$y, $red); }  header('content-type: image/png'); imagepng($gd); 

see: http://codepad.viper-7.com/vsud1g


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -