summaryrefslogtreecommitdiff
path: root/4d.html
blob: c66b89aeb4ca4921faab03e54198831a5b6b9eec (plain)
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
171
172
173
174
175
<!DOCTYPE html>
<html>
<head>
<title>bald four dimensional thing</title>
<style>
canvas { border: 1px solid black; border-radius:5px }
body {background: url(https://upload.wikimedia.org/wikipedia/en/2/27/Bliss_%28Windows_XP%29.png) no-repeat center center fixed;background-size:cover;}
body {color:black; font-family:sans-serif; text-shadow: 10px 10px red;}
</style>

</head>
<body>
<h1>maths</h1>
<canvas width=500 height=500 id=canvas></canvas><br>
<input type="range" id="zx" min="-5" max="5" step="0.05" value="1.15"><span id=zxd>1.15</span><br>
<input type="range" id="zy" min="-5" max="5" step="0.05" value="-1.05"><span id=zyd>-1.05</span><br>
<input type="range" id="wx" min="-5" max="5" step="0.05" value="0.3"><span id=wxd>0.3</span><br>
<input type="range" id="wy" min="-5" max="5" step="0.05" value="0.95"><span id=wyd>0.95</span><br>
<input type="checkbox" checked id="spin"><label for=spin>spin</label><br>
<input type="checkbox" id="wiggle"><label for=wiggle>wiggle</label>
<script>
var zx = 1.15;
var zy = -1.05;
var wx = 0.3;
var wy = 0.95;

document.getElementById("zx").addEventListener("input", function(e) {
	var val = this.value;
	zx = val;
	document.getElementById("zxd").innerHTML = val.toString();
	go();
});
document.getElementById("zy").addEventListener("input", function(e) {
	var val = this.value;
	zy = val;
	document.getElementById("zyd").innerHTML = val.toString();
	go();
});
document.getElementById("wx").addEventListener("input", function(e) {
	var val = this.value;
	wx = val;
	document.getElementById("wxd").innerHTML = val.toString();
	go();
});
document.getElementById("wy").addEventListener("input", function(e) {
	var val = this.value;
	wy = val;
	document.getElementById("wyd").innerHTML = val.toString();
	go();
});

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

function rotVert1(v,th) {
	var c = Math.cos(th);
	var s = Math.sin(th);
	
	return [c*v[0] + s*v[2], v[1], -s*v[0] + c*v[2], v[3]];
}
function rotVert2(v,th) {
	th /=1.11245;
	var c = Math.cos(th);
	var s = Math.sin(th);
	return [c*v[0] + s*v[3], v[1], v[2], -s*v[0] + c*v[3],];
}
function rotVert(v,th) {
	return rotVert1(rotVert2(v, th), th);
}
function projVert(v) {
	return [v[0] + zx*v[2] + wx*v[3], v[1] + zy*v[2] + wy*v[3]];
}

function niceVert(v) {
	return [60*v[0]+250, 60*v[1]+250]
}

var vertices = [ 
	[-1,-1,-1, 1],
	[-1,-1, 1, 1],
	[-1, 1,-1, 1],
	[-1, 1, 1, 1],
	[ 1,-1,-1, 1],
	[ 1,-1, 1, 1],
	[ 1, 1,-1, 1],
	[ 1, 1, 1, 1],
	[-1,-1,-1,-1],
	[-1,-1, 1,-1],
	[-1, 1,-1,-1],
	[-1, 1, 1,-1],
	[ 1,-1,-1,-1],
	[ 1,-1, 1,-1],
	[ 1, 1,-1,-1],
	[ 1, 1, 1,-1]
];

var edges = [];
for (var i = 0; i < 16; i++) {
	for (var j = 0; j < 16; j++) {
		var v1 = vertices[i];
		var v2 = vertices[j];
		var d = 0;
		for (var p = 0; p < 4; p++) {
			if (v1[p]  != v2[p]) {
				d += 1;
			}
		}
		if (d==1) {
			edges.push([i,j]);
		}
	}

}
ctx.fillStyle = "red";
ctx.strokeStyle = "white";

var angle = 0;
var frame = 0;

var gradient = ctx.createLinearGradient(0,0,500,0);
gradient.addColorStop("0","hotpink");
gradient.addColorStop("0.5","orange");
gradient.addColorStop("1.0","lightsteelblue");

function go() {
	ctx.fillStyle="purple";
	ctx.fillRect(0,0,500,500);
	ctx.fillStyle=gradient;
	for (var i = 0; i < 16; i++) {
		var v = vertices[i];
		var p = niceVert(projVert(rotVert(v,angle)));
		ctx.fillRect(p[0]-10,p[1]-10,20,20);
	}
	
	for (var x = 0; x < edges.length; x++) {
		var e = edges[x];
		var v1 = vertices[e[0]];
		var v2 = vertices[e[1]];
		var p1 = niceVert(projVert(rotVert(v1,angle)));
		var p2 = niceVert(projVert(rotVert(v2,angle)));
		ctx.beginPath();
		ctx.moveTo(p1[0],p1[1]);
		ctx.lineTo(p2[0],p2[1]);
		ctx.stroke();
	}
	
}

function wiggle() {
    for (var x = 0; x < 16; x++) {
        for (var v = 0; v < 4; v++) {
            if (Math.random() > 0.6) {
                vertices[x][v] += (Math.random() - 0.5)/10;
            }
        }
    }
}

function step() {
	go();
	if (document.getElementById("spin").checked) {
		angle += 0.05;
	}
	if (document.getElementById("wiggle").checked) {
		wiggle();
	}
	frame += 1;
	requestAnimationFrame(step);
}
step();


</script>
</body>
</html>