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