PROGRAM Bresenham;
PROCEDURE DrawPixel (x,y: INTEGER);
BEGIN
WRITELN ('(x,y) = (',x:3,',',y:3,')')
END {DrawPixel};
PROCEDURE DrawLine (xStart,yStart, xEnd,yEnd: INTEGER);
{Bresenham's Line Algorithm. Byte, March 1988, pp. 249-253.}
VAR
a,b : INTEGER; {displacements in x and y}
d : INTEGER; {decision variable}
diag_inc : INTEGER; {d's increment for diagonal steps}
dx_diag : INTEGER; {diagonal x step for next pixel}
dx_nondiag : INTEGER; {nondiagonal x step for next pixel}
dy_diag : INTEGER; {diagonal y step for next pixel}
dy_nondiag : INTEGER; {nondiagonal y step for next pixel}
i : INTEGER; {loop index}
nondiag_inc: INTEGER; {d's increment for nondiagonal steps}
swap : INTEGER; {temporary variable for swap}
x,y : INTEGER; {current x and y coordinates}
BEGIN {DrawLine}
x := xStart; {line starting point}
y := yStart;
{Determine drawing direction and step to the next pixel.}
a := xEnd - xStart; {difference in x dimension}
b := yEnd - yStart; {difference in y dimension}
{Determine whether end point lies to right or left of start point.}
IF a < 0 {drawing towards smaller x values?}
THEN BEGIN
a := -a; {make 'a' positive}
dx_diag := -1
END
ELSE
dx_diag := 1;
{Determine whether end point lies above or below start point.}
IF b < 0 {drawing towards smaller x values?}
THEN BEGIN
b := -b; {make 'a' positive}
dy_diag := -1
END
ELSE
dy_diag := 1;
{Identify octant containing end point.}
IF a < b
THEN BEGIN
swap := a;
a := b;
b := swap;
dx_nondiag := 0;
dy_nondiag := dy_diag
END
ELSE BEGIN
dx_nondiag := dx_diag;
dy_nondiag := 0
END;
d := b + b - a; {initial value for d is 2*b - a}
nondiag_inc := b + b; {set initial d increment values}
diag_inc := b + b - a - a;
FOR i := 0 TO a DO BEGIN {draw the a+1 pixels}
DrawPixel (x,y);
IF d < 0 {is midpoint above the line?}
THEN BEGIN {step nondiagonally}
x := x + dx_nondiag;
y := y + dy_nondiag;
d := d + nondiag_inc {update decision variable}
END
ELSE BEGIN {midpoint is above the line; step diagonally}
x := x + dx_diag;
y := y + dy_diag;
d := d + diag_inc
END
END;
WRITELN;
READLN
END {DrawLine};
BEGIN
DrawLine (0,0, 10, 4);
DrawLine (0,0, 4, 10);
DrawLine (0,0, -4, 10);
DrawLine (0,0,-10, 4);
DrawLine (0,0,-10, -4);
DrawLine (0,0, -4,-10);
DrawLine (0,0, 4,-10);
DrawLine (0,0, 10, -4)
END {Bresenham}.