home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Hack-Phreak Scene Programs
/
cleanhpvac.zip
/
cleanhpvac
/
DRAWLINE.ZIP
/
DRAWLINE.DOC
next >
Wrap
Text File
|
1992-02-06
|
5KB
|
116 lines
─ (F) Zone 3 Pascal Programming (3:635/552@fidonet) ──────────────── Z3_PASCAL ─
Msg : 77 of 90 - 67
From : Frank Malcolm 3:712/505 Sat 01 Feb 92 15:54
To : Ben Burns
Subj : Bresenham?
────────────────────────────────────────────────────────────────────────────────
Ben, hi.
BB> a) What is Bresenham's line drawing algorithm? If
BB> someone would be willing to leave the algorithm here
BB> it would be much apreciated. also
J. E. Bresenham wrote "Algorithm for Computer Control of a Digital Plotter"
in the IBM Systems Journal in 1965 (yes, 1965!). It's described in most
books on graphics programming and I believe it's still regarded as the
fastest way to draw lines. Can also be adapted to other shapes. It seems to
be the method used by Turbo Pascal, so you won't get any improvement by
coding it yourself (but you'll learn heaps!)
Wilton's version in "Programmer's Guide to PC & PS/2 Video Systems" is
separately optimised (different versions) for each of the different display
modes, and looks for all the special cases (vertical, horizontal). It's
fast.
A Pascal version appeared in APC in April '88 and very similarly in another
book I've got. I'll post it here, but if you can get the original article
the description is helpful for understanding.
procedure draw_line (xstart, ystart, xend, yend: integer);
{ Bresenham's line-drawing algorithm. APC April '88. }
{ See also Using Turbo & IBM Pascal p364, and Newman & Sproull }
var x, y, { current x and y coordinates }
d, { decision variable }
a, b, { line displacements in x and y }
dx_diag, { diagonal x step for next pixel }
dy_diag, { diagonal y step for next pixel }
dx_nondiag, { non-diagonal x step for next pixel }
dy_nondiag, { non-diagonal y step for next pixel }
diag_inc, { d increment for diagonal steps }
nondiag_inc, { d increment for non-diagonal steps }
swap, { temporary variable for swaps }
i: integer;
begin { draw_line }
x := xstart; { line starting point }
y := ystart;
{ Determine drawing direction and step to next pixel. }
a := xend - xstart; { Calculate difference in x }
b := yend - ystart; { Calculate difference in y }
{ Determine whether ending point lies to right or left of starting point. }
if a < 0 then { Drawing towards smaller x-values? }
begin { Yes, because a is negative }
a := -a; { Make a positive }
dx_diag := -1; { and set x-movement accordingly }
end
else { Draw is towards larger x-values. }
dx_diag := 1; { Set x-movement towards larger x. }
{ Determine whether ending point lies above or below starting point. }
if b < 0 then { Drawing towards smaller y-values? }
begin { Yes, because b is negative }
b := -b; { Make b positive }
dy_diag := -1; { and set y-movement accordingly }
end
else
dy_diag := 1; { Set y-movement towards larger y. }
{ Identify octant containing ending point. }
if a < b then { Is y-difference larger than x-difference? }
begin { Yes, so swap a and b }
swap := a;
a := b;
b := swap;
dx_nondiag := 0; { Since y difference is larger, x doesn't change on }
dy_nondiag := dy_diag; { non-diagonal steps but y changes every step. }
end
else
begin { When x difference is larger than y difference, x }
dx_nondiag := dx_diag; { changes every step and y changes only on the }
dy_nondiag := 0; { diagonal steps. }
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 { Draw the a + 1 pixels. }
begin
plot (x, y, 1);
if d < 0 then { Is mid-point above the line? }
begin { Yes, step non-diagonally. }
x := x + dx_nondiag;
y := y + dy_nondiag;
d := d + nondiag_inc; { Update the decision variable. }
end
else { Mid-point is below the line, so step diagonally. }
begin
x := x + dx_diag;
y := y + dy_diag;
d := d + diag_inc; { Update the decision variable. }
end
end
end { draw_line };
BB> b) Would somebody be willing to lend me a copy of
BB> `The programmers
BB> Guide to PC & PS/2 Video Systems'.. I dont have enough
BB> money to actually buy it
I have it, but use it frequently. Are you in Sydney?
Regards, FIM
--- via Silver Xpress V2.28 [NR]
* Origin: Sydney PC Users Group COMPAQ BBS (3:712/505)