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

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