-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_function_drawer.html
More file actions
497 lines (480 loc) · 20.2 KB
/
math_function_drawer.html
File metadata and controls
497 lines (480 loc) · 20.2 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="[MAZ01001.github.io] Polynomial visualizer with P5.js">
<meta name="author" content="MAZ01001">
<link rel="apple-touch-icon" sizes="180x180" href="../img/apple-touch-icon.png">
<link rel="icon" type="image/x-icon" href="../img/MAZ_logo.svg">
<link rel="icon" type="image/png" sizes="32x32" href="../img/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="../img/favicon-16x16.png">
<link rel="manifest" href="../img/site.webmanifest">
<link rel="mask-icon" href="../img/safari-pinned-tab.svg" color="#ff9900">
<link rel="shortcut icon" href="../img/favicon.ico">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="msapplication-config" content="../img/browserconfig.xml">
<meta name="theme-color" content="#ffffff">
<title>Math Function Drawer</title>
<style>
body{
background-color: #2c2c2c;
padding: 0;
margin: 0;
overflow: hidden;
}
</style>
<script src="../libraries/p5.js/v1.4.2/p5.min.js"></script>
<script>
/// <reference path="../libraries/p5.js/intellisense/p5.d.ts"/>
/// <reference path="../libraries/p5.js/intellisense/p5.global-mode.d.ts"/>
let sliderArr = [],
hideSlider = true,
promVal = [],
valsCmd = [],
drawCmd = false,
scrollingVal = 33,
moveX = 0,
moveY = 0;
function setup() {
cnv = createCanvas(windowWidth, windowHeight);
cnv.mouseWheel(e => scrolling(cnv, true));
own = createCheckbox('own function?', false);
own.changed(e => {
if (own.checked()) {
hideSlider = true;
if (hideSlider) {
hideSliderB.html('show sliders');
} else {
hideSliderB.html('hide sliders');
}
promVal.length = 0;
for (let i = 0; i < sliderArr.length; i++) {
let tmp = NaN;
tmp = prompt('\<a' + i + '> * X^' + i, 0);
if(tmp==null)break;
promVal.push(parseFloat(tmp==""?0:tmp));
}
} else { hideSlider = false; }
});
vSlider = createSlider(1, 100, 10, 0.1).mouseWheel(e => scrolling(vSlider));
hSlider = createSlider(1, 100, 10, 0.1).mouseWheel(e => scrolling(hSlider));
vSel = createSelect();
vSel.option('1-100');
vSel.option('100-10k');
vSel.option('10k-1M');
vSel.option('1M-100M');
vSel.changed(e => {
switch (vSel.value()) {
case '1-100':
vSlider.elt.min = 1;
vSlider.elt.max = 100;
vSlider.elt.step = 0.1;
break;
case '100-10k':
vSlider.elt.min = 100;
vSlider.elt.max = 10000;
vSlider.elt.step = 1;
break;
case '10k-1M':
vSlider.elt.min = 10000;
vSlider.elt.max = 1000000;
vSlider.elt.step = 1;
break;
case '1M-100M':
vSlider.elt.min = 1000000;
vSlider.elt.max = 100000000;
vSlider.elt.step = 1;
break;
default:
break;
}
});
hSel = createSelect();
hSel.option('1-100');
hSel.option('100-10k');
hSel.option('10k-1M');
hSel.option('1M-100M');
hSel.changed(e => {
switch (hSel.value()) {
case '1-100':
hSlider.elt.min = 1;
hSlider.elt.max = 100;
hSlider.elt.step = 0.1;
break;
case '100-10k':
hSlider.elt.min = 100;
hSlider.elt.max = 10000;
hSlider.elt.step = 1;
break;
case '10k-1M':
hSlider.elt.min = 10000;
hSlider.elt.max = 1000000;
hSlider.elt.step = 1;
break;
case '1M-100M':
hSlider.elt.min = 1000000;
hSlider.elt.max = 100000000;
hSlider.elt.step = 1;
break;
default:
break;
}
});
hideSliderB = createButton('show sliders');
hideSliderB.mousePressed(e => {
hideSlider = !hideSlider;
if (hideSlider) {
hideSliderB.html('show sliders');
} else {
hideSliderB.html('hide sliders');
}
});
sliderArr.range = 10;
sliderArr.default = [0, 0, 1, 0, 0, 0]; //<!--HK__[a0 → a5]
for (let i = 0; i < 6; i++) {
sliderArr.push(createSlider(-sliderArr.range, sliderArr.range, sliderArr.default[i], 0.001));
sliderArr[i].mouseWheel(e => scrolling(sliderArr[i]));
}
// frameRate(30);
}
//<!--HW__setup-end________________________________________________________
function draw() {
//<!--HK__box to hilight canvas
strokeWeight(4);
stroke('blue');
fill('#202020');
rectMode(CORNER);
rect(0, 0, width, height);
//<!--HK__reset fill, stroke width,stroke color
fill('0');
strokeWeight(2);
stroke('white');
//TODO OOP!
//TODO OOP!
//<!--HK__draw graph
const numH = hSlider.value(),
numV = vSlider.value();
drawingfield(numH, numV);
noFill();
strokeWeight(4);
stroke('green');
beginShape();
for (let i = 0; i <= width; i++) {
let formPunkt = formel(map(i, 0, width, -numH, numH));
curveVertex(i, map(formPunkt, -numV, numV, height, 0));
}
endShape();
if (own.checked()) {
//<!--HK__draw graph2
stroke('cyan');
beginShape();
for (let i = 0; i <= width; i++) {
let formPunkt = ownF(map(i, 0, width, -numH, numH));
curveVertex(i, map(formPunkt, -numV, numV, height, 0));
}
endShape();
}
if (drawCmd) { //TODO Object-array of (max?) graphCmd's with random color~~
//<!--HK__draw graph3
stroke('magenta');
beginShape();
for (let i = 0; i <= width; i++) {
let formPunkt = 0;
for (let i2 = 9; i2 >= 0; i2--) {
formPunkt += (valsCmd[i2] * Math.pow(map(i, 0, width, -numH, numH), i2));
}
curveVertex(i, map(formPunkt, -numV, numV, height, 0));
}
endShape();
}
//<!--HK__graph names
strokeWeight(1);
textSize(20);
textAlign(LEFT, TOP);
fill('green');
stroke('green');
text(`f(x)`, 20, 30);
if (own.checked()) {
fill('cyan');
stroke('cyan');
text(`own(x)`, 20, 50);
}
if (drawCmd) {
fill('magenta');
stroke('magenta');
text(`cmd(x)`, 20, 70);
}
textSize(12);
//<!--HK__reset sliders/selects
own.position(10, 10);
hideSliderB.position((width - hideSliderB.width) - 20, (((height / 2) - 200) - hideSliderB.height) - 20);
//<!--HK__v
vSlider.size(200);
vSlider.position((width / 2) - (vSlider.width / 2), 0);
vSlider.doubleClicked(function() {
vSel.value('1-100');
vSlider.elt.min = 1;
vSlider.elt.max = 1000;
vSlider.value(100);
});
vSel.position((vSlider.position().x + vSlider.width) + 4, vSlider.position().y);
fill('yellow');
noStroke();
textAlign(RIGHT, CENTER);
text('v' + '=' + vSlider.value(), vSlider.position().x - 4, (vSlider.position().y + (vSlider.height / 2)));
strokeWeight(1);
//<!--HK__h
hSlider.size(200);
hSlider.position(10, (height / 2) - (hSlider.height / 2));
hSlider.doubleClicked(function() {
hSel.value('1-1K');
hSlider.elt.min = 1;
hSlider.elt.max = 1000;
hSlider.value(100);
});
hSel.position(hSlider.position().x + 4, hSlider.position().y - hSlider.height);
fill('yellow');
noStroke();
textAlign(LEFT, CENTER);
text('h' + '=' + hSlider.value(), (hSlider.position().x + hSlider.width) + 4, (hSlider.position().y + (hSlider.height / 2)));
strokeWeight(1);
//<!--HK__anArr
if (!hideSlider) { sliderAArrDraw(); } else {
sliderArr.forEach(e => { e.hide(); });
}
//<!--HK__mouse cross with select box + coordinates
if (mouseIsPressed) {
let xx = roundTo(map(mouseX, 0, width, -hSlider.value(), hSlider.value()), 4),
yy = roundTo(map(mouseY, 0, height, -vSlider.value(), vSlider.value()), 4),
pxx = roundTo(map(moveX, 0, width, -hSlider.value(), hSlider.value()), 4),
pyy = roundTo(map(moveY, 0, height, -vSlider.value(), vSlider.value()), 4);
strokeWeight(1);
stroke('black');
noCursor();
noFill();
line(mouseX, 0, mouseX, height);
line(0, mouseY, width, mouseY);
line(moveX, 0, moveX, height);
line(0, moveY, width, moveY);
cursor(CROSS);
fill(255, 255, 0, 20);
stroke(255, 255, 0);
quad(mouseX, mouseY, moveX, mouseY, moveX, moveY, mouseX, moveY);
textAlign(LEFT, BOTTOM);//TODO auto
noStroke();
fill('yellow');
text('px:' + pxx + '\npy:' + pyy + '\nx:' + xx + '\ny:' + yy, mouseX, mouseY);
} else {
let xx = roundTo(map(mouseX, 0, width, -hSlider.value(), hSlider.value()), 4),
yy = roundTo(map(mouseY, 0, height, -vSlider.value(), vSlider.value()), 4);
strokeWeight(1);
stroke('black');
noCursor();
noFill();
line(mouseX, 0, mouseX, height);
line(0, mouseY, width, mouseY);
cursor(CROSS);
textAlign(LEFT, BOTTOM);//TODO auto
noStroke();
fill('yellow');
text('x:' + xx + '\ny:' + yy, mouseX, mouseY);
}
//<!--HW__draw-end_____________________________________________________
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight - 4);
return false;
}
function mouseMoved() {
moveX = mouseX;
moveY = mouseY;
return false;
}
function sliderAArrDraw() {
for (let i = 0; i < sliderArr.length; i++) {
sliderArr[i].show();
sliderArr[i].size(width / 1.5);
sliderArr[i].position((width - sliderArr[i].width) - 20, ((height / 2) + ((i + i) * sliderArr[i].height)) - (9.5 * sliderArr[i].height));
sliderArr[i].doubleClicked(function() { sliderArr[i].value(0); });
fill('yellow');
noStroke();
textAlign(LEFT, CENTER);
text('a' + i + '=' + sliderArr[i].value(), sliderArr[i].position().x, ((height / 2) + ((i + i) * sliderArr[i].height)) - (9.5 * sliderArr[i].height));
strokeWeight(1);
}
return false;
}
function drawingfield(zahlH = 10, zahlV = 1000) {
stroke('red');
noFill();
strokeWeight(3);
//<!--HK__coordinates
line(0, height / 2, width, height / 2);
line(width / 2, 0, width / 2, height);
//<!--HK__lines %
let numbr = -zahlH,
schrittH = zahlH / 10,
schrittV = zahlV / 10;
for (let i = 0; i <= width; i += (width / 20)) {
stroke('red');
noFill();
strokeWeight(3);
line(i, (height / 2) - 10, i, (height / 2) + 10);
if (numbr !== 0) {
fill('red');
strokeWeight(1);
textAlign(CENTER, CENTER);
text(roundTo(numbr), i, (height / 2) + 15);
numbr += schrittH;
} else { numbr += schrittH; }
}
numbr = zahlV;
for (let i = 0; i <= height; i += (height / 20)) {
stroke('red');
noFill();
strokeWeight(3);
line((width / 2) - 10, i, (width / 2) + 10, i);
if (numbr !== 0) {
fill('red');
strokeWeight(1);
textAlign(LEFT, CENTER);
text(roundTo(numbr), (width / 2) + 15, i);
numbr -= schrittV;
} else { numbr -= schrittV; }
}
stroke(0);
strokeWeight(1);
return false;
}
//TODO OOP!
//TODO OOP!
function formel(x = NaN) {
//<!--HK__f(x) = (a9*(x^9)) + ... + (a3*(x^3)) + (a2*(x^2)) + (a1*(x^1)) + (a0*(x^0))
//<!--HK__[0=a0, ... ,9=a9] || [9=a9, ... ,0=a0].reverse()
let tmp = 0;
for (let i = sliderArr.length-1; i >= 0; i--) {
tmp += (sliderArr[i].value() * Math.pow(x, i));
}
return tmp;
}
function ownF(x = NaN) {
//<!--HK__f(x) = (a9*(x^9)) + ... + (a3*(x^3)) + (a2*(x^2)) + (a1*(x^1)) + (a0*(x^0))
//<!--HK__[0=a0, ... ,9=a9] || [9=a9, ... ,0=a0].reverse()
let tmp2 = 0;
for (let i = 0; i < promVal.length; i++) {
tmp2 += (promVal[i] * Math.pow(x, i));
}
return tmp2;
}
function graphCmd(vals = ['a9', 'a8', 'a7', 'a6', 'a5', 'a4', 'a3', 'a2', 'a1', 'a0'], drawGraph = true) {
//<!--HK__f(x) = (a9*(x^9)) + ... + (a3*(x^3)) + (a2*(x^2)) + (a1*(x^1)) + (a0*(x^0))
//<!--HK__[0=a0, ... ,9=a9] || [9=a9, ... ,0=a0].reverse()
let testNan = false;
for(value of vals){
if (isNaN(parseFloat(value))) {
testNan = true;
break;
}
}
if (drawGraph && !testNan) {
valsCmd.length = 0;
vals.reverse();
vals.forEach(function(value) {
valsCmd.push(value);
});
drawCmd = true;
} else {
valsCmd.length = 0;
drawCmd = false;
}
return 'done';
}
function roundTo(n, digi = 3) {
let neg = false;
if (n < 0) {
neg = true;
n = n * -1;
}
let multi = Math.pow(10, digi);
n = parseFloat((n * multi).toFixed(15));
n = (Math.fround(n) / multi).toFixed(digi);
if (neg) {
n = (n * -1).toFixed(digi);
}
return n;
}
function scrolling(ee, canvas = false) {
// element.mouseWheel(e=>scrolling(e));
if (canvas) {
//<!--HK__h&v
vSlider.value(vSlider.value() + parseFloat(parseFloat(vSlider.elt.step) * (-parseFloat(scrollingVal) / 33.33333206176758)));
hSlider.value(hSlider.value() + parseFloat(parseFloat(hSlider.elt.step) * (-parseFloat(scrollingVal) / 33.33333206176758)));
}
ee.value(ee.value() + parseFloat(parseFloat(ee.elt.step) * (parseFloat(scrollingVal) / 33.33333206176758)));
// print(_mouseWheelDeltaY / 33.33333206176758);
return false;
}
function mouseWheel(event) {
scrollingVal = -event.delta;
return false;
}
function mouseReleased() {
if ((mouseButton == CENTER)) {
print(logValues());
copyToClipboard(logValues());
alert(logValues());
}
return false;
}
function logValues() {
let tmp3 = '';
tmp3 += 'vScale: ' + vSlider.value() + '\nhScale: ' + hSlider.value();
tmp3 += '\nf(x) = ';
sliderArr.forEach(function(value, index) {
if(value.value()>=0) tmp3 += (index==0?'':'+') + value.value() + '*X^' + index;
else tmp3 += value.value() + '*X^' + index;
});
if (own.checked()) {
tmp3 += '\nown(x) = ';
if(promVal.length>1) promVal.forEach(function(value, index) {
if(value>=0) tmp3 += (index==0?'':'+')+value + '*X^' + index;
else tmp3 += value + '*X^' + index;
});
else if(promVal.length==1) {
if(promVal[0]>=0) tmp3 += promVal[0] + '*X^0+0*X^1';
else tmp3 += promVal[0] + '*X^0+0*X^1';
} else tmp3 += '0*X^0+0*X^1';
}
if (drawCmd) {
tmp3 += '\ncmd(x) = ';
valsCmd.forEach(function(value, index) {
if(value>=0) tmp3 += (index==0?'':'+')+value + '*X^' + index;
else tmp3 += value + '*X^' + index;
});
}
return tmp3;
}
//TODO custom context menu [copy values]<(logValues.tostring{split by'\n'}.to clipboard!!)
//TODO [paste values]<(textbox popup with {ok} button{same format required})
function copyToClipboard(str = '') {
const el = document.createElement('textarea'); //<!--HK__Create a <textarea> element
el.value = str; //<!--HK__Set its value to the string that you want copied
el.setAttribute('readonly', ''); //<!--HK__Make it readonly to be tamper-proof
el.style.position = 'absolute';
el.style.left = '-9999px'; //<!--HK__Move outside the screen to make it invisible
document.body.appendChild(el); //<!--HK__Append the <textarea> element to the HTML document
const selected = document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false; //<!--HK__Mark as false to know no selection existed before
el.select(); //<!--HK__Select the <textarea> content
document.execCommand('copy'); //<!--HK__Copy - only works as a result of a user action (e.g. click events)
document.body.removeChild(el); //<!--HK__Remove the <textarea> element
if (selected) { //<!--HK__If a selection existed before copying
document.getSelection().removeAllRanges(); //<!--HK__Unselect everything on the HTML document
document.getSelection().addRange(selected); //<!--HK__Restore the original selection
}
return false;
}
</script>
</head>
<body></body>
</html>