126 lines
3.9 KiB
JavaScript
126 lines
3.9 KiB
JavaScript
var Outs, Inps, Btnt, Canvas;
|
|
// async/await z příkladu na webu
|
|
window.onload = async function() {
|
|
Outs = document.getElementById('stdout');
|
|
Inps = document.getElementById('input');
|
|
Btnt = document.getElementById('buttonTest');
|
|
Canvas = document.getElementById('canvas');
|
|
Outs.value = '';
|
|
await getFile ('osc.txt');
|
|
Btnt.onclick = () => {
|
|
Outs.value = '';
|
|
run ();
|
|
};
|
|
};
|
|
function print (str) {
|
|
Outs.value += str;
|
|
}
|
|
async function getFile (name) {
|
|
console.log(name);
|
|
const response = await fetch (name);
|
|
if (!response.ok) return;
|
|
const bytes = await response.arrayBuffer();
|
|
const array = new Uint8Array(bytes);
|
|
const decoder = new TextDecoder();
|
|
Inps.value = decoder.decode(bytes);
|
|
return false;
|
|
}
|
|
///////////////////////////////////////////////
|
|
function run () {
|
|
|
|
let code = Inps.value;
|
|
try {
|
|
eval (code);
|
|
} catch (err) {
|
|
print('Error: ' + err.message + '\n');
|
|
}
|
|
}
|
|
function solve (hh, steps, data, pf) {
|
|
let p = data;
|
|
|
|
const N = pf.length;
|
|
const hal = 0.5 * hh;
|
|
const hhs = hh / 6.0;
|
|
let min = {t:+1.0e100, w:[]};
|
|
for (let k=0; k<N; k++) { min.w[k] = min.t; }
|
|
let max = {t:-1.0e100, w:[]};
|
|
for (let k=0; k<N; k++) { max.w[k] = max.t; }
|
|
|
|
let result = new Array(steps + 1);
|
|
if (p.t < min.t) min.t = p.t;
|
|
for (let k=0; k<N; k++) { if (p.w[k] < min.w[k]) min.w[k] = p.w[k]; }
|
|
if (p.t > max.t) max.t = p.t;
|
|
for (let k=0; k<N; k++) { if (p.w[k] > max.w[k]) max.w[k] = p.w[k]; }
|
|
result [0] = new Object (structuredClone(p));
|
|
for (let index=1; index<result.length; index++) {
|
|
let k1 = [];
|
|
for (let k=0; k<N; k++) { k1[k] = pf[k] (p); }
|
|
let p2 = structuredClone(p); p2.t += hal;
|
|
for (let k=0; k<N; k++) { p2.w[k] += hal * k1[k]; }
|
|
let k2 = [];
|
|
for (let k=0; k<N; k++) { k2[k] = pf[k](p2); }
|
|
let p3 = structuredClone(p); p3.t += hal;
|
|
for (let k=0; k<N; k++) { p3.w[k] += hal * k2[k]; }
|
|
let k3 = [];
|
|
for (let k=0; k<N; k++) { k3[k] = pf[k](p3); }
|
|
let p4 = structuredClone(p); p4.t += hh;
|
|
for (let k=0; k<N; k++) { p4.w[k] += hh * k3[k]; }
|
|
let k4 = [];
|
|
for (let k=0; k<N; k++) { k4[k] = pf[k](p4); }
|
|
p.t += hh;
|
|
for (let k=0; k<N; k++) { p.w[k] += hhs * (k1[k] + 2.0 * k2[k] + 2.0 * k3[k] + k4[k]); }
|
|
|
|
let np = structuredClone (p);
|
|
if (np.t < min.t) min.t = np.t;
|
|
for (let k=0; k<N; k++) { if (np.w[k] < min.w[k]) min.w[k] = np.w[k]; }
|
|
if (np.t > max.t) max.t = np.t;
|
|
for (let k=0; k<N; k++) { if (np.w[k] > max.w[k]) max.w[k] = np.w[k]; }
|
|
result [index] = new Object (np);
|
|
}
|
|
initCanvas ()
|
|
return {data:result, m:min, p:max};
|
|
}
|
|
function getValue (pt, si) {
|
|
if (si.length > 1) { // w
|
|
return pt[si[0]][si[1]];
|
|
} else { // t
|
|
return pt[si[0]];
|
|
}
|
|
}
|
|
function initCanvas () {
|
|
const width = canvas.clientWidth;
|
|
const height = canvas.clientHeight;
|
|
canvas.width = width;
|
|
canvas.height = height;
|
|
const ctx = canvas.getContext("2d");
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
}
|
|
function plot (r, po, color) {
|
|
print('plot:' + JSON.stringify(po) + '\n');
|
|
const p0 = r.data[0];
|
|
print('min: ' + JSON.stringify(r.m) + '\n');
|
|
print('max: ' + JSON.stringify(r.p) + '\n');
|
|
const xmin = getValue (r.m, po.x);
|
|
const xmax = getValue (r.p, po.x);
|
|
const ymin = getValue (r.m, po.y);
|
|
const ymax = getValue (r.p, po.y);
|
|
const xzom = canvas.width / (xmax - xmin);
|
|
const yzom = canvas.height / (ymin - ymax);
|
|
const xofs = xmin * canvas.width / (xmin - xmax);
|
|
const yofs = ymax * canvas.height / (ymax - ymin);
|
|
//console.log (xmin, xmax, ymin, ymax);
|
|
const ctx = canvas.getContext("2d");
|
|
ctx.beginPath();
|
|
const x0 = xzom * getValue(p0, po.x) + xofs;
|
|
const y0 = yzom * getValue(p0, po.y) + yofs;
|
|
ctx.moveTo(x0, y0);
|
|
for (let n=1; n<r.data.length; n++) {
|
|
const p = r.data[n];
|
|
const x = xzom * getValue(p, po.x) + xofs;
|
|
const y = yzom * getValue(p, po.y) + yofs;
|
|
ctx.lineTo(x, y);
|
|
}
|
|
ctx.lineWidth = 3;
|
|
ctx.strokeStyle = color;
|
|
ctx.stroke();
|
|
}
|