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