1 ! Copyright (c) 2012 Anonymous
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays kernel math ranges rosetta-code.bitmap sequences ;
4 IN: rosetta-code.bitmap-line
6 ! http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm
8 ! Using the data storage type defined on this page for raster
9 ! graphics images, draw a line given 2 points with the Bresenham's
12 :: line-points ( pt1 pt2 -- points )
13 pt1 first2 :> y0! :> x0!
14 pt2 first2 :> y1! :> x1!
15 y1 y0 - abs x1 x0 - abs > :> steep
27 deltay deltax / abs :> deltaerr
30 y0 y1 < [ 1 ystep! ] [ -1 ystep! ] if
32 y steep [ swap ] when 2array
33 current-error deltaerr + current-error!
34 current-error 0.5 >= [
36 current-error 1 - current-error!
40 ! Needs rosetta-code.bitmap for the set-pixel function and to create the image
41 : draw-line ( {R,G,B} pt1 pt2 image -- )
43 [ set-pixel ] curry with each ;