factor/examples/mandel.factor

107 lines
2.0 KiB
Factor
Raw Normal View History

2004-11-09 12:25:13 -05:00
! Graphical mandelbrot fractal renderer.
!
2004-12-25 18:08:20 -05:00
! To run this code, bootstrap Factor like so:
2004-11-09 12:25:13 -05:00
!
2004-12-25 18:08:20 -05:00
! ./f boot.image.le32
! -libraries:sdl:name=libSDL.so
! -libraries:sdl-gfx:name=libSDL_gfx.
!
! (But all on one line)
!
! Then, start Factor as usual (./f factor.image) and enter this
! at the listener:
2004-11-09 12:25:13 -05:00
!
! "examples/mandel.factor" run-file
2004-11-09 12:25:13 -05:00
IN: mandel
2004-12-17 21:46:19 -05:00
USE: compiler
2004-11-09 12:25:13 -05:00
USE: alien
USE: errors
USE: kernel
USE: lists
USE: math
USE: namespaces
USE: sdl
USE: sdl-event
USE: sdl-gfx
USE: sdl-video
2004-11-09 12:25:13 -05:00
USE: vectors
USE: prettyprint
USE: stdio
USE: test
: scale 255 * >fixnum ;
: scale-rgb ( r g b a -- n )
2004-11-09 12:25:13 -05:00
scale
swap scale 8 shift bitor
swap scale 16 shift bitor
swap scale 24 shift bitor ;
: sat 0.85 ;
: val 0.85 ;
: <color-map> ( nb-cols -- map )
[
2004-11-09 12:25:13 -05:00
dup [
dup 360 * pick 1 + / 360 / sat val
2005-01-23 16:47:28 -05:00
hsv>rgb 1.0 scale-rgb ,
] repeat
] make-vector nip ;
2004-11-09 12:25:13 -05:00
2004-12-17 21:46:19 -05:00
: absq >rect swap sq swap sq + ; inline
2004-11-09 12:25:13 -05:00
: iter ( c z nb-iter -- x )
over absq 4 >= over 0 = or [
nip nip
] [
1 - >r sq dupd + r> iter
2004-12-25 18:08:20 -05:00
] ifte ;
2004-11-09 12:25:13 -05:00
: max-color 360 ;
SYMBOL: zoom-fact
SYMBOL: x-inc
SYMBOL: y-inc
SYMBOL: nb-iter
SYMBOL: cols
SYMBOL: center
: init-mandel ( -- )
width get 200000 zoom-fact get * / x-inc set
height get 150000 zoom-fact get * / y-inc set
nb-iter get max-color min <color-map> cols set ;
: c ( i j -- c )
2005-01-23 16:47:28 -05:00
>r
2004-11-09 12:25:13 -05:00
x-inc get * center get real x-inc get width get 2 / * - + >float
r>
y-inc get * center get imaginary y-inc get height get 2 / * - + >float
2004-12-25 18:08:20 -05:00
rect> ;
2004-11-09 12:25:13 -05:00
: render ( -- )
2005-01-23 16:47:28 -05:00
[
2004-11-09 12:25:13 -05:00
c 0 nb-iter get iter dup 0 = [
drop 0
] [
cols get [ vector-length mod ] keep vector-nth
] ifte
2004-12-25 18:08:20 -05:00
] with-pixels ; compiled
2004-11-09 12:25:13 -05:00
: mandel ( -- )
640 480 0 SDL_HWSURFACE [
2004-11-09 12:25:13 -05:00
[
0.8 zoom-fact set
-0.65 center set
100 nb-iter set
2004-12-25 18:08:20 -05:00
init-mandel
2004-11-09 12:25:13 -05:00
[ render ] time
"Done." print flush
] with-surface
<event> event-loop
SDL_Quit
] with-screen ;
mandel