File: hilbert.html

package info (click to toggle)
jsxgraph 1.3.5+dfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 26,044 kB
  • sloc: xml: 5,869; java: 1,072; python: 747; php: 192; makefile: 146; sh: 47
file content (170 lines) | stat: -rw-r--r-- 5,528 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<html>
<head>
   <title>JSXGraph example</title>
   <link rel="stylesheet" type="text/css" href="../distrib/jsxgraph.css" />
   <script type="text/javascript" src="/javascript/prototype/prototype.js"></script>
   <script type="text/javascript" src="../src/loadjsxgraph.js"></script>
</head>
<body>
<h2>The Hilbert curve</h2>
<p>
The famous space filling curve drawn with JavaScript.
This web page uses the open source library <a href="http://jsxgraph.org">JSXGraph (http://jsxgraph.org)</a>.
If it is set to level=9 the curve is defined by 
262 144 points - so be careful if you are using the Internet Explorer.
</p>
<div style="width:800px">
<div id="jxgbox" class="jxgbox" style="width:600px; height:600px; float:left; "></div>
  <form>
  <p> Max. iteration level:
  <select id='maxlev' style='margin:1em'>
        <option value="1"> 1
        <option value="2"> 2
        <option value="3"> 3
        <option value="4"> 4
        <option value="5"> 5
        <option value="6"> 6
        <option value="7"> 7
        <option value="8"> 8
        <option value="9"> 9
  </select>
  <input type="button" value=" set " onClick="doIt()" style='margin:1em'>
</p>
  </form>
</div>
<br clear=all>
<div id="debug" style="display:block;"></div>
   <script type="text/javascript">
    /* <![CDATA[ */

        var b = JXG.JSXGraph.initBoard('jxgbox', {originX: 20, originY: 20, unitX: 5, unitY: 5, zoomX:1, axis:true});
        var UP = 0;
        var LEFT = 1;
        var DOWN = 2;
        var RIGHT = 3;
        var pos = [0,0];
        var step = 1;
        var pointsX = [0];
        var pointsY = [0];

        move = function(dir)  {
                switch (dir) {
                    case LEFT:
                        pos[0] -= step;
                        break;
                    case RIGHT:
                        pos[0] += step;
                        break;
                    case UP:
                        pos[1] += step;
                        break;
                    case DOWN:
                        pos[1] -= step;
                        break;
                } 
                pointsX[pointsX.length] = pos[0];
                pointsY[pointsY.length] = pos[1];
        };

        hilbert = function(level,dir) {
            if (level<=1) {
                switch (dir) {
                    case LEFT:
                        move(RIGHT);     
                        move(DOWN);      
                        move(LEFT);
                        break;
                    case RIGHT:
                        move(LEFT);
                        move(UP);
                        move(RIGHT);
                        break;
                    case UP:
                        move(DOWN);
                        move(RIGHT);
                        move(UP);
                        break;
                    case DOWN:
                        move(UP);
                        move(LEFT);
                        move(DOWN);
                        break;
                } 
            } else {
                switch (dir) {
                    case LEFT:
                        hilbert(level-1,UP);
                        move(RIGHT);
                        hilbert(level-1,LEFT);
                        move(DOWN);
                        hilbert(level-1,LEFT);
                        move(LEFT);
                        hilbert(level-1,DOWN);
                        break;
                    case RIGHT:
                        hilbert(level-1,DOWN);
                        move(LEFT);
                        hilbert(level-1,RIGHT);
                        move(UP);
                        hilbert(level-1,RIGHT);
                        move(RIGHT);
                        hilbert(level-1,UP);
                        break;
                    case UP:
                        hilbert(level-1,LEFT);
                        move(DOWN);
                        hilbert(level-1,UP);
                        move(RIGHT);
                        hilbert(level-1,UP);
                        move(UP);
                        hilbert(level-1,RIGHT);
                        break;
                    case DOWN:
                        hilbert(level-1,RIGHT);
                        move(UP);
                        hilbert(level-1,DOWN);
                        move(LEFT);
                        hilbert(level-1,DOWN);
                        move(DOWN);
                        hilbert(level-1,LEFT);
                        break;
                } 
            }
        };

        var s = b.createElement('slider', [[0,-110],[50,-110],[1,2,7]], {name:'S',snapWidth:1});
        var curve = b.createElement('curve',[pointsX,pointsY],{curveType:'plot'});


        function doIt(){
            pos = [0,0];
            pointsX = [0];
            pointsY = [0];
            maxlev = $('maxlev').value;
            step = 50/(Math.pow(2,maxlev-1));
            hilbert(maxlev,LEFT);
            curve.dataX = pointsX;
            curve.dataY = pointsY;
            b.update();
        } 

        var oldlev = -1;
        curve.updateDataArray = function() {
            maxlev = s.Value();
            if (oldlev==maxlev) {
                return;
            }
            pos = [0,0];
            pointsX = [0];
            pointsY = [0];
            step = 50/(Math.pow(2,maxlev-1));
            hilbert(maxlev,LEFT);
            this.dataX = pointsX;
            this.dataY = pointsY;
            oldlev=maxlev;
        };
       
  /* ]]> */
  </script>
</body>
</html>