// bresn.pas
// file which shows how to draw a line

program bresn;

uses Graph;

procedure initialize;
// initialize graphics
var Gm, Gd : integer;
begin
   Gd := DETECT;
   initgraph( gd, gm, '' );
end;

procedure drawLine( xStart, yStart, xEnd, yEnd : integer );
// draws line
var dx, dy, E, NE, d, x, y : integer;
begin
   dx := xEnd-xStart;
   dy := yEnd-yStart;
   d := 2*dy-dx;
   E := 2*dy;
   NE := 2*(dy-dx);
   x := xStart;
   y := yStart;
   putpixel( x, y, WHITE );
   while x < xEnd do
      begin
         if d <= 0 then
            begin
               d := d+E;
               x := x+1;
            end
         else
            begin
               d := d+NE;
               x := x+1;
               y := y+1;
            end;
         putpixel( x, y, WHITE );
      end;
end;

// The way Bresenham's algorithm (also known as midpoint algorithm) works
// basically plotting the line raster by raster rather than producing it
// by brute force with a formula.  Obviously, its better than using the
// formula since it allows for vertical lines and uses no multiplication
// or division.  The algorithm in its entirety uses 4 line algorithms for
// slopes of 0 to 1, 1 to infinity, 0 to -1, and -1 to -infinity.  The
// algorithm above shows just works for slopes 0 to -1, since the other
// three are easily obtained with a few simple modifications.
// What the algorithm does, in this case, is that it draws a line from
// xStart,yStart to xEnd,yEnd.  It then computes the distance of the midpoint
// between the two pixels to the line itself, KEEPING THE SIGN.  If the sign
// is positive, then the plotter goes down one line as well as over one pixel.
// If negative, then the plotter remains at the same line.  The plotter then
// goes to the next point and repeats the process.  Bresenham's is extremely
// useful for anti-aliased lines.  If you want, e-mail me and I'll send
// details on that as well.

// chetan deshpande