-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
1285 lines (999 loc) · 42.4 KB
/
script.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
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file Main script of the complex graphing calculator.
*
* @copyright Oscar Litorell 2019
*/
/**
* Contains the colour and width of line when drawing on a canvas.
*/
class LineStyle {
/**
* @param {number} width - Width in px.
* @param {string} color - Colour in hex e.g. "#F9DA2C".
*/
constructor(width, color) {
this.width = width;
this.color = color;
}
}
/**
* Holds information about the viewport perspective and position relative to the coordinate system.
* @property {number} longitude - Longitude angle (degrees)
* @property {number} latitude - Latitude angle (degrees)
* @property {string} projection - Either "perspective" or "orthogonal".
* @property {number[]} offset - Coordinates for center of rotation.
* @property {number} zoom - Zoom for perspective mode this equates to distance from the center of rotation).
*/
class View {
/**
* @param {number} [longitude] - Longitude angle (degrees).
* @param {number} [latitude] - Latitude angle (degrees).
* @param {number} [offset] - Coordinates for center of rotation.
* @param {number} [zoom] - Zoom for perspective mode this equates to distance from the center of rotation).
* @param {string} [projection] - Type of projection (either "perspective" or "orthogonal").
*/
constructor(longitude = 0, latitude = 0, offset = [0, 0, 0], zoom = 100, projection="perspective") {
this._longitude = longitude;
this._latitude = latitude;
this.offset = offset;
this.zoom = zoom;
this.projection = projection;
this.updateMatrix();
}
// Setters and getters because the view matrix needs to be updated when the rotation changes.
set longitude(value) {
this._longitude = value;
this.updateMatrix();
}
get longitude() {
return this._longitude;
}
set latitude(value) {
this._latitude = value;
this.updateMatrix();
}
get latitude() {
return this._latitude;
}
/**
* Snap the viewing angle to the closest 45 degrees (both longitudinally and laterally).
*/
snapAngle() {
this._latitude = Math.round((this.latitude / 45)) * 45;
this._longitude = Math.round((this.longitude / 45)) * 45;
this.updateMatrix();
updateCanvas();
}
/**
* Snap the center of rotation to an integer position.
*/
snapPosition() {
this.offset = this.offset.map(x => Math.round(x));
xOffset.value = this.offset[0];
yOffset.value = this.offset[1];
zOffset.value = this.offset[2];
updateCanvas();
}
/**
* Update the rotation matrix based on the current longitude and latitude.
*/
updateMatrix() {
let lo = this._longitude / 180 * Math.PI;
let la = this._latitude / 180 * Math.PI;
let cos = Math.cos;
let sin = Math.sin;
let longMatrix = new Matrix([
[ cos(lo), 0, sin(lo)],
[ 0, 1, 0],
[-sin(lo), 0, cos(lo)]
]);
let latMatrix = new Matrix([
[1, 0, 0],
[0, cos(la), sin(la)],
[0, -sin(la), cos(la)]
]);
this.matrix = Matrix.multiplication(latMatrix, longMatrix);
}
/**
* Get the 3D coordinate of a certain point on the canvas a given distance away.
* @param {number[]} point - The coordinates on the canvas, with [0, 0] being in the middle and positive values up and to the right.
* @param {number} distance - The distance that the point is. Will default to the distance to the rotation point.
* @returns {number[]}
*/
getProjectedVector(point, distance=null) {
if (distance === null) distance = this.zoom;
if (this.projection === "perspective") {
let fovCoeff = Math.max(mainCanvas.width, mainCanvas.height) * 0.8;
point[0] *= distance / fovCoeff;
point[1] *= distance / fovCoeff;
} else {
point[0] *= this.zoom / 600;
point[1] *= this.zoom / 600;
}
distance -= this.zoom;
let projectedVector = this.matrix.transpose.transformVector([point[0], point[1], distance]);
projectedVector[0] += this.offset[0];
projectedVector[1] += this.offset[1];
projectedVector[2] += this.offset[2];
return projectedVector;
}
/**
* Get the canvas coordinates of a given vector.
* @param {number[]} vectorInput
* @returns {number[]} The coordinates on the canvas, with [0, 0] being in the middle and positive values up and to the right.
*/
projectVector(vectorInput) {
return (this.projection === "perspective") ? this.projectVectorPerspective(vectorInput) : this.projectVectorOrtho(vectorInput);
}
/**
* Get the canvas coordinates of a given vector using a perspective projection (like a pinhole camera).
* @param {number[]} vectorInput
* @returns {number[]} The coordinates on the canvas, with [0, 0] being in the middle and positive values up and to the right.
*/
projectVectorPerspective(vectorInput) {
let vector = [vectorInput[0] - this.offset[0], vectorInput[1] - this.offset[1], vectorInput[2] - this.offset[2]];
let transformed = this.matrix.transformVector(vector);
let fovCoeff = Math.max(mainCanvas.width, mainCanvas.height) * 0.8;
transformed[2] += this.zoom;
let x = transformed[0] / transformed[2] * fovCoeff;
let y = transformed[1] / transformed[2] * fovCoeff;
return [x, y];
}
/**
* Get the canvas coordinates of a given vector using an orthogonal projection (parallel lines remain parallel).
* @param {number[]} vectorInput
* @returns {number[]} The coordinates on the canvas, with [0, 0] being in the middle and positive values up and to the right.
*/
projectVectorOrtho(vectorInput) {
let vector = [vectorInput[0] - this.offset[0], vectorInput[1] - this.offset[1], vectorInput[2] - this.offset[2]];
let transformed = this.matrix.transformVector(vector);
let x = transformed[0] / this.zoom * 900;
let y = transformed[1] / this.zoom * 900;
return [x, y];
}
/**
* Cut a line such that only the section in front of the "camera" is rendered.
* @param {number[]} lineStart - Vector for the start of the line.
* @param {number[]} lineEnd - Vector for the end of the line.
* @returns {number[][]} Array with the start and end vectors of the part of the line in front of the camera.
*/
calculateClip(lineStart, lineEnd) {
let vector1 = [lineStart[0] - this.offset[0], lineStart[1] - this.offset[1], lineStart[2] - this.offset[2]];
let vector2 = [lineEnd[0] - this.offset[0], lineEnd[1] - this.offset[1], lineEnd[2] - this.offset[2]];
let transformed1 = this.matrix.transformVector(vector1);
let transformed2 = this.matrix.transformVector(vector2);
transformed1[2] += this.zoom;
transformed2[2] += this.zoom;
// If at least one of the points is behind the camera
if (transformed1[2] < 0 || transformed2[2] < 0) {
// If there is clipping (ONLY one point is behind the camera)
if (transformed1[2] < 0 !== transformed2[2] < 0) {
// Distance between the points in the direction parallel to the direction the camera is pointing
let dz = transformed2[2] - transformed1[2];
// Output points
let point1;
let point2;
// If the first point is behind the camera
if (transformed1[2] < 0) {
point1 = [...lineEnd];
let c = transformed2[2] / Math.abs(dz);
point2 = [lineEnd[0] - (lineEnd[0] - lineStart[0]) * c * 0.99, lineEnd[1] - (lineEnd[1] - lineStart[1]) * c * 0.99, lineEnd[2] - (lineEnd[2] - lineStart[2]) * c * 0.99];
// If the second point is behind the camera
} else {
point1 = [...lineStart];
let c = transformed1[2] / Math.abs(dz);
point2 = [lineStart[0] - (lineStart[0] - lineEnd[0]) * c * 0.99, lineStart[1] - (lineStart[1] - lineEnd[1]) * c * 0.99, lineStart[2] - (lineStart[2] - lineEnd[2]) * c * 0.99];
}
return [point1, point2];
}
// Entire line is behind the camera, no need to render.
return null;
}
// No clipping
return [[...lineStart], [...lineEnd]];
}
}
/**
* Draw a line on the canvas between two 3D points.
* @param {HTMLElement} canvas - The canvas to draw on.
* @param {number[]} lineStart - Vector for the start of the line.
* @param {number[]} lineEnd - Vector for the end of the line.
* @param {LineStyle} lineStyle - Thickness and colour of the line.
* @param {View} view - How the camera is positioned.
*/
function drawCanvas3d(canvas, lineStart, lineEnd, lineStyle, view) {
let ctx = canvas.getContext("2d");
let width = canvas.width;
let height = canvas.height;
let originalStyle = new LineStyle(ctx.lineWidth, ctx.strokeStyle);
ctx.lineWidth = lineStyle.width;
ctx.strokeStyle = lineStyle.color;
let point1;
let point2;
if (view.projection === "perspective") {
// Perspective projection
clipped = view.calculateClip(lineStart, lineEnd);
if (clipped == null) return;
point1 = clipped[0];
point2 = clipped[1];
} else {
// Orthogonal projection
point1 = lineStart;
point2 = lineEnd;
}
// Start and end pixel coordinates
let start = view.projectVector(point1);
let end = view.projectVector(point2);
// Center graph
start[0] += width * 0.5;
start[1] = height * 0.5 - start[1];
end[0] += width * 0.5;
end[1] = height * 0.5 - end[1];
// Draw
ctx.beginPath();
ctx.moveTo(...start);
ctx.lineTo(...end);
ctx.stroke();
ctx.lineWidth = originalStyle.width;
ctx.strokeStyle = originalStyle.color;
}
function cssVar(name) {
return getComputedStyle(document.documentElement).getPropertyValue(name);
}
var functionColor = cssVar("--function-color");
var labelBackground = cssVar("--label-bg");
var labelColor = cssVar("--label-color");
// 1px black
var lineStyle = new LineStyle(1, "#000000");
// Main canvas in the document.
var mainCanvas = document.getElementById("maincanvas");
/**
* Draw the x, y (re) and z (im) axis lines as well as 1 numbers.
* @param {HTMLElement} canvas - The canvas to draw on.
* @param {View} view - How the camera is positioned.
*/
function drawAxisLines(canvas, view) {
let pixelRatio = window.devicePixelRatio;
let width = canvas.width;
let height = canvas.height;
// Draw lines
drawCanvas3d(canvas, [-20, 0, 0], [0, 0, 0], new LineStyle(1 * pixelRatio, "#00FF00"), view);
drawCanvas3d(canvas, [ 20, 0, 0], [0, 0, 0], new LineStyle(2 * pixelRatio, "#00FF00"), view);
drawCanvas3d(canvas, [ 0, -20, 0], [0, 0, 0], new LineStyle(1 * pixelRatio, "#FF0000"), view);
drawCanvas3d(canvas, [ 0, 20, 0], [0, 0, 0], new LineStyle(2 * pixelRatio, "#FF0000"), view);
drawCanvas3d(canvas, [ 0, 0, -20], [0, 0, 0], new LineStyle(1 * pixelRatio, "#0000FF"), view);
drawCanvas3d(canvas, [ 0, 0, 20], [0, 0, 0], new LineStyle(2 * pixelRatio, "#0000FF"), view);
let ctx = canvas.getContext("2d");
ctx.font = `${20 * pixelRatio}px sans-serif`;
ctx.fillStyle = functionColor;
let xVector;
let yVector;
let zVector;
// Draw numbers
// If statements check if the number is in front of the camera
if (view.projection === "orthogonal" || view.matrix.transformVector(Vector.subtraction([1, 0, 0], view.offset))[2] + view.zoom > 0) {
xVector = view.projectVector([1, 0, 0]);
ctx.fillText(".1", xVector[0] + width * 0.5 - 2.5 * pixelRatio, -xVector[1] + height * 0.5 + 1 * pixelRatio);
}
if (view.projection === "orthogonal" || view.matrix.transformVector(Vector.subtraction([0, 1, 0], view.offset))[2] + view.zoom > 0) {
yVector = view.projectVector([0, 1, 0]);
ctx.fillText(".1", yVector[0] + width * 0.5 - 2.5 * pixelRatio, -yVector[1] + height * 0.5 + 1 * pixelRatio);
}
if (view.projection === "orthogonal" || view.matrix.transformVector(Vector.subtraction([0, 0, 1], view.offset))[2] + view.zoom > 0) {
zVector = view.projectVector([0, 0, 1]);
ctx.fillText(".1", zVector[0] + width * 0.5 - 2.5 * pixelRatio, -zVector[1] + height * 0.5 + 1 * pixelRatio);
}
}
/**
* Draws all values for a function on the canvas
* @param {HTMLElement} canvas - The canvas to draw on.
* @param {number} begin - The start of the domain of the function.
* @param {number} end - The end of the domain of the function.
* @param {number} [step] - The distance between each sample (the resolution).
* @param {View} view - How the camera is positioned.
*/
function drawFunction(canvas, begin, end, step = 0.1, view) {
let pixelRatio = window.devicePixelRatio;
let style = new LineStyle(1.5 * pixelRatio, functionColor);
let reStyle = new LineStyle(0.5 * pixelRatio, "#FF4444");
let imStyle = new LineStyle(0.5 * pixelRatio, "#4444FF");
let lastValue = resultList[0];
for (let j = 0; j < lastValue.length; j++) {
// Real component line
drawCanvas3d(canvas, [begin, 0, 0], [begin, lastValue[j].re, 0], reStyle, view);
// Imaginary component line
drawCanvas3d(canvas, [begin, 0, 0], [begin, 0, lastValue[j].im], imStyle, view);
}
// All values for the function are precalculated when the function updates, and stored in the resultList array.
for (let i = 1; i <= (end - begin) / step; i++) {
let x = i * step + begin
//let result = func(new Complex(x, 0));
let result = resultList[i];
// Each element in the result (for when there are several functions)
for (let j = 0; j < result.length; j++) {
// Function line
drawCanvas3d(canvas, [x - step, lastValue[j].re, lastValue[j].im], [x, result[j].re, result[j].im], style, view);
// Real component line
drawCanvas3d(canvas, [x, 0, 0], [x, result[j].re, 0], reStyle, view); // Vertical re lines
drawCanvas3d(canvas, [x - step, lastValue[j].re, 0], [x, result[j].re, 0], reStyle, view);
// Imaginary component line
drawCanvas3d(canvas, [x, 0, 0], [x, 0, result[j].im], imStyle, view); // Horizontal im lines
drawCanvas3d(canvas, [x - step, 0, lastValue[j].im], [x, 0, result[j].im], imStyle, view);
}
lastValue = result;
}
}
/**
* Draw the label when tracing the function.
* @param {HTMLElement} canvas - The canvas to draw on.
* @param {View} view - How the camera is positioned.
*/
function drawLabel(canvas, view) {
let pixelRatio = window.devicePixelRatio;
let width = canvas.width;
let height = canvas.height;
let ctx = canvas.getContext("2d");
if (functionText.length > 0) {
userFunction(tracingPoint).forEach((tracingResult) => {
if (tracingResult.constructor !== Complex) tracingResult = new Complex(tracingResult);
if (view.projection === "orthogonal" || view.matrix.transformVector([tracingPoint, tracingResult.re, tracingResult.im])[2] + view.zoom > 0) {
let tracing = view.projectVector([tracingPoint, tracingResult.re, tracingResult.im]);
originalFill = ctx.fillStyle;
ctx.fillStyle = labelBackground;
let pointX = tracing[0] + width * 0.5;
let pointY = -tracing[1] + height * 0.5;
ctx.font = `${14 * pixelRatio}px sans-serif`;
let line1 = `x = ${Math.round(tracingPoint * 100) / 100}`;
let line2 = `z = ${tracingResult.print(2)}`;
let labelWidth = Math.max(ctx.measureText(line1).width, ctx.measureText(line2).width);
ctx.fillRect(pointX + 10 * pixelRatio, pointY - 20 * pixelRatio, 10 * pixelRatio + labelWidth, 40 * pixelRatio);
ctx.beginPath();
ctx.moveTo(pointX, pointY);
ctx.lineTo(pointX + 10 * pixelRatio, pointY - 5 * pixelRatio);
ctx.lineTo(pointX + 10 * pixelRatio, pointY + 5 * pixelRatio)
ctx.closePath();
ctx.fill();
ctx.fillStyle = labelColor;
ctx.fillText(line1, pointX + 15 * pixelRatio, pointY - 4 * pixelRatio);
ctx.fillText(line2, pointX + 15 * pixelRatio, pointY + 14 * pixelRatio);
ctx.fillStyle = originalFill;
}
});
}
}
var zoom = document.getElementById("zoom");
var xOffset = document.getElementById("x-offset");
var yOffset = document.getElementById("y-offset");
var zOffset = document.getElementById("z-offset");
var view = new View(40, 30, [xOffset.value, yOffset.value, zOffset.value].map(x => Number(x)), 5);
// Contains all the values for the result of the function(s)
var resultList = [];
/**
* Update the view and canvas when an offset is changed.
*/
function updateView() {
view.offset = [xOffset.value, yOffset.value, zOffset.value].map(x => Number(x));
updateCanvas();
}
xOffset.oninput = updateView;
yOffset.oninput = updateView;
zOffset.oninput = updateView;
let updateRequested = false;
/**
* Update the canvas and redraw the axis lines and function lines. Normally called when the view is changed.
*/
function updateCanvas() {
if (!updateRequested) {
updateRequested = true;
window.requestAnimationFrame(() => {
mainCanvas.getContext("2d").clearRect(0, 0, mainCanvas.width, mainCanvas.height);
// Start and end of the function
let minX = document.getElementById("minX").value;
let maxX = document.getElementById("maxX").value;
let resolution = Math.abs(document.getElementById("resolution").value);
if (resolution === 0) resolution = 0.05; // Resolution cannot be 0, leads to ZeroDivisionError
drawAxisLines(mainCanvas, view);
drawFunction(mainCanvas, Math.min(minX, maxX), Math.max(minX, maxX), resolution, view);
drawLabel(mainCanvas, view);
updateRequested = false;
});
}
}
/**
* Update the values in resultList by calling func for each value of x.
* @param {function} func - The function to call.
*/
function updateFunctionValues(func) {
let begin = Number(document.getElementById("minX").value);
let end = Number(document.getElementById("maxX").value);
let step = Math.abs(document.getElementById("resolution").value);
resultList = [];
for (let i = 0; i <= (end - begin) / step; i++) {
let x = i * step + begin
let result = func(new Complex(x, 0));
resultList.push(result);
}
}
/**
* @member {Object[]} variableList
* List of all variables, with e and pi as default.
*/
let variableList = [
{
name: "pi",
type: "constant",
value: new Complex(Math.PI)
},
{
name: "e",
type: "constant",
value: new Complex(Math.E)
},
];
// Gets the index of a variable in variableList.
// Returns -1 if it doesn't exist.
function getVariableIndex(name) {
for (let i = 0; i < variableList.length; i++) {
if (variableList[i].name === name) return i;
}
return -1;
}
/**
* Convert a complex number string e.g. "4" or "3i" into a Complex object.
* @param {string} num
* @returns {Complex}
*/
function parseNumber(num) {
if (num[num.length - 1] !== "i") {
return new Complex(num);
} else {
if (num === "i") num = "1i";
return new Complex(0, num.substring(0, num.length - 1));
}
}
let functionText = "";
/**
* Updates the function values and the canvas, and is normally called when the user clicks the "set function" button.
*/
function updateFunction() {
functionText = document.getElementById("function").value;
updateFunctionValues(userFunction);
updateCanvas();
}
// The symbols for the available functions.
let operations = {
"+": {args: 2, function: Complex.add},
"-": {args: 2, function: Complex.subtract},
"*": {args: 2, function: Complex.multiply},
"/": {args: 2, function: Complex.divide},
"^": {args: 2, function: Complex.raise},
"ln": {args: 1, function: Complex.ln},
"abs": {args: 1, function: Complex.abs},
"sin": {args: 1, function: Complex.sin},
"cos": {args: 1, function: Complex.cos},
"tan": {args: 1, function: Complex.tan},
"asin": {args: 1, function: Complex.asin},
"acos": {args: 1, function: Complex.acos},
"atan": {args: 1, function: Complex.atan}
};
/**
* Interprets the function the user has entered and runs it.
* @param {number} num - The x variable of the function the user entered.
* @returns {number} The result of running the function the user entered with the given x-value.
*/
function userFunction(num) {
// Variables assigned using equals symbol, calculated by the function (e.g. =var1)
let calculatedVars = {};
let functionList = [];
let lines = functionText.split("\n").filter(line => line.trim() !== "");
lines.forEach((line) => {
if ("=#".includes(line[0])) {
functionList.push(line);
} else {
ExpressionParser.parseExpression(line).forEach((token) => {
functionList.push(token);
});
}
});
// Stack of all the values the function has calculated.
// Read more here: https://en.wikipedia.org/wiki/Reverse_Polish_notation
let functionStack = [];
// For each line in the function
for (let i = 0; i < functionList.length; i++) {
let line = functionList[i].trim();
// Check if line is x
if (line === "x") {
functionStack.push(num);
// Check if line should be interpreted or if it is commented out or empty
} else if (line !== "" && line[0] !== "#") {
let variableIndex = getVariableIndex(line);
let isCalculatedVariable = line in calculatedVars;
if (line[0] === "=") {
// Store last element in the stack as a variable
calculatedVars[line.substring(1).trim()] = functionStack.pop();
} else if (operations.hasOwnProperty(line)) {
// Line is an operation
let operation = operations[line];
let values = functionStack.slice(functionStack.length - operation.args);
let value = operation.function.apply(this, values);
functionStack.length -= operation.args;
functionStack.push(value);
} else if (variableIndex !== -1) {
// Line is a user defined variable
functionStack.push(variableList[variableIndex].value);
} else if (isCalculatedVariable) {
// Line is a calculated variable
functionStack.push(calculatedVars[line.trim()]);
} else {
// Line is a number/constant
functionStack.push(parseNumber(line));
}
}
}
return functionStack;
}
let addVarElementBtn = document.getElementById("addVariableElement");
/**
* Add a new variable to variableList and update the HTML document.
* Normally called when the user presses the "add variable" button.
*/
function addVariableElement() {
variableList.push({
name: "",
type: "constant",
value: new Complex(0, 0),
min: 0,
max: 1,
step: 0.1
});
variableListToHTML();
}
/**
* Updates the list of variables based on the values in the HTML document.
* Normally called by event listeners when the user changes any of the variables in the HTML document.
* @param {Event} event
*/
function updateVariableList(event) {
let element = event.target;
let parent = element.parentElement;
let index = Array.prototype.slice.call(parent.parentElement.children).indexOf(parent);
// Type of the variable (e.g. constant or range)
let type = parent.children[1].value;
variableList[index].type = type;
variableList[index].name = parent.children[0].value
switch (type) {
case "constant":
variableList[index].value = new Complex(parent.children[2].value, parent.children[3].value);
break;
case "range":
variableList[index].value = new Complex((parent.children[3].value - parent.children[2].value) * parent.children[4].value * 0.005 + Number(parent.children[2].value));
variableList[index].min = parent.children[2].value;
variableList[index].max = parent.children[3].value;
break;
}
variableListToHTML();
}
/**
* Updates variableList and the HTML document when the user changes the type of a variable.
* @param {event} event
*/
function updateVariableType(event) {
let element = event.target;
let parent = element.parentElement;
let index = Array.prototype.slice.call(parent.parentElement.children).indexOf(parent);
let type = parent.children[1].value;
variableList[index].type = type;
variableListToHTML();
}
var variableTypes = [
"constant",
"range"
];
/**
* Delete a variable from variableList and the HTML document.
* Normally called when a user clicks a "delete" button.
* @param {event} event
*/
function deleteVariable(event) {
let element = event.target;
let parent = element.parentElement;
let index = Array.prototype.slice.call(parent.parentElement.children).indexOf(parent);
variableList.splice(index, 1);
variableListToHTML();
}
/**
* Shows the variables in variableList in the HTML document.
*/
function variableListToHTML() {
let variableElement = document.getElementById("variable-list");
variableElement.innerHTML = "";
// For each variable
for (let i = 0; i < variableList.length; i++) {
let htmlVariableElement = variableElement.appendChild(document.createElement("li"));
htmlVariableElement.name = i;
let html = "";
let type = variableList[i].type;
html += `Name: <input class="updateVariables" value="${variableList[i].name}">`
html += `Type:<select class="updateVariableType">`;
for (let j = 0; j < variableTypes.length; j++) {
html += "<option";
if (variableTypes[j] === type) {
html += " selected";
}
html += ` value="${variableTypes[j]}">${variableTypes[j]}</option>`;
}
html += "</select>";
switch (type) {
case "constant":
html += `
Real part: <input class="updateVariables" type="number" value="${variableList[i].value.re}">
Imaginary part: <input class="updateVariables" type="number" value="${variableList[i].value.im}">`;
break;
case "range":
let value = variableList[i].value.re;
value = Number((value + 1E-15).toFixed(13));
html += `
Min: <input class="updateVariables" type="number" value="${variableList[i].min}">
Max: <input class="updateVariables" type="number" value="${variableList[i].max}">
<input class="updateVariables" type="range" min="0" max="200" value="${200 * ((variableList[i].value.re - variableList[i].min) / (variableList[i].max - variableList[i].min))}">
<span>${value}</span>`
break;
}
html += `<p class="button" onclick="deleteVariable(event)">Delete</p>`
htmlVariableElement.innerHTML = html;
}
// For each HTML element with class "updateVariables"
[].forEach.call(document.getElementsByClassName("updateVariables"), function (element) {
if (element.type === "range") {
// Update when the user moves a slider
element.oninput = function(event) {
let element = event.target;
let parent = element.parentElement;
let index = Array.prototype.slice.call(parent.parentElement.children).indexOf(parent);
// Read slider value
let value = (parent.children[3].value - parent.children[2].value) * parent.children[4].value * 0.005 + Number(parent.children[2].value);
variableList[index].value = new Complex(value);
variableList[index].min = parent.children[2].value;
variableList[index].max = parent.children[3].value;
// Round slightly
parent.children[5].innerHTML = Number((value + 1E-15).toFixed(13));
updateFunctionValues(userFunction);
updateCanvas();
};
}
element.addEventListener("change", (event) => {
updateVariableList(event);
});
});
// For the type dropdown element in each variable
[].forEach.call(document.getElementsByClassName("updateVariableType"), function (element) {
element.addEventListener("change", (event) => {
updateVariableType(event);
});
});
updateFunctionValues(userFunction);
updateCanvas();
}
// "Add variable" button
addVarElementBtn.addEventListener("click", addVariableElement);
// When the user scrolls/zooms over the graph
mainCanvas.addEventListener("wheel", function(event) {
event.preventDefault(); // Prevent scrolling
view.zoom *= Math.pow(10, event.deltaY * 0.001);
updateCanvas();
});
let tracingPoint = 0;
/**
* Set the tracing point based on where the user's mouse is.
* @param {number} clickX - The x coordinate of the click, with 0 on the left side of the canvas.
* @param {number} clickY - The y coordinate of the click, with 0 on the top side of the canvas.
* @param {HTMLElement} canvas - The canvas that the user clicked.
*/
function setTracingPoint(clickX, clickY, canvas) {
let width = canvas.width;
let height = canvas.height;
clickX -= width * 0.5 / devicePixelRatio;
clickY -= height * 0.5 / devicePixelRatio;
clickY *= -1;
let unit;
if (view.projection === "perspective") {
unit = view.calculateClip([0, 0, 0], [1, 0, 0]);
} else {
unit = [[0, 0, 0], [1, 0, 0]];
}
let origin = view.projectVector(unit[0]);
let end = view.projectVector(unit[1]);
let angleUnit = Math.atan2(end[1] - origin[1], end[0] - origin[0]);
let distanceUnit = Math.sqrt(Math.pow(end[1] - origin[1], 2) + Math.pow(end[0] - origin[0], 2));
let angleClick = Math.atan2(clickY - origin[1], clickX - origin[0]);
let distanceClick = Math.sqrt(Math.pow(clickY - origin[1], 2) + Math.pow(clickX - origin[0], 2));
tracingPoint = distanceClick / distanceUnit * Math.cos(angleClick - angleUnit);
updateCanvas();
document.getElementById("trace-x").value = Math.round(tracingPoint * 100) / 100;
}
document.getElementById("trace-x").addEventListener("change", (event) => {
tracingPoint = Number(event.target.value);
updateCanvas();
});
/**
* Used to change the view when the user drags thew cursor/finger over the graph.
* Normally called when the user clicks/touches the graph window.
* @param {MouseEvent|TouchEvent} event
*/
function rotateGraph(event) {
let originalX;
let originalY;
let originalLongitude;
let originalLatitude;
if (event.offsetX) {
// User has clicked
originalX = event.offsetX;
originalY = event.offsetY;
} else {
// User has touched
event.preventDefault();
originalX = event.touches[0].pageX;
originalY = event.touches[0].pageY;
}
originalLatitude = view.latitude;
originalLongitude = view.longitude;
// User moves the mouse cursor
function mousemove(event) {
view.longitude = originalLongitude - (event.offsetX - originalX) * 0.5;
view.latitude = originalLatitude + (event.offsetY - originalY) * 0.5;
updateCanvas();
}
// User moves one touch point
function touchMove(event) {
let x = event.touches[0].pageX;
let y = event.touches[0].pageY;
mousemove({
offsetX: x,
offsetY: y
});
updateCanvas();
}
function stopListeners() {
mainCanvas.removeEventListener("touchmove", touchMove);
mainCanvas.removeEventListener("touchend", stopListeners);
mainCanvas.removeEventListener("touchstart", stopListeners);
mainCanvas.removeEventListener("mousemove", mousemove);
}
mainCanvas.addEventListener("touchmove", touchMove);
mainCanvas.addEventListener("touchend", stopListeners);
mainCanvas.addEventListener("touchstart", stopListeners);
mainCanvas.addEventListener("mousemove", mousemove);
mainCanvas.addEventListener("mouseup", stopListeners);
}
/**
* Moves the graph translationally. Called when the user moves their fingers or mouse.
* @param {MouseEvent|TouchEvent} event
*/
function moveGraph(event) {
let width = mainCanvas.width;
let height = mainCanvas.height;
let originalX;
let originalY;
let originalZoom;