home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
source
/
graph.seq
< prev
next >
Wrap
Text File
|
1990-07-25
|
8KB
|
231 lines
\\ LINE-DRAWING ALGORITHM by JOHNNY GRAVES modified by Tom Zimmer
Simple graphics support.
This algorithm, LINE, is written in high-level FORTH, and thus is
slower relatively slow. It has the advantage, however, that it
works properly in the EGA, VGA640, and VEGA modes.
Issue GRAPH-INIT before trying to use PLOT or POINT. Then select a
graphics mode from the list (for example 320x200x16). After this is
done, you can set the COLOR of plotting, and start plotting points.
For hercules graphics, execute GRAPH-INIT, then HERCULES to enter
graphics mode. Set COLOR to 1 and start plotting.
The VALUE's HDOTS, VDOTS and #COLORS are initialize for the maximum
horizontal resolution, vertical resolution and colors of the graphics
mode selected.
Requires HERC.SEG.
{
FORTH DECIMAL
CODE VIDEO ( DX CX BX AX -- DX AX ) \ perform a VIDEO interrupt call.
POP AX POP BX POP CX POP DX
PUSH SI PUSH BP
INT $10
POP BP POP SI
2push END-CODE
: VMODE! ( n1 -- ) \ use to set video modes. n1 is the
\ desired mode number. For example
\ 6 VMODE! will select 640x200
\ black & white graphics.
>R 0 0 0 R> VIDEO 2DROP ;
0 VALUE VMODE-SAVE
DEFER PLOT ( col row -- ) \ Plot point at pixel 'col row'.
DEFER POINT ( col row -- color ) \ Get pixel color at 'col row'.
DEFER TEXT-MODE ( -- ) \ return to text mode.
: CTEXT ( -- )
VMODE-SAVE VMODE! ;
CODE CGA.PALETTE ( bh bl -- ) \ bh=palette #, bl=background color
POP BX
POP AX
MOV BH, AL
MOV AH, # $0B
PUSH BP PUSH SI
INT $10
POP SI POP BP
NEXT END-CODE
CODE CPLOT ( col row -- )
POP DX
POP CX
MOV AL, COLOR
MOV AH, # $0C
SUB BX, BX
PUSH BP MOV DI, SI
INT $10
MOV SI, DI POP BP
NEXT END-CODE
: CPOINT ( col row -- color ) \ get the color of a point on CGA
\ graphics screen.
SWAP 0 $0D00 VIDEO NIP ;
: GRAPH-INIT ( -- F1 ) \ init graphics plot word returns TRUE if COLOR
\ and FALSE if HERCULES
?VMODE DUP =: VMODE-SAVE 7 = DUP
IF ['] HPLOT IS PLOT ['] HPOINT IS POINT
['] HTEXT IS TEXT-MODE
ELSE ['] CPLOT IS PLOT ['] CPOINT IS POINT
['] CTEXT IS TEXT-MODE
THEN 0= ;
: CMODE! ( mode horiz vert colors seg len -- )
=: VID.SIZE =: VID.SEG
=: #COLORS =: VDOTS =: HDOTS VMODE! ;
\ Graph mode Mode Hor Ver Clrs Seg Size
: 320x200x4 ( -- ) $04 320 200 4 $B800 $4000 CMODE! ; \ CGA
: 320x200x4M ( -- ) $05 320 200 4 $B800 $4000 CMODE! ; \ CGA COMPOSITE
: 640x200x2 ( -- ) $06 640 200 2 $B800 $4000 CMODE! ; \ CGA
: 320x200x16 ( -- ) $0D 320 200 16 $A000 $9600 CMODE! ; \ EGA,VGA
: 640x200x16 ( -- ) $0E 640 200 16 $A000 $9600 CMODE! ; \ EGA,VGA
: 640x350x2 ( -- ) $0F 640 350 2 $A000 $9600 CMODE! ; \ EGA,VGA MONO
: 640x350x16 ( -- ) $10 640 350 16 $A000 $9600 CMODE! ; \ EGA,VGA
: 640x480x2 ( -- ) $11 640 480 2 $A000 $9600 CMODE! ; \ EGA,VGA
: 640x480x16 ( -- ) $12 640 480 16 $A000 $9600 CMODE! ; \ VGA
: 320x200x256 ( -- ) $13 320 200 256 $A000 $FA00 CMODE! ; \ VGA
: 800x600x16 ( -- ) $16 800 600 16 $A000 $FA00 CMODE! ; \ VEGA
\ Line drawing, high level
VARIABLE X1 VARIABLE X2
VARIABLE Y1 VARIABLE Y2
VARIABLE ERR
VARIABLE DELX VARIABLE DELY
VARIABLE HALFX VARIABLE HALFY
VARIABLE CHEC
: LINEINIT ( -- )
X1 @ X2 @ - ABS DUP DELX ! 2/
HALFX ! Y1 @ Y2 @ - ABS DUP DELY ! 2/ HALFY ! 0 ERR !
DELY @ DELX @ > CHEC ! ;
: X1=X2 ( -- f ) X1 @ X2 @ = ;
: Y1=Y2 ( -- f ) Y1 @ Y2 @ = ;
( LINE ALGORITHM )
: DO-1 X1 @ Y1 @ PLOT X1 INCR DELY @ ERR +!
HALFX @ ERR @ < IF DELX @ NEGATE ERR +! Y1 INCR THEN ;
: DO-2 X1 @ Y1 @ PLOT X1 INCR DELY @ ERR +!
HALFX @ ERR @ < IF DELX @ NEGATE ERR +! Y1 DECR THEN ;
: DO-3 X1 @ Y1 @ PLOT X1 DECR DELY @ ERR +!
HALFX @ ERR @ < IF DELX @ NEGATE ERR +! Y1 INCR THEN ;
: DO-4 X1 @ Y1 @ PLOT X1 DECR DELY @ ERR +!
HALFX @ ERR @ < IF DELX @ NEGATE ERR +! Y1 DECR THEN ;
: DO-1' X1 @ Y1 @ PLOT Y1 INCR DELX @ ERR +!
HALFY @ ERR @ < IF DELY @ NEGATE ERR +! X1 INCR THEN ;
: DO-2' X1 @ Y1 @ PLOT Y1 DECR DELX @ ERR +!
HALFY @ ERR @ < IF DELY @ NEGATE ERR +! X1 INCR THEN ;
: DO-3' X1 @ Y1 @ PLOT Y1 INCR DELX @ ERR +!
HALFY @ ERR @ < IF DELY @ NEGATE ERR +! X1 DECR THEN ;
: DO-4' X1 @ Y1 @ PLOT Y1 DECR DELX @ ERR +!
HALFY @ ERR @ < IF DELY @ NEGATE ERR +! X1 DECR THEN ;
: 1/1' CHEC @ IF BEGIN DO-1' Y1=Y2 UNTIL
ELSE BEGIN DO-1 X1=X2 UNTIL THEN ;
: 2/2' CHEC @ IF BEGIN DO-2' Y1=Y2 UNTIL
ELSE BEGIN DO-2 X1=X2 UNTIL THEN ;
: 3/3' CHEC @ IF BEGIN DO-3' Y1=Y2 UNTIL
ELSE BEGIN DO-3 X1=X2 UNTIL THEN ;
: 4/4' CHEC @ IF BEGIN DO-4' Y1=Y2 UNTIL
ELSE BEGIN DO-4 X1=X2 UNTIL THEN ;
: 2S-OR-3S X1 @ X2 @ < IF 2/2' ELSE 3/3' THEN ;
: 1S-OR-4S X1 @ X2 @ < IF 1/1' ELSE 4/4' THEN ;
: LINE? X1 @ X2 @ < Y1 @ Y2 @ < XOR
IF 2S-OR-3S
ELSE 1S-OR-4S THEN ;
: DO-I BEGIN X1 @ Y1 @ PLOT X1 INCR X1=X2 UNTIL ;
: DO-II BEGIN X1 @ Y1 @ PLOT X1 DECR X1=X2 UNTIL ;
: DO-J BEGIN X1 @ Y1 @ PLOT Y1 INCR Y1=Y2 UNTIL ;
: DO-JJ BEGIN X1 @ Y1 @ PLOT Y1 DECR Y1=Y2 UNTIL ;
: IORII? X1 @ X2 @ < IF DO-I ELSE DO-II THEN ;
: JORJJ? Y1 @ Y2 @ < IF DO-J ELSE DO-JJ THEN ;
: HOR-OR-VER? X1 @ X2 @ = IF JORJJ? ELSE IORII? THEN ;
: LINETO ( x y -- ) \ draw a line to x,y from previous
\ LINETO, LINE or LINEFROM.
Y2 ! X2 ! LINEINIT
X1 @ X2 @ = Y1 @ Y2 @ = XOR
IF HOR-OR-VER? ELSE LINE? THEN
X1 @ Y1 @ PLOT ;
: LINEFROM ( x y -- ) \ set origin of next LINETO.
Y1 ! X1 ! ;
: LINE ( X1 Y1 X2 Y2 -- )
2SWAP LINEFROM LINETO ;
\ LINE ALGORITHM 23Jun88mds
comment:
LINE ALGORITHM USER'S GUIDE
key in the endpoints of the segment you wish to draw followed by the
command : LINE.
example : entering, 1 2 3 4 LINE,
a segment with endpoints [1,2] and [3,4] {unscaled} will be drawn. if
you wish to continue drawing from [3,4], simply key in the next
endpoint followed by the command, LINETO.
example : to draw two segments from [1,2] to [3,4] to [7,8],
key in ; 1 2 3 4 LINE, followed by; 7 8 LINETO.
The primed ['] procedures draw non-horizontal, non-vertical segments
whose slopes are greater than one, and the corresponding unprimed
procedures draw non-horizontal, non-vertical lines whose slopes are
less than one. the "i" and "j" procedures take care of horizontal and
vertical lines, respectively. The commands: 1/1', 2/2', 3/3', 4/4',
2s-or-3s, 1s-or-4s, line?, iorii?, jorjj?, hor-or-ver? simply
determine which algorithm is to be used. Finally, unless otherwise
noted in the body of the program, commands in the program neither
expect or leave anything on the stack.
comment;