\ Midpoint line drawing algorithm \ (after an idea of C.H. Ting) \ \ --------------------------------------------------- \ (c) Copyright 2001 Julian V. Noble. \ \ Permission is granted by the author to \ \ use this software for any application pro- \ \ vided this copyright notice is preserved. \ \ --------------------------------------------------- \ This is an ANS Forth program \ Environmental dependencies: \ Assumes non-Standard graphics words from Win32Forth MARKER -mid \ non-Standard Win32Forth graphics words WinDC theDC : set_plot ( -- ) CONDC PutHandle: theDC \ initialize DC to the console 0 0 800 600 BLUE FillArea: theDC WHITE BrushColor: theDC ; : put_dot ( x y --) 255 255 255 rgb \ set the color buffer to "white" -ROT SWAP GetHandle: theDC Call SetPixelV DROP ; \ Line-drawing via midpoint algorithm : 2tuck 2swap 2over ; : v+ ( a b c d -- a+c b+d) locals| d c b a | a c + b d + ; : v2/ 2/ swap 2/ swap ; ( a b -- a/2 b/2) : nearly ( end1 end2 -- f) LOCALS| d c b a | a c - ABS 2 < b d - ABS 2 < AND ; 4 value scale : plot_dot ( a b --) scale / swap scale / swap put_dot ; : midpoint ( end1 end2 --) 4dup nearly IF 2DROP 2DROP EXIT THEN 4dup v+ v2/ 2tuck 2dup plot_dot recurse recurse ; : (draw_line) ( v1 v2 --) scale * swap scale * swap 2swap scale * swap scale * swap 4dup plot_dot plot_dot midpoint ; : draw_line hide-cursor set_plot (draw_line) BEGIN KEY? UNTIL \ loop until key pressed KEY DROP CLS \ clean up show-cursor ;