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