factor/extra/benchmark/mandel/mandel.factor

72 lines
1.6 KiB
Factor
Raw Normal View History

2007-09-20 18:09:08 -04:00
IN: benchmark.mandel
2008-04-26 12:03:41 -04:00
USING: arrays io kernel math math.order namespaces sequences
2008-03-11 22:58:50 -04:00
byte-arrays byte-vectors math.functions math.parser io.files
colors.hsv io.encodings.binary ;
2007-09-20 18:09:08 -04:00
: max-color 360 ; inline
: zoom-fact 0.8 ; inline
: width 640 ; inline
: height 480 ; inline
: nb-iter 40 ; inline
: center -0.65 ; inline
: scale 255 * >fixnum ; inline
: scale-rgb ( r g b -- n )
rot scale rot scale rot scale 3array ;
: sat 0.85 ; inline
: val 0.85 ; inline
: <color-map> ( nb-cols -- map )
dup [
360 * swap 1+ / sat val
3array hsv>rgb first3 scale-rgb
2008-01-09 17:36:30 -05:00
] with map ;
2007-09-20 18:09:08 -04:00
: iter ( c z nb-iter -- x )
over absq 4.0 >= over zero? or
[ 2nip ] [ 1- >r sq dupd + r> iter ] if ; inline
SYMBOL: cols
: x-inc width 200000 zoom-fact * / ; inline
: y-inc height 150000 zoom-fact * / ; inline
: c ( i j -- c )
>r
x-inc * center real-part x-inc width 2 / * - + >float
2007-09-20 18:09:08 -04:00
r>
y-inc * center imaginary-part y-inc height 2 / * - + >float
2007-09-20 18:09:08 -04:00
rect> ; inline
: render ( -- )
height [
width swap [
c 0 nb-iter iter dup zero? [
drop "\0\0\0"
] [
cols get [ length mod ] keep nth
] if %
] curry each
] each ;
: ppm-header ( w h -- )
"P6\n" % swap # " " % # "\n255\n" % ;
2008-03-11 22:58:50 -04:00
: buf-size width height * 3 * 100 + ;
2007-09-20 18:09:08 -04:00
2008-03-11 22:58:50 -04:00
: mandel ( -- data )
2007-09-20 18:09:08 -04:00
[
2008-03-11 22:58:50 -04:00
buf-size <byte-vector> building set
2007-09-20 18:09:08 -04:00
width height ppm-header
nb-iter max-color min <color-map> cols set
render
2008-03-11 22:58:50 -04:00
building get >byte-array
2007-09-20 18:09:08 -04:00
] with-scope ;
2007-11-25 00:50:12 -05:00
: mandel-main ( -- )
2008-03-11 22:58:50 -04:00
mandel "mandel.ppm" temp-file binary set-file-contents ;
2007-09-20 18:09:08 -04:00
MAIN: mandel-main