]> gitweb.factorcode.org Git - factor.git/blob - extra/benchmark/mandel/mandel.factor
64a673c8ec9faa7df8520f6f5fec69a30dc17422
[factor.git] / extra / benchmark / mandel / mandel.factor
1 ! Copyright (C) 2005, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: io kernel math math.functions sequences prettyprint
4 io.files io.encodings io.encodings.ascii io.encodings.binary fry
5 benchmark.mandel.params benchmark.mandel.colors ;
6 IN: benchmark.mandel
7
8 : x-inc width  200000 zoom-fact * / ; inline
9 : y-inc height 150000 zoom-fact * / ; inline
10
11 : c ( i j -- c )
12     [ x-inc * center real-part x-inc width 2 / * - + >float ]
13     [ y-inc * center imaginary-part y-inc height 2 / * - + >float ] bi*
14     rect> ; inline
15
16 : count-iterations ( z max-iterations step-quot test-quot -- #iters )
17     '[ drop @ dup @ ] find-last-integer nip ; inline
18
19 : pixel ( c -- iterations )
20     [ C{ 0.0 0.0 } max-iterations ] dip
21     '[ sq _ + ] [ absq 4.0 >= ] count-iterations ; inline
22
23 : color ( iterations -- color )
24     [ color-map [ length mod ] keep nth ] [ B{ 0 0 0 } ] if* ; inline
25
26 : render ( -- )
27     height [ width swap '[ _ c pixel color write ] each ] each ; inline
28
29 : ppm-header ( -- )
30     ascii encode-output
31     "P6\n" write width pprint " " write height pprint "\n255\n" write
32     binary encode-output ; inline
33
34 : mandel-main ( -- )
35     "mandel.ppm" temp-file binary [ ppm-header render ] with-file-writer ;
36
37 MAIN: mandel-main