annotate toolboxes/graph_visualisation/lib/lefty/fractal.lefty @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 load ('def.lefty');
wolffd@0 2 definit ();
wolffd@0 3 #
wolffd@0 4 # initialize window data
wolffd@0 5 #
wolffd@0 6 canvas = defcanvas;
wolffd@0 7 wrect = [0 = ['x' = 0; 'y' = 0;]; 1 = ['x' = 400; 'y' = 500;];];
wolffd@0 8 setwidgetattr (canvas, ['window' = wrect;]);
wolffd@0 9
wolffd@0 10 sq = function (x) {
wolffd@0 11 return x * x;
wolffd@0 12 };
wolffd@0 13
wolffd@0 14 # data structures
wolffd@0 15 #
wolffd@0 16 length = 300;
wolffd@0 17 center = ['x' = 200; 'y' = 250;];
wolffd@0 18 radius = 2 * length / sqrt (12);
wolffd@0 19 fractalangle = 0;
wolffd@0 20 maxlevel = 2;
wolffd@0 21
wolffd@0 22 # drawing functions
wolffd@0 23 #
wolffd@0 24 # draw a Koch curve (a ``snowflake'' fractal)
wolffd@0 25 #
wolffd@0 26 # start with a triangle and keep replacing edges
wolffd@0 27 # with the construct: _/\_
wolffd@0 28 # until the recursion level reaches 'maxlevel'
wolffd@0 29 #
wolffd@0 30 fractal = function (level, length, angle) {
wolffd@0 31 local nlength, newpenpos;
wolffd@0 32
wolffd@0 33 if (level >= maxlevel) {
wolffd@0 34 newpenpos.x = penpos.x + length * cos (angle);
wolffd@0 35 newpenpos.y = penpos.y + length * sin (angle);
wolffd@0 36 line (canvas, null, penpos, newpenpos, ['color' = 1;]);
wolffd@0 37 penpos = newpenpos;
wolffd@0 38 return;
wolffd@0 39 }
wolffd@0 40 nlength = length / 3;
wolffd@0 41 fractal (level + 1, nlength, angle);
wolffd@0 42 fractal (level + 1, nlength, angle + 60);
wolffd@0 43 fractal (level + 1, nlength, angle - 60);
wolffd@0 44 fractal (level + 1, nlength, angle);
wolffd@0 45 };
wolffd@0 46 drawfractal = function () {
wolffd@0 47 clear (canvas);
wolffd@0 48 setpick (canvas, center, wrect);
wolffd@0 49 penpos = [
wolffd@0 50 'x' = center.x + cos (fractalangle + 210) * radius;
wolffd@0 51 'y' = center.y + sin (fractalangle + 210) * radius;
wolffd@0 52 ];
wolffd@0 53 fractal (0, length, fractalangle + 60);
wolffd@0 54 fractal (0, length, fractalangle - 60);
wolffd@0 55 fractal (0, length, fractalangle - 180);
wolffd@0 56 remove ('penpos');
wolffd@0 57 };
wolffd@0 58
wolffd@0 59 # editing functions
wolffd@0 60 #
wolffd@0 61 # transform the fractal.
wolffd@0 62 #
wolffd@0 63 # map point 'prevpoint' to point 'currpoint'
wolffd@0 64 # with respect to the center of the fractal.
wolffd@0 65 #
wolffd@0 66 transformfractal = function (prevpoint, currpoint) {
wolffd@0 67 local prevtan, currtan, prevradius, currradius;
wolffd@0 68
wolffd@0 69 prevtan = atan (prevpoint.y - center.y, prevpoint.x - center.x);
wolffd@0 70 currtan = atan (currpoint.y - center.y, currpoint.x - center.x);
wolffd@0 71 fractalangle = fractalangle + (currtan - prevtan);
wolffd@0 72 prevradius = sqrt (
wolffd@0 73 sq (prevpoint.y - center.y) + sq (prevpoint.x - center.x)
wolffd@0 74 );
wolffd@0 75 currradius = sqrt (
wolffd@0 76 sq (currpoint.y - center.y) + sq (currpoint.x - center.x)
wolffd@0 77 );
wolffd@0 78 radius = radius / prevradius * currradius;
wolffd@0 79 length = radius / 2 * sqrt (12);
wolffd@0 80 };
wolffd@0 81
wolffd@0 82 # user interface functions
wolffd@0 83 #
wolffd@0 84 # bind changes to the fractal to user actions
wolffd@0 85 #
wolffd@0 86 leftup = function (data) {
wolffd@0 87 transformfractal (data.ppos, data.pos);
wolffd@0 88 drawfractal ();
wolffd@0 89 };
wolffd@0 90 dops = function () {
wolffd@0 91 local s;
wolffd@0 92
wolffd@0 93 s = ['x' = 8 * 300; 'y' = 10.5 * 300;];
wolffd@0 94 canvas = createwidget (-1, ['type' = 'ps'; 'size' = s;]);
wolffd@0 95 setwidgetattr (canvas, ['window' = wrect;]);
wolffd@0 96 drawfractal ();
wolffd@0 97 destroywidget (canvas);
wolffd@0 98 canvas=defcanvas;
wolffd@0 99 };
wolffd@0 100 transformfractal (['x' = 0; 'y' = 0;], ['x' = 0; 'y' = 0;]);
wolffd@0 101 drawfractal ();