-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdesmos.js
More file actions
79 lines (66 loc) · 2.59 KB
/
desmos.js
File metadata and controls
79 lines (66 loc) · 2.59 KB
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
let calculator;
let rSquaredElem = document.getElementById("r_squared");
window.onload = async () => {
if (typeof Desmos == 'undefined') {
return;
}
var elt = document.getElementById('calculator');
calculator = Desmos.Calculator3D(elt);
calculator.updateSettings({"expressions": false});
const timestamp = new Date().getTime(); // can remove after development
const response_state = await fetch(`./state.json?cache_bust=${timestamp}`);
var state = await response_state.json();
calculator.setState(state);
var rSquared = calculator.HelperExpression({latex: 'R_{squared}'});
rSquared.observe('numericValue', function() {
rSquaredElem.innerText = "" + rSquared.numericValue;
});
}
function readVariable(variable, expressions) {
for (const i in expressions) {
if (expressions[i].id == variable) {
return expressions[i].latex.split("=")[1];
}
}
}
export function setVariable(variable, value) {
calculator.setExpression({ id: variable, latex: variable + "=" + value });
}
export function readData() {
var expressions = calculator.getExpressions();
var data = [];
let t = JSON.parse(readVariable('t_{0}', expressions).split("\\left")[1].split("\\right")[0] + "]");
let x = JSON.parse(readVariable('x_{0}', expressions).split("\\left")[1].split("\\right")[0] + "]");
let y = JSON.parse(readVariable('y_{0}', expressions).split("\\left")[1].split("\\right")[0] + "]");
let weights = JSON.parse(readVariable('w_{eights}', expressions).split("\\left")[1].split("\\right")[0] + "]");
let methods = JSON.parse(readVariable('m_{ethods}', expressions).split("\\left")[1].split("\\right")[0] + "]");
for (const i in t) {
data.push({
't': t[i],
'x': x[i],
'y': y[i],
'weight': weights[i],
'method': methods[i]
})
}
return data;
}
export function setData(data) {
let t = [];
let x = [];
let y = [];
let weights = [];
let methods = [];
for (const point of data) {
t.push(point['t']);
x.push(point['x']);
y.push(point['y']);
weights.push(point['weight']);
methods.push(point['method']);
}
setVariable("t_{0}", "\\left[" + t.join(", ") + "\\right]");
setVariable("x_{0}", "\\left[" + x.join(", ") + "\\right]");
setVariable("y_{0}", "\\left[" + y.join(", ") + "\\right]");
setVariable("w_{eights}", "\\left[" + weights.join(", ") + "\\right]");
setVariable("m_{ethods}", "\\left[" + methods.join(", ") + "\\right]");
}