-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
138 lines (114 loc) · 3.72 KB
/
index.js
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
import { DrawContext } from "./graph.js";
import { m3Identity, m3Inverse, vColumnLeftMultiply } from "./matrix.js";
{
function updatePoints(points) {
document.querySelector("#points").innerHTML = points
.map(
(vec, i) =>
`\\(\\vec{v_{${i}}}=\\begin{bmatrix}${vec[0] * 10} \\\\ ${
vec[1] * 10
} \\\\${vec[2] * 10} \\end{bmatrix}\\)`
)
.join(" ");
MathJax.typeset();
}
const canvas = document.getElementById("canvas1");
const draw = new DrawContext(canvas);
let points = [];
draw.addLine([0.1, 0.2, 0.3]);
draw.addPoint([0.1, 0.2, 0.3]);
draw.startDraw();
document.querySelector(".resetCameraButton").onclick = () =>
draw.resetCamera();
// document.querySelector(".resetTransformButton").onclick = () => draw.resetTransform();
document.querySelector(".resetObjectsButton").onclick = () => {
draw.resetObjects();
points = [];
updatePoints(points);
};
document.querySelector("#addPoint").addEventListener("submit", (evt) => {
evt.preventDefault();
const data = new FormData(evt.target);
const vec = [...data.entries()].map((val) => +val[1] / 10);
points.push(vec);
draw.addPoint(vec);
draw.addLine(vec);
updatePoints(points);
});
}
{
const canvas = document.querySelector("#linearTransformations");
const draw = new DrawContext(canvas);
document.querySelector(".resetObjectsButton").onclick = () => {
draw.resetObjects();
points = [];
updatePoints(points);
};
draw.startDraw();
const m = [1, 0, 0, 1, 1, 0.5, 0, 0.5, 0.5];
let A = [1, 0, 0, 0, 1, 0, 0, 0, 1];
let vec = [0, 0, 0];
document.querySelector("#basis").onsubmit = (evt) => {
evt.preventDefault();
const vals = [...new FormData(evt.target).entries()].map((val) => +val[1]);
const e1 = vals.slice(0, 3);
const e2 = vals.slice(3, 6);
const e3 = vals.slice(6);
A = vals.map((val, i) => vals[(i % 3) * 3 + Math.floor(i / 3)]);
draw.interpolateTransform(A, 2);
};
document.querySelector("#addPoint1").onsubmit = (evt) => {
evt.preventDefault();
const data = new FormData(evt.target);
draw.resetObjects();
vec = [...data.entries()].map((val) => +val[1]);
const res = vColumnLeftMultiply(A, vec);
draw.addLine(
res,
[0, 0, 0],
[Math.random(), Math.random(), Math.random()],
1,
false
);
};
document.querySelector("#btn3").onclick = () => {
draw.interpolateTransform(m3Identity(), 2);
const div = document.querySelector("#transformed");
const pt = vColumnLeftMultiply(A, vec).map((pt) => pt.toPrecision(2));
div.innerHTML = `\\(\\vec{v}=\\begin{bmatrix}${pt[0]} \\\\ ${pt[1]} \\\\${pt[2]} \\end{bmatrix}\\)`;
MathJax.typeset();
};
}
{
const canvas = document.querySelector("#canvas3");
const draw = new DrawContext(canvas, true);
document.querySelector(".resetObjectsButton").onclick = () => {
draw.resetObjects();
points = [];
updatePoints(points);
};
draw.startDraw();
console.log(canvas);
document.querySelector("#shape").onchange = (evt) => {
if (evt.target.value === "placeholder") return;
const val = evt.target.value;
draw.resetObjects();
switch (val) {
case "torus":
draw.addTorus();
break;
case "sphere":
draw.addPoint([0, 0, 0], 10, [0.5, 0.5, 1], true, true);
break;
}
};
document.querySelector("#basis1").onsubmit = (evt) => {
evt.preventDefault();
const vals = [...new FormData(evt.target).entries()].map((val) => +val[1]);
const e1 = vals.slice(0, 3);
const e2 = vals.slice(3, 6);
const e3 = vals.slice(6);
const A = vals.map((val, i) => vals[(i % 3) * 3 + Math.floor(i / 3)]);
draw.interpolateTransform(A, 2);
};
}