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