wolffd@0: load ('def.lefty'); wolffd@0: definit (); wolffd@0: # wolffd@0: # initialize window data wolffd@0: # wolffd@0: canvas = defcanvas; wolffd@0: wrect = [0 = ['x' = 0; 'y' = 0;]; 1 = ['x' = 400; 'y' = 500;];]; wolffd@0: setwidgetattr (canvas, ['window' = wrect;]); wolffd@0: wolffd@0: sq = function (x) { wolffd@0: return x * x; wolffd@0: }; wolffd@0: wolffd@0: # data structures wolffd@0: # wolffd@0: length = 300; wolffd@0: center = ['x' = 200; 'y' = 250;]; wolffd@0: radius = 2 * length / sqrt (12); wolffd@0: fractalangle = 0; wolffd@0: maxlevel = 2; wolffd@0: wolffd@0: # drawing functions wolffd@0: # wolffd@0: # draw a Koch curve (a ``snowflake'' fractal) wolffd@0: # wolffd@0: # start with a triangle and keep replacing edges wolffd@0: # with the construct: _/\_ wolffd@0: # until the recursion level reaches 'maxlevel' wolffd@0: # wolffd@0: fractal = function (level, length, angle) { wolffd@0: local nlength, newpenpos; wolffd@0: wolffd@0: if (level >= maxlevel) { wolffd@0: newpenpos.x = penpos.x + length * cos (angle); wolffd@0: newpenpos.y = penpos.y + length * sin (angle); wolffd@0: line (canvas, null, penpos, newpenpos, ['color' = 1;]); wolffd@0: penpos = newpenpos; wolffd@0: return; wolffd@0: } wolffd@0: nlength = length / 3; wolffd@0: fractal (level + 1, nlength, angle); wolffd@0: fractal (level + 1, nlength, angle + 60); wolffd@0: fractal (level + 1, nlength, angle - 60); wolffd@0: fractal (level + 1, nlength, angle); wolffd@0: }; wolffd@0: drawfractal = function () { wolffd@0: clear (canvas); wolffd@0: setpick (canvas, center, wrect); wolffd@0: penpos = [ wolffd@0: 'x' = center.x + cos (fractalangle + 210) * radius; wolffd@0: 'y' = center.y + sin (fractalangle + 210) * radius; wolffd@0: ]; wolffd@0: fractal (0, length, fractalangle + 60); wolffd@0: fractal (0, length, fractalangle - 60); wolffd@0: fractal (0, length, fractalangle - 180); wolffd@0: remove ('penpos'); wolffd@0: }; wolffd@0: wolffd@0: # editing functions wolffd@0: # wolffd@0: # transform the fractal. wolffd@0: # wolffd@0: # map point 'prevpoint' to point 'currpoint' wolffd@0: # with respect to the center of the fractal. wolffd@0: # wolffd@0: transformfractal = function (prevpoint, currpoint) { wolffd@0: local prevtan, currtan, prevradius, currradius; wolffd@0: wolffd@0: prevtan = atan (prevpoint.y - center.y, prevpoint.x - center.x); wolffd@0: currtan = atan (currpoint.y - center.y, currpoint.x - center.x); wolffd@0: fractalangle = fractalangle + (currtan - prevtan); wolffd@0: prevradius = sqrt ( wolffd@0: sq (prevpoint.y - center.y) + sq (prevpoint.x - center.x) wolffd@0: ); wolffd@0: currradius = sqrt ( wolffd@0: sq (currpoint.y - center.y) + sq (currpoint.x - center.x) wolffd@0: ); wolffd@0: radius = radius / prevradius * currradius; wolffd@0: length = radius / 2 * sqrt (12); wolffd@0: }; wolffd@0: wolffd@0: # user interface functions wolffd@0: # wolffd@0: # bind changes to the fractal to user actions wolffd@0: # wolffd@0: leftup = function (data) { wolffd@0: transformfractal (data.ppos, data.pos); wolffd@0: drawfractal (); wolffd@0: }; wolffd@0: dops = function () { wolffd@0: local s; wolffd@0: wolffd@0: s = ['x' = 8 * 300; 'y' = 10.5 * 300;]; wolffd@0: canvas = createwidget (-1, ['type' = 'ps'; 'size' = s;]); wolffd@0: setwidgetattr (canvas, ['window' = wrect;]); wolffd@0: drawfractal (); wolffd@0: destroywidget (canvas); wolffd@0: canvas=defcanvas; wolffd@0: }; wolffd@0: transformfractal (['x' = 0; 'y' = 0;], ['x' = 0; 'y' = 0;]); wolffd@0: drawfractal ();