comparison toolboxes/graph_visualisation/share/graphviz/lefty/fractal2.lefty @ 0:e9a9cd732c1e tip

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