Mandeltrix by Paul Johnston29th Aug 2003 23:32
|
---|
Summary This code will take a 32 iteration Mandelbrot set and map it onto a Dark Basic 3D matrix which you can then fly around using the mouse and arrow keys. Uses the matrix lines for ef Description ========================================================== Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com ` -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- ` Mandeltrix ` -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- ` By Rich Davey (rich@fatal-design.com) ` -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- ` Turns a mandlebrot set into a matrix ` you can fly around in with the mouse ` and arrow keys! ` ` Music listened to while coding this ` Dirty Trancing DJ Mix by Timo Maas. ` -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- sync rate 0 sync on hide mouse ` Calculate the mandelbrot and place into an array MinX#=-1.75; MaxX#=1.75; MinY#=-1.75; MaxY#=1.75 dx#=(MaxX#-MinX#)/80 dy#=(Maxy#-MinY#)/60 dim fractal#(4800) i=0 for y=0 to 60-1 for x=0 to 80-1 pixel_color#=calc_pixel(MinX#+x*dx#,MinY#+y*dy#)*10 fractal#(i)=pixel_color# inc i next x next y ` Make a texture for the matrix (saves having a graphic image file) x1=0; y1=0; x2=0; y2=0; shade=0; boxsize=4; gridsize=16 create bitmap 1,boxsize*gridsize,boxsize*gridsize set current bitmap 1 for b=0 to gridsize for c=0 to gridsize ink rgb(255,shade,0),rgb(0,0,0) box x1,y1,x1+boxsize+3,y1+boxsize+3 x1=x1+boxsize shade=shade+2 next c y1=y1+boxsize x1=0 next b get image 1,0,0,boxsize*gridsize,boxsize*gridsize delete bitmap 1 ` Create the matrix and map the height according to the array mw=3000; mh=3000; gx=80; gy=60 make matrix 1,mw,mh,gx,gy prepare matrix texture 1,1,gridsize,gridsize position matrix 1,0,0,0 set matrix wireframe on 1 color backdrop rgb(0,0,0) ` Map the fractal to the matrix i=0 for h=0 to gy-1 for w=0 to gx-1 set matrix height 1,w,h,fractal#(i) tilepic#=fractal#(i) if tilepic#>boxsize*gridsize then tilepic#=boxsize*gridsize set matrix tile 1,w,h,tilepic# inc i next w next h update matrix 1 ` Set-up the camera cx=mw/2; cy=400; cz=-200 position camera cx,cy,cz ` The main loop do cx#=wrapvalue(cx#+mousemovey()) cy#=wrapvalue(cy#+mousemovex()) cz#=wrapvalue(cz#+mousemovez()) rotate camera cx#,cy#,cz# if upkey()=1 then move camera 25 if downkey()=1 then move camera -25 sync loop ` End ` Fractal calculation function ` From the Dark Fractal code at www.darkforge.co.uk function calc_pixel(ca#,cbi#) max_iteration=32 a#=0; b#=0; iteration#=0 repeat old_a# = a# a# = a#*a# - b#*b# + ca# b# = 2 * old_a#*b# + cbi# length_z# = a#*a# + b#*b# inc iteration# until length_z#>4 or iteration#>max_iteration pixel_color#=iteration# endfunction pixel_color# |