annotate toolboxes/graph_visualisation/lib/lefty/fractal2.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 #
Daniel@0 2 # data structures
Daniel@0 3 #
Daniel@0 4 length = 300;
Daniel@0 5 center = ['x' = 200; 'y' = 250;];
Daniel@0 6 radius = 2 * length / sqrt (12);
Daniel@0 7 fractalangle = 0;
Daniel@0 8 maxlevel = 2;
Daniel@0 9 sizes = [
Daniel@0 10 'button' = [ 'x' = 100; 'y' = 40; ];
Daniel@0 11 'canvas' = [ 'x' = 400; 'y' = 500; ];
Daniel@0 12 'view' = [ 'x' = 400; 'y' = 600; ];
Daniel@0 13 ];
Daniel@0 14 sq = function (x) {
Daniel@0 15 return x * x;
Daniel@0 16 };
Daniel@0 17 #
Daniel@0 18 # create view and other widgets
Daniel@0 19 #
Daniel@0 20 init = function () {
Daniel@0 21 view = createwidget (-1, [
Daniel@0 22 'type' = 'view'; 'name' = 'fractal'; 'size' = sizes.view;
Daniel@0 23 ]);
Daniel@0 24
Daniel@0 25 array1 = createwidget (view, [
Daniel@0 26 'type' = 'array'; 'borderwidth' = 1; 'mode' = 'vertical';
Daniel@0 27 ]);
Daniel@0 28 widgets[array1].resize = resize;
Daniel@0 29
Daniel@0 30 array2 = createwidget (array1, [
Daniel@0 31 'type' = 'array'; 'borderwidth' = 1; 'mode' = 'horizontal';
Daniel@0 32 ]);
Daniel@0 33 widgets[array2].resize = resize;
Daniel@0 34
Daniel@0 35 array3 = createwidget (array2, [
Daniel@0 36 'type' = 'array'; 'borderwidth' = 1; 'mode' = 'vertical';
Daniel@0 37 ]);
Daniel@0 38 widgets[array3].resize = resize;
Daniel@0 39
Daniel@0 40 morebutton = createwidget (array3, [
Daniel@0 41 'type' = 'button'; 'text' = 'more';
Daniel@0 42 ]);
Daniel@0 43 widgets[morebutton].pressed = pressed;
Daniel@0 44 lessbutton = createwidget (array3, [
Daniel@0 45 'type' = 'button'; 'text' = 'less';
Daniel@0 46 ]);
Daniel@0 47 widgets[lessbutton].pressed = pressed;
Daniel@0 48 setwidgetattr (morebutton, ['size' = sizes.button;]);
Daniel@0 49 setwidgetattr (lessbutton, ['size' = sizes.button;]);
Daniel@0 50
Daniel@0 51 atext = createwidget (array2, [
Daniel@0 52 'type' = 'text'; 'mode' = 'oneline';
Daniel@0 53 ]);
Daniel@0 54 widgets[atext].oneline = oneline;
Daniel@0 55 setwidgetattr (atext, [
Daniel@0 56 'size' = ['x' = sizes.button.x; 'y' = sizes.button.y * 2;];
Daniel@0 57 ]);
Daniel@0 58
Daniel@0 59 scroll = createwidget (array1, ['type' = 'scroll';]);
Daniel@0 60 canvas = createwidget (scroll, ['type' = 'canvas';]);
Daniel@0 61 wrect = [0 = ['x' = 0; 'y' = 0;]; 1 = sizes.canvas;];
Daniel@0 62 setwidgetattr (canvas, ['window' = wrect; 'viewport' = wrect[1];]);
Daniel@0 63 };
Daniel@0 64 #
Daniel@0 65 # drawing functions
Daniel@0 66 #
Daniel@0 67 # draw a Koch curve (a ``snowflake'' fractal)
Daniel@0 68 #
Daniel@0 69 # start with a triangle and keep replacing edges
Daniel@0 70 # with the construct: _/\_
Daniel@0 71 # until the recursion level reaches 'maxlevel'
Daniel@0 72 #
Daniel@0 73 fractal = function (level, length, angle) {
Daniel@0 74 local nlength, newpenpos;
Daniel@0 75
Daniel@0 76 if (level >= maxlevel) {
Daniel@0 77 newpenpos.x = penpos.x + length * cos (angle);
Daniel@0 78 newpenpos.y = penpos.y + length * sin (angle);
Daniel@0 79 line (canvas, null, penpos, newpenpos, ['color' = 1;]);
Daniel@0 80 penpos = newpenpos;
Daniel@0 81 return;
Daniel@0 82 }
Daniel@0 83 nlength = length / 3;
Daniel@0 84 fractal (level + 1, nlength, angle);
Daniel@0 85 fractal (level + 1, nlength, angle + 60);
Daniel@0 86 fractal (level + 1, nlength, angle - 60);
Daniel@0 87 fractal (level + 1, nlength, angle);
Daniel@0 88 };
Daniel@0 89 redrawfractal = function () {
Daniel@0 90 clear (canvas);
Daniel@0 91 setpick (canvas, center, wrect);
Daniel@0 92 penpos = [
Daniel@0 93 'x' = center.x + cos (fractalangle + 210) * radius;
Daniel@0 94 'y' = center.y + sin (fractalangle + 210) * radius;
Daniel@0 95 ];
Daniel@0 96 fractal (0, length, fractalangle + 60);
Daniel@0 97 fractal (0, length, fractalangle - 60);
Daniel@0 98 fractal (0, length, fractalangle - 180);
Daniel@0 99 remove ('penpos');
Daniel@0 100 };
Daniel@0 101 #
Daniel@0 102 # editing functions
Daniel@0 103 #
Daniel@0 104 # transform the fractal.
Daniel@0 105 #
Daniel@0 106 # map point 'prevpoint' to point 'currpoint'
Daniel@0 107 # with respect to the center of the fractal.
Daniel@0 108 #
Daniel@0 109 transformfractal = function (prevpoint, currpoint) {
Daniel@0 110 local prevtan, currtan, prevradius, currradius;
Daniel@0 111
Daniel@0 112 prevtan = atan (prevpoint.y - center.y, prevpoint.x - center.x);
Daniel@0 113 currtan = atan (currpoint.y - center.y, currpoint.x - center.x);
Daniel@0 114 fractalangle = fractalangle + (currtan - prevtan);
Daniel@0 115 prevradius = sqrt (
Daniel@0 116 sq (prevpoint.y - center.y) + sq (prevpoint.x - center.x)
Daniel@0 117 );
Daniel@0 118 currradius = sqrt (
Daniel@0 119 sq (currpoint.y - center.y) + sq (currpoint.x - center.x)
Daniel@0 120 );
Daniel@0 121 radius = radius / prevradius * currradius;
Daniel@0 122 length = radius / 2 * sqrt (12);
Daniel@0 123 };
Daniel@0 124 #
Daniel@0 125 # main actions
Daniel@0 126 #
Daniel@0 127 redraw = function (data) {
Daniel@0 128 redrawfractal ();
Daniel@0 129 };
Daniel@0 130 changemaxlevel = function (dn) {
Daniel@0 131 maxlevel = maxlevel + dn;
Daniel@0 132 if (maxlevel < 0)
Daniel@0 133 maxlevel = 0;
Daniel@0 134 redrawfractal ();
Daniel@0 135 };
Daniel@0 136 resize = function (data) {
Daniel@0 137 local ret;
Daniel@0 138 if (data.widget == array1) {
Daniel@0 139 ret = [
Daniel@0 140 array2 = [
Daniel@0 141 'x' = data.size.x;
Daniel@0 142 'y' = sizes.button.y * 2;
Daniel@0 143 ];
Daniel@0 144 scroll = [
Daniel@0 145 'x' = data.size.x;
Daniel@0 146 'y' = data.size.y - sizes.button.y * 2;
Daniel@0 147 ];
Daniel@0 148 ];
Daniel@0 149 } else if (data.widget == array2) {
Daniel@0 150 ret = [
Daniel@0 151 array3 = [
Daniel@0 152 'x' = sizes.button.x;
Daniel@0 153 'y' = 2 * sizes.button.y;
Daniel@0 154 ];
Daniel@0 155 atext = [
Daniel@0 156 'x' = data.size.x - sizes.button.x;
Daniel@0 157 'y' = 2 * sizes.button.y;
Daniel@0 158 ];
Daniel@0 159 ];
Daniel@0 160 } else if (data.widget == array3) {
Daniel@0 161 ret = [
Daniel@0 162 morebutton = sizes.button;
Daniel@0 163 lessbutton = sizes.button;
Daniel@0 164 ];
Daniel@0 165 }
Daniel@0 166 return ret;
Daniel@0 167 };
Daniel@0 168 #
Daniel@0 169 # user interface functions
Daniel@0 170 #
Daniel@0 171 # bind changes to the fractal to user actions
Daniel@0 172 #
Daniel@0 173 leftup = function (data) {
Daniel@0 174 transformfractal (data.ppos, data.pos);
Daniel@0 175 redrawfractal ();
Daniel@0 176 };
Daniel@0 177 menu = [
Daniel@0 178 0 = 'more';
Daniel@0 179 1 = 'less';
Daniel@0 180 ];
Daniel@0 181 domenu = function (i) {
Daniel@0 182 local s;
Daniel@0 183 s = menu[i];
Daniel@0 184 if (s == 'more')
Daniel@0 185 changemaxlevel (1);
Daniel@0 186 else if (s == 'less')
Daniel@0 187 changemaxlevel (-1);
Daniel@0 188 };
Daniel@0 189 rightdown = function (data) {
Daniel@0 190 domenu (displaymenu (canvas, menu));
Daniel@0 191 };
Daniel@0 192 pressed = function (data) {
Daniel@0 193 if (data.widget == morebutton)
Daniel@0 194 changemaxlevel (1);
Daniel@0 195 else if (data.widget == lessbutton)
Daniel@0 196 changemaxlevel (-1);
Daniel@0 197 };
Daniel@0 198 oneline = function (data) {
Daniel@0 199 local dn;
Daniel@0 200 dn = ston (data.text);
Daniel@0 201 if (dn > 0 | dn < 0)
Daniel@0 202 changemaxlevel (dn - maxlevel);
Daniel@0 203 };
Daniel@0 204 #
Daniel@0 205 # postscript generation
Daniel@0 206 #
Daniel@0 207 dops = function () {
Daniel@0 208 local r;
Daniel@0 209
Daniel@0 210 r = [0 = ['x' = 0; 'y' = 0;]; 1 = ['x' = 8 * 300; 'y' = 10.5 * 300;];];
Daniel@0 211 canvas = opencanvas ('pscanvas', '', r);
Daniel@0 212 setwidgetattr (canvas, ['window' = wrect;]);
Daniel@0 213 redraw ();
Daniel@0 214 closecanvas (canvas);
Daniel@0 215 canvas=defcanvas;
Daniel@0 216 };
Daniel@0 217 init ();
Daniel@0 218 #txtview ('off');