1 ! Copyright (c) 2012 Anonymous
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays kernel locals math math.functions math.vectors
4 rosetta-code.bitmap rosetta-code.bitmap-line sequences ;
5 IN: rosetta-code.bitmap-bezier
7 ! http://rosettacode.org/wiki/Bitmap/Bézier_curves/Cubic
9 ! Using the data storage type defined on this page for raster
10 ! images, and the draw_line function defined in this other one,
11 ! draw a cubic bezier curves (definition on Wikipedia).
13 :: (cubic-bezier) ( P0 P1 P2 P3 -- bezier )
16 1 x - sq 3 * x * P1 n*v
17 1 x - 3 * x sq * P2 n*v
21 ! gives an interval of x from 0 to 1 to map the bezier function
22 : t-interval ( x -- interval )
23 [ iota ] keep 1 - [ / ] curry map ;
25 ! turns a list of points into the list of lines between them
26 : points-to-lines ( seq -- seq )
27 dup rest [ 2array ] 2map ;
29 : draw-lines ( {R,G,B} points image -- )
30 [ [ first2 ] dip draw-line ] curry with each ;
32 :: bezier-lines ( {R,G,B} P0 P1 P2 P3 image -- )
33 ! 100 is an arbitrary value.. could be given as a parameter..
34 100 t-interval P0 P1 P2 P3 (cubic-bezier) map
36 {R,G,B} swap image draw-lines ;