PHP Zeichnungslinie Pixel
/**
* Draw a line using Bresenham's line algorithm.
*
* @param resource $im
* The image resource.
* @param int $x0
* The x part of the starting coordinate.
* @param int $y0
* The y part of the starting coordinate.
* @param int $x1
* The x part of the ending coordinate.
* @param int $y1
* The y part of the ending coordinate.
* @param int $color
* The color of the line, created from imagecolorallocate().
*/
function drawLine($im, $x0, $y0, $x1, $y1, $color) {
if ($x0 == $x1 && $y0 == $y1) {
// Start and finish are the same.
imagesetpixel($im, $x0, $y0, $color);
return;
}
$dx = $x1 - $x0;
if ($dx < 0) {
// x1 is lower than x0.
$sx = -1;
} else {
// x1 is higher than x0.
$sx = 1;
}
$dy = $y1 - $y0;
if ($dy < 0) {
// y1 is lower than y0.
$sy = -1;
} else {
// y1 is higher than y0.
$sy = 1;
}
if (abs($dy) < abs($dx)) {
// Slope is going downwards.
$slope = $dy / $dx;
$pitch = $y0 - $slope * $x0;
while ($x0 != $x1) {
imagesetpixel($im, $x0, round($slope * $x0 + $pitch), $color);
$x0 += $sx;
}
} else {
// Slope is going upwards.
$slope = $dx / $dy;
$pitch = $x0 - $slope * $y0;
while ($y0 != $y1) {
imagesetpixel($im, round($slope * $y0 + $pitch), $y0, $color);
$y0 += $sy;
}
}
// Finish by adding the final pixel.
imagesetpixel($im, $x1, $y1, $color);
}
steamboatid