YMIN) { for ($j = 0; $j < count($bmis); $j++) { if ($bmis[$j] > ($i / (HEIGHT * HEIGHT))) { break; } } dashLine($im, $y); } } for ($i = ceil(YMIN / (HEIGHT*HEIGHT)); $i < ceil(YMAX / (HEIGHT*HEIGHT)); $i++) { $y = GHEIGHT - 45 - ($i * HEIGHT * HEIGHT - YMIN) * ((GHEIGHT - 90) / (YMAX - YMIN)); imageline($im, GWIDTH - 56, $y, GWIDTH - 53, $y, $black); imagestring($im, 1, GWIDTH - 50, $y - 4, $i, $black); if ($i % 5 != 0) { for ($j = 0; $j < count($bmis); $j++) { if ($bmis[$j] > $i) { break; } } dashLine2($im, $y); } } fillBMI($im, 0, 18.5, $colours[0], "Underweight"); fillBMI($im, 18.5, 25, $colours[1], "Normal"); fillBMI($im, 25, 30, $colours[2], "Overweight"); fillBMI($im, 30, 35, $colours[3], "Obese class I"); fillBMI($im, 35, 40, $colours[4], "Obese class II"); fillBMI($im, 40, 100, $colours[5], "Obese class III"); $lx = $ly = 0; $points = array(); for($i = 0; $i < XMAX; $i++) { $x = 65 + ($i - XMIN) * ((GWIDTH - 120) / (XMAX - XMIN)); if ($lx != 0) { //imageline($im, $lx, $ly, $x, $y, $ly > $y ? $red : $black); } imageline($im, $x, GHEIGHT - 46, $x, GHEIGHT - 43, $black); if ($i % 2 == 0) { imagestring($im, 1, $x - 2, GHEIGHT - 40, substr($i + 1, 0, 1), $black); if ($i >= 9) { imagestring($im, 1, $x - 2, GHEIGHT - 32, substr($i + 1, 1, 1), $black); if ($i >= 99) { imagestring($im, 1, $x - 2, GHEIGHT - 24, substr($i + 1, 2, 1), $black); } } } #imageline($im, $x - 4, $y - 4, $x + 4, $y + 4, $white); #imageline($im, $x + 4, $y - 4, $x - 4, $y + 4, $white); if (isset($data[$i])) { $weight = $data[$i]; $y = GHEIGHT - 45 - ($weight - YMIN) * ((GHEIGHT - 90) / (YMAX - YMIN)); $points[] = array($x, $y); if ($i % 5 == 4) { //dashLine3($im, $x, $y, imagecolorallocatealpha($im, 0x66, 0x66, 0x66, 10));// $i % 5 == 4 ? $black : $grey); //imagearc($im, $x, $y, 10, 10, 0, 360, $red); }// else { imagefilledarc($im, $x, $y, 3, 3, 0, 360, $red, IMG_ARC_PIE); //} $lx = $x; $ly = $y; } } foreach ($targets as $target) { list($week, $bmi) = $target; $weight = $bmi * HEIGHT * HEIGHT; if ($week > 0) { $x = 65 + (($week - 1) - XMIN) * ((GWIDTH - 120) / (XMAX - XMIN)); $y = GHEIGHT - 45 - ($weight - YMIN) * ((GHEIGHT - 90) / (YMAX - YMIN)); imageline($im, $x - 3, $y - 3, $x + 3, $y + 3, $red); imageline($im, $x - 3, $y + 3, $x + 3, $y - 3, $red); } } $avg = array(); $avg[] = $points[0]; for ($i = 0; $i < XMAX - XMIN - 5; $i += 5) { $tx = $ty = 0; for ($j = 0; $j < 5; $j++) { list($x, $y) = $points[$i + $j]; $tx += $x; $ty += $y; } $avg[] = array($tx/5, $ty/5); } $colours = array($red, $black); foreach (array(/*$avg,*/ $points) as $points) { $colour = array_shift($colours); $lx = $ly = 0; for ($i = 0; $i < count($points) - 1; $i++) { $func = interpolate($points, $i, $i + 1); list($x1,$y1) = $points[$i]; list($x2,$y2) = $points[$i + 1]; for ($t = 0; $t < 1; $t += 4 / ($x2 - $x1)) { $x = round($x1 + ($x2 - $x1) * $t, 0); $y = round(call_user_func($func, $t), 0); if ($lx != 0) { imagesmoothline($im, $lx, $ly, $x, $y, $red); } $lx = $x; $ly = $y; } } } function h00($t) { return 2 * pow($t, 3) - 3 * pow($t, 2) + 1; } function h10($t) { return pow($t, 3) - 2 * pow($t, 2) + $t; } function h01($t) { return -2 * pow($t, 3) + 3 * pow($t, 2); } function h11($t) { return pow($t, 3) - pow($t, 2); } function p($t, $p0, $m0, $p1, $m1, $h) { return h00($t) * $p0 + h10($t) * $h * $m0 + h01($t) * $p1 + h11($t) * $h * $m1; } function getLineGradient($a, $b) { list($x1,$y1) = $a; list($x2,$y2) = $b; $dx = $x2 - $x1; $dy = $y2 - $y1; if ($dx == 0) { return PHP_INT_MAX; } return $dy/$dx; } function getPointGradient($cur, $last, $next) { if ($last == null) { return getLineGradient($cur, $next); } if ($next == null) { return getLineGradient($last, $cur); } return (getLineGradient($cur, $next) + getLineGradient($last, $cur))/2; } function interpolate($data, $from, $to) { $prev = isset($data[$from - 1]) ? $data[$from - 1] : null; $next = isset($data[$to + 1]) ? $data[$to + 1] : null; $one = $data[$from]; $two = $data[$to]; list($x1, $y1) = $one; list($x2, $y2) = $two; $h = $x2 - $x1; $p0 = $y1; $p1 = $y2; $m0 = getPointGradient($prev, $one, $two); $m1 = getPointGradient($one, $two, $next); return create_function('$t', 'return p($t, ' . $p0 . ', ' . $m0 . ', ' . $p1 . ', ' . $m1 . ', ' . $h . ');'); } imagettftext($im, 8, 0, GWIDTH/2 - 50, GHEIGHT - 5, $black, FONT, 'Week number'); imagettftext($im, 8, 90, 30, GHEIGHT/2 + 20, $black, FONT, 'Mass (KG)'); imagettftext($im, 8, 270, GWIDTH - 25, GHEIGHT/2 - 40, $black, FONT, 'BMI (KG/m^2)'); imagettftext($im, 12, 0, GWIDTH/2 - 110, 30, $black, FONT, 'Graph of mass against time'); header('Content-type: image/png'); imagepng($im); ?>