Hilbert curve: Difference between revisions
From JSXGraph Wiki
A WASSERMANN (talk | contribs) No edit summary |
A WASSERMANN (talk | contribs) No edit summary |
||
Line 3: | Line 3: | ||
262 144 points - so be careful if you are using the Internet Explorer. | 262 144 points - so be careful if you are using the Internet Explorer. | ||
<html> | <html> | ||
<!-- | |||
<form> | <form> | ||
Set the iteration level: | Set the iteration level: | ||
Line 18: | Line 19: | ||
<input type="button" value=" set " onClick="doIt()" style='margin:1em'> | <input type="button" value=" set " onClick="doIt()" style='margin:1em'> | ||
</form> | </form> | ||
--> | |||
</html> | </html> | ||
Line 122: | Line 124: | ||
var curve = b.createElement('curve',[pointsX,pointsY],{curveType:'plot'}); | var curve = b.createElement('curve',[pointsX,pointsY],{curveType:'plot'}); | ||
var s = b.createElement('slider', [[0.75,-110],[50,-110],[ | var s = b.createElement('slider', [[0.75,-110],[50,-110],[1,1,9]], {name:'S',snapWidth:1}); | ||
/* | |||
function doIt(){ | function doIt(){ | ||
pos = [0,0]; | pos = [0,0]; | ||
Line 135: | Line 137: | ||
b.update(); | b.update(); | ||
} | } | ||
*/ | |||
var oldlev = -1; | var oldlev = -1; | ||
curve.updateDataArray = function() { | curve.updateDataArray = function() { | ||
Line 255: | Line 257: | ||
var curve = b.createElement('curve',[pointsX,pointsY],{curveType:'plot'}); | var curve = b.createElement('curve',[pointsX,pointsY],{curveType:'plot'}); | ||
var s = b.createElement('slider', [[0.75,-110],[50,-110],[ | var s = b.createElement('slider', [[0.75,-110],[50,-110],[1,1,9]], {name:'S',snapWidth:1}); | ||
var oldlev = -1; | var oldlev = -1; | ||
curve.updateDataArray = function() { | curve.updateDataArray = function() { |
Revision as of 09:33, 21 August 2009
The famous space filling curve drawn with JavaScript. 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.
References
The underlying JavaScript code
var b = JXG.JSXGraph.initBoard('jxgbox', {originX: 20, originY: 20, unitX: 5, unitY: 5});
var UP = 0;
var LEFT = 1;
var DOWN = 2;
var RIGHT = 3;
var pos = [0,0];
var step = 1;
var pointsX = [];
var pointsY = [];
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 curve = b.createElement('curve',[pointsX,pointsY],{curveType:'plot'});
var s = b.createElement('slider', [[0.75,-110],[50,-110],[1,1,9]], {name:'S',snapWidth:1});
var oldlev = -1;
curve.updateDataArray = function() {
maxlev = Math.round(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;
};