protected function plot($gd, $x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3)
{
/* plot any cubic Bezier curve */
$n = 0;
$i = 0;
$xc = $x0 + $x1 - $x2 - $x3;
$xa = $xc - 4 * ($x1 - $x2);
$xb = $x0 - $x1 - $x2 + $x3;
$xd = $xb + 4 * ($x1 + $x2);
$yc = $y0 + $y1 - $y2 - $y3;
$ya = $yc - 4 * ($y1 - $y2);
$yb = $y0 - $y1 - $y2 + $y3;
$yd = $yb + 4 * ($y1 + $y2);
$fx0 = $x0;
$fx1 = 0;
$fx2 = 0;
$fx3 = 0;
$fy0 = $y0;
$fy1 = 0;
$fy2 = 0;
$fy3 = 0;
$t1 = $xb * $xb - $xa * $xc;
$t2 = 0;
$t = array();
/* sub-divide curve at gradient sign changes */
if ($xa == 0) {
/* horizontal */
if (abs($xc) < 2 * abs($xb)) {
$t[$n++] = $xc / (2.0 * $xb);
}
/* one change */
} else {
if ($t1 > 0.0) {
/* two changes */
$t2 = sqrt($t1);
$t1 = ($xb - $t2) / $xa;
if (abs($t1) < 1.0) {
$t[$n++] = $t1;
}
$t1 = ($xb + $t2) / $xa;
if (abs($t1) < 1.0) {
$t[$n++] = $t1;
}
}
}
$t1 = $yb * $yb - $ya * $yc;
if ($ya == 0) {
/* vertical */
if (abs($yc) < 2 * abs($yb)) {
$t[$n++] = $yc / (2.0 * $yb);
}
/* one change */
} else {
if ($t1 > 0.0) {
/* two changes */
$t2 = sqrt($t1);
$t1 = ($yb - $t2) / $ya;
if (abs($t1) < 1.0) {
$t[$n++] = $t1;
}
$t1 = ($yb + $t2) / $ya;
if (abs($t1) < 1.0) {
$t[$n++] = $t1;
}
}
}
for ($i = 1; $i < $n; $i++) {
if (($t1 = $t[$i - 1]) > $t[$i]) {
$t[$i - 1] = $t[$i];
$t[$i] = $t1;
$i = 0;
}
}
$t1 = -1.0;
$t[$n] = 1.0;
/* begin / end point */
for ($i = 0; $i <= $n; $i++) {
/* plot each segment separately */
$t2 = $t[$i];
/* sub-divide at $t[$i-1], $t[$i] */
$fx1 = ($t1 * ($t1 * $xb - 2 * $xc) - $t2 * ($t1 * ($t1 * $xa - 2 * $xb) + $xc) + $xd) / 8 - $fx0;
$fy1 = ($t1 * ($t1 * $yb - 2 * $yc) - $t2 * ($t1 * ($t1 * $ya - 2 * $yb) + $yc) + $yd) / 8 - $fy0;
$fx2 = ($t2 * ($t2 * $xb - 2 * $xc) - $t1 * ($t2 * ($t2 * $xa - 2 * $xb) + $xc) + $xd) / 8 - $fx0;
$fy2 = ($t2 * ($t2 * $yb - 2 * $yc) - $t1 * ($t2 * ($t2 * $ya - 2 * $yb) + $yc) + $yd) / 8 - $fy0;
$fx0 -= $fx3 = ($t2 * ($t2 * (3 * $xb - $t2 * $xa) - 3 * $xc) + $xd) / 8;
$fy0 -= $fy3 = ($t2 * ($t2 * (3 * $yb - $t2 * $ya) - 3 * $yc) + $yd) / 8;
$x3 = floor($fx3 + 0.5);
$y3 = floor($fy3 + 0.5);
/* scale bounds to int */
if ($fx0 != 0.0) {
$fx1 *= $fx0 = ($x0 - $x3) / $fx0;
$fx2 *= $fx0;
}
if ($fy0 != 0.0) {
$fy1 *= $fy0 = ($y0 - $y3) / $fy0;
$fy2 *= $fy0;
}
if ($x0 != $x3 || $y0 != $y3) {
$this->plotCubicSegment($gd, $x0, $y0, $x0 + $fx1, $y0 + $fy1, $x0 + $fx2, $y0 + $fy2, $x3, $y3);
}
$x0 = $x3;
$y0 = $y3;
$fx0 = $fx3;
$fy0 = $fy3;
$t1 = $t2;
}
}