! generate simple random fractals recursively ! this one is based on equilateral triangles ! Program to accompany "Problems in Computational Physics" by N. Giordano ! Copyright Prentice Hall 1997 program generate option nolet randomize call init input prompt "number of recursive levels -> ": level open #1: name "random_fractal."&str$(level), organization text, create new x_start = 0 ! initialize some variables then call show() y_start = 0 x_end = 300 y_end = 0 print #1: x_start,",",y_start call show(x_start,y_start,x_end,y_end,level,#1) close #1 end sub show(xi,yi,xf,yf,level,#1) ! recursive routine for drawing a random fractal if level <= 1 then ! have hit bottom - just draw a line plot lines: xi,yi;xf,yf print #1: xf,",",yf ! and save coordinates to a file else dx = (xf - xi) / 3 ! divide the interval into three parts dy = (yf - yi) / 3 ctheta = 2 * (rnd - 0.5) ! pick a random angle - get its cosine and sine stheta = sqr(1 - ctheta^2) ! next choose new points based on underlying ! "structure" of an equilateral triangle call show(xi,yi,xi+dx,yi+dy,level-1,#1) call show(xi+dx,yi+dy,xi+1.5*dx-stheta*dy,yi+1.5*dy+ctheta*dx,level-1,#1) call show(xi+1.5*dx-stheta*dy,yi+1.5*dy+ctheta*dx,xi+2*dx,yi+2*dy,level-1,#1) call show(xi+2*dx,yi+2*dy,xf,yf,level-1,#1) end if end sub sub init ! set up window for plotting set window 0,300,-100,200 clear end sub