@zushah/chalkboard 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -2
- package/README.md +7 -7
- package/dist/Chalkboard.d.ts +56 -12
- package/dist/Chalkboard.d.ts.map +1 -1
- package/dist/Chalkboard.js +1977 -571
- package/dist/Chalkboard.js.map +1 -1
- package/package.json +7 -6
- package/examples/README.md +0 -12
- package/examples/fluid.js +0 -80
- package/examples/hyperbolics.js +0 -90
- package/examples/mandelbrot.js +0 -54
- package/examples/matr-donut.js +0 -51
- package/examples/newton.js +0 -50
- package/examples/quat-donut.js +0 -52
package/package.json
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zushah/chalkboard",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "The Chalkboard library provides a plethora of mathematical functionalities for its user.",
|
|
5
5
|
"main": "dist/Chalkboard.js",
|
|
6
|
-
"
|
|
6
|
+
"types": "dist/Chalkboard.d.ts",
|
|
7
|
+
"files": ["dist", "CHANGELOG.md", "LICENSE.md", "package.json", "README.md"],
|
|
7
8
|
"scripts": {
|
|
8
9
|
"build": "tsc --project tsconfig.json",
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
10
|
+
"lint": "eslint src/**",
|
|
11
|
+
"lint:fix": "eslint --fix src/**",
|
|
12
|
+
"format": "prettier src",
|
|
13
|
+
"format:fix": "prettier --write src",
|
|
13
14
|
"docs": "typedoc --options typedoc.json"
|
|
14
15
|
},
|
|
15
16
|
"repository": {
|
package/examples/README.md
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
# Chalkboard examples
|
|
2
|
-
Six basic programs made by Zushah that utilize [Chalkboard v2.0.0 al-Khwarizmi](https://www.github.com/Zushah/Chalkboard/releases/tag/v2.0.0) are located in this directory.
|
|
3
|
-
|
|
4
|
-
They can be viewed in action here:
|
|
5
|
-
- [fluid.js](https://zushah.github.io/Chalkboard/examples/fluid.html)
|
|
6
|
-
- [hyperbolics.js](https://zushah.github.io/Chalkboard/examples/hyperbolics.html)
|
|
7
|
-
- [mandelbrot.js](https://zushah.github.io/Chalkboard/examples/mandelbrot.html)
|
|
8
|
-
- [matr-donut.js](https://zushah.github.io/Chalkboard/examples/matr-donut.html)
|
|
9
|
-
- [newton.js](https://zushah.github.io/Chalkboard/examples/newton.html)
|
|
10
|
-
- [quat-donut.js](https://zushah.github.io/Chalkboard/examples/quat-donut.html)
|
|
11
|
-
|
|
12
|
-
The main webpage for the examples can be visited at the [Chalkboard website](https://zushah.github.io/Chalkboard/examples/index.html).
|
package/examples/fluid.js
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
The Chalkboard Library ===> https://www.github.com/Zushah/Chalkboard
|
|
3
|
-
Version 2.0.0 al-Khwarizmi Example Program: Fluid Flow
|
|
4
|
-
Authored by Zushah ===> https://www.github.com/Zushah
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
// Get the JavaScript Canvas API
|
|
8
|
-
var ctx = document.getElementById("canvas").getContext("2d");
|
|
9
|
-
canvas.width = window.innerWidth;
|
|
10
|
-
canvas.height = window.innerHeight;
|
|
11
|
-
|
|
12
|
-
var cb = Chalkboard; // Initialize Chalkboard as cb
|
|
13
|
-
|
|
14
|
-
// Vector field defined as F(x, y) = (-y, -x/(1 + x^2)^2), adapted from "Learning about Hamiltonian Monte Carlo" which can be found here: https://github.com/anvaka/fieldplay/blob/main/Awesome%20Fields.md
|
|
15
|
-
var F = cb.vect.field("y", "-x / ((1 + x * x) * (1 + x * x))");
|
|
16
|
-
|
|
17
|
-
// Basic particle system to simulate the fluid flow
|
|
18
|
-
class Particle {
|
|
19
|
-
constructor() {
|
|
20
|
-
this.pos = cb.vect.random(-canvas.width / 2, canvas.width / 2, 2); // Position vector
|
|
21
|
-
this.vel = cb.vect.init(0, 0); // Velocity vector
|
|
22
|
-
this.ppos = this.pos; // Previous position vector
|
|
23
|
-
}
|
|
24
|
-
update() {
|
|
25
|
-
// Update the particle's position and velocity
|
|
26
|
-
this.vel = cb.vect.magset(cb.vect.fromField(F, cb.vect.scl(this.pos, 1/100)), 5); // Velocity direction depends on the (scaled) vector field, velocity magnitude is set to always be 5
|
|
27
|
-
this.pos = cb.vect.add(this.pos, this.vel); // Velocity is added to the position
|
|
28
|
-
}
|
|
29
|
-
constrain() {
|
|
30
|
-
// Constrain the particle position within the canvas
|
|
31
|
-
// The canvas coordinates are within -width/-height and width/height so that the Cartestian point (0, 0) is in the center of the canvas
|
|
32
|
-
if (this.pos.x > canvas.width / 2) {
|
|
33
|
-
this.pos.x = -canvas.width / 2;
|
|
34
|
-
this.ppos = this.pos;
|
|
35
|
-
}
|
|
36
|
-
if (this.pos.x < -canvas.width / 2) {
|
|
37
|
-
this.pos.x = canvas.width / 2;
|
|
38
|
-
this.ppos = this.pos;
|
|
39
|
-
}
|
|
40
|
-
if (this.pos.y > canvas.height / 2) {
|
|
41
|
-
this.pos.y = -canvas.height / 2;
|
|
42
|
-
this.ppos = this.pos;
|
|
43
|
-
}
|
|
44
|
-
if (this.pos.y < -canvas.height / 2) {
|
|
45
|
-
this.pos.y = canvas.height / 2;
|
|
46
|
-
this.ppos = this.pos;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
draw() {
|
|
50
|
-
// Draw the particle
|
|
51
|
-
ctx.strokeStyle = "rgba(0, 0, 255, 0.1)";
|
|
52
|
-
ctx.lineWidth = 1;
|
|
53
|
-
ctx.save();
|
|
54
|
-
ctx.translate(canvas.width / 2, canvas.height / 2);
|
|
55
|
-
ctx.beginPath();
|
|
56
|
-
ctx.moveTo(this.pos.x, this.pos.y);
|
|
57
|
-
ctx.lineTo(this.ppos.x, this.ppos.y);
|
|
58
|
-
ctx.stroke();
|
|
59
|
-
ctx.restore();
|
|
60
|
-
this.ppos = this.pos;
|
|
61
|
-
this.update();
|
|
62
|
-
this.constrain();
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
// Create 500 particles in the particles array
|
|
66
|
-
var particles = [];
|
|
67
|
-
for (var i = 0; i < 500; i++) {
|
|
68
|
-
particles.push(new Particle());
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// Draw everything
|
|
72
|
-
ctx.fillStyle = "black";
|
|
73
|
-
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
74
|
-
function main() {
|
|
75
|
-
for (var i = 0; i < particles.length; i++) {
|
|
76
|
-
particles[i].draw();
|
|
77
|
-
}
|
|
78
|
-
window.requestAnimationFrame(main);
|
|
79
|
-
}
|
|
80
|
-
window.requestAnimationFrame(main);
|
package/examples/hyperbolics.js
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
The Chalkboard Library ===> https://www.github.com/Zushah/Chalkboard
|
|
3
|
-
Version 2.0.0 al-Khwarizmi Example Program: Hyperbolic Functions
|
|
4
|
-
Authored by Zushah ===> https://www.github.com/Zushah
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
// Get the JavaScript Canvas API
|
|
8
|
-
var ctx = document.getElementById("canvas").getContext("2d");
|
|
9
|
-
canvas.width = window.innerWidth;
|
|
10
|
-
canvas.height = window.innerHeight;
|
|
11
|
-
|
|
12
|
-
var cb = Chalkboard; // Initialize Chalkboard as cb
|
|
13
|
-
|
|
14
|
-
var theta = 0;
|
|
15
|
-
function main() {
|
|
16
|
-
ctx.fillStyle = "white";
|
|
17
|
-
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
18
|
-
cb.plot.xyplane({lineWidth: 2});
|
|
19
|
-
|
|
20
|
-
// Plot the unit hyperbola with a parametric function (see: https://en.wikipedia.org/wiki/Hyperbola)
|
|
21
|
-
var f = cb.real.define(["(t * t + 1) / (2 * t)", "(t * t - 1) / (2 * t)"], "curv");
|
|
22
|
-
cb.plot.definition(f, {strokeStyle: "rgb(100, 100, 255)", domain: [0, 10], lineWidth: 4});
|
|
23
|
-
cb.plot.definition(f, {strokeStyle: "rgb(100, 100, 255)", domain: [-10, 0], lineWidth: 4});
|
|
24
|
-
|
|
25
|
-
// The two main hyperbolic trigonometric functions, hyperbolic sine (sinh) and hyperbolic cosine (cosh)
|
|
26
|
-
var x = cb.trig.cosh(theta);
|
|
27
|
-
var y = cb.trig.sinh(theta);
|
|
28
|
-
|
|
29
|
-
// Show the values of all of the hyperbolic trigonometric functions as theta varies
|
|
30
|
-
ctx.strokeStyle = "rgb(255, 100, 100)";
|
|
31
|
-
ctx.lineWidth = 4;
|
|
32
|
-
ctx.save();
|
|
33
|
-
ctx.translate(canvas.width / 2, canvas.height / 2);
|
|
34
|
-
ctx.beginPath();
|
|
35
|
-
ctx.moveTo(x * 100, 0);
|
|
36
|
-
ctx.lineTo(x * 100, -y * 100);
|
|
37
|
-
ctx.stroke();
|
|
38
|
-
ctx.beginPath();
|
|
39
|
-
ctx.moveTo(0, -y * 100);
|
|
40
|
-
ctx.lineTo(x * 100, -y * 100);
|
|
41
|
-
ctx.stroke();
|
|
42
|
-
ctx.fillStyle = "black";
|
|
43
|
-
ctx.beginPath();
|
|
44
|
-
ctx.ellipse(x * 100, -y * 100, 5, 5, 0, 0, cb.PI(2));
|
|
45
|
-
ctx.fill();
|
|
46
|
-
ctx.restore();
|
|
47
|
-
ctx.fillStyle = "black";
|
|
48
|
-
ctx.font = "50px Times New Roman";
|
|
49
|
-
ctx.fillText("x² - y² = 1", 20, 70);
|
|
50
|
-
ctx.fillText("θ = " + theta.toFixed(2), 20, 120);
|
|
51
|
-
ctx.fillText("sinh(θ) = " + cb.trig.sinh(theta).toFixed(2), 20, 170);
|
|
52
|
-
ctx.fillText("cosh(θ) = " + cb.trig.cosh(theta).toFixed(2), 20, 220);
|
|
53
|
-
ctx.fillText("tanh(θ) = " + cb.trig.tanh(theta).toFixed(2), 20, 270);
|
|
54
|
-
ctx.fillText("csch(θ) = " + cb.trig.csch(theta).toFixed(2), 20, 320);
|
|
55
|
-
ctx.fillText("sech(θ) = " + cb.trig.sech(theta).toFixed(2), 20, 370);
|
|
56
|
-
ctx.fillText("coth(θ) = " + cb.trig.coth(theta).toFixed(2), 20, 420);
|
|
57
|
-
ctx.fillText("arcsinh(θ) = " + cb.trig.arcsinh(theta).toFixed(2), 20, 470);
|
|
58
|
-
if (cb.trig.arccosh(theta) !== undefined) {
|
|
59
|
-
ctx.fillText("arccosh(θ) = " + cb.trig.arccosh(theta).toFixed(2), 20, 520);
|
|
60
|
-
} else {
|
|
61
|
-
ctx.fillText("arccosh(θ) = undefined", 20, 520);
|
|
62
|
-
}
|
|
63
|
-
if (cb.trig.arctanh(theta) !== undefined) {
|
|
64
|
-
ctx.fillText("arctanh(θ) = " + cb.trig.arctanh(theta).toFixed(2), 20, 570);
|
|
65
|
-
} else {
|
|
66
|
-
ctx.fillText("arctanh(θ) = undefined", 20, 570);
|
|
67
|
-
}
|
|
68
|
-
if (cb.trig.arccsch(theta) !== undefined) {
|
|
69
|
-
ctx.fillText("arccsch(θ) = " + cb.trig.arccsch(theta).toFixed(2), 20, 620);
|
|
70
|
-
} else {
|
|
71
|
-
ctx.fillText("arccsch(θ) = undefined", 20, 620);
|
|
72
|
-
}
|
|
73
|
-
if (cb.trig.arcsech(theta) !== undefined) {
|
|
74
|
-
ctx.fillText("arcsech(θ) = " + cb.trig.arcsech(theta).toFixed(2), 20, 670);
|
|
75
|
-
} else {
|
|
76
|
-
ctx.fillText("arcsech(θ) = undefined", 20, 670);
|
|
77
|
-
}
|
|
78
|
-
if (cb.trig.arccoth(theta) !== undefined) {
|
|
79
|
-
ctx.fillText("arccoth(θ) = " + cb.trig.arccoth(theta).toFixed(2), 20, 720);
|
|
80
|
-
} else {
|
|
81
|
-
ctx.fillText("arccoth(θ) = undefined", 20, 720);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
theta += 0.05;
|
|
85
|
-
if (theta > 2.5) {
|
|
86
|
-
theta = -2.5;
|
|
87
|
-
}
|
|
88
|
-
window.requestAnimationFrame(main);
|
|
89
|
-
}
|
|
90
|
-
window.requestAnimationFrame(main);
|
package/examples/mandelbrot.js
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
The Chalkboard Library ===> https://www.github.com/Zushah/Chalkboard
|
|
3
|
-
Version 2.0.0 al-Khwarizmi Example Program: Mandelbrot Set
|
|
4
|
-
Authored by Zushah ===> https://www.github.com/Zushah
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
// Get the JavaScript Canvas API
|
|
8
|
-
var ctx = document.getElementById("canvas").getContext("2d");
|
|
9
|
-
canvas.width = window.innerWidth;
|
|
10
|
-
canvas.height = window.innerHeight;
|
|
11
|
-
var ratio = canvas.width / canvas.height;
|
|
12
|
-
|
|
13
|
-
var cb = Chalkboard; // Initialize Chalkboard as cb
|
|
14
|
-
|
|
15
|
-
// The Mandelbrot set will be rendered using an ImageData object from the Canvas API
|
|
16
|
-
var imageData = ctx.createImageData(canvas.width, canvas.height);
|
|
17
|
-
var pixels = imageData.data;
|
|
18
|
-
function main() {
|
|
19
|
-
for (var x = 0; x < canvas.width; x++) {
|
|
20
|
-
for (var y = 0; y < canvas.height; y++) {
|
|
21
|
-
// The complex numbers from the Mandelbrot set's definition (see: lines 33-34), z and c
|
|
22
|
-
var z = cb.comp.init(cb.numb.map(x, [0, canvas.width], [-2 * ratio, 2 * ratio]), cb.numb.map(y, [0, canvas.height], [-2, 2]));
|
|
23
|
-
var c = cb.comp.init(cb.numb.map(x, [0, canvas.width], [-2 * ratio, 2 * ratio]), cb.numb.map(y, [0, canvas.height], [-2, 2]));
|
|
24
|
-
|
|
25
|
-
var currIteration = 0; // The current iteration of the fractal
|
|
26
|
-
var maxIterations = 100; // The maximum iterations intended to be evaluated
|
|
27
|
-
|
|
28
|
-
while (currIteration < maxIterations) {
|
|
29
|
-
// The definition of the Mandelbrot set: f(z) = z^2 + c (see: https://en.wikipedia.org/wiki/Mandelbrot_set)
|
|
30
|
-
z = cb.comp.add(cb.comp.sq(z), c);
|
|
31
|
-
|
|
32
|
-
// Of course we can't keep iterating infinitely, so we'll just round infinity down to 2
|
|
33
|
-
if (cb.comp.magsq(z) > 4) {
|
|
34
|
-
break;
|
|
35
|
-
}
|
|
36
|
-
currIteration++; // Keep iterating
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// The fractal will be colored based on how many iterations it's gone through at a point
|
|
40
|
-
var color = cb.numb.map(cb.real.sqrt(cb.numb.map(currIteration, [0, maxIterations], [0, 1])), [0, 1], [0, 255]);
|
|
41
|
-
if (currIteration === maxIterations) {
|
|
42
|
-
color = 0;
|
|
43
|
-
}
|
|
44
|
-
var px = (x + y * canvas.width) * 4;
|
|
45
|
-
pixels[px + 0] = color;
|
|
46
|
-
pixels[px + 1] = color;
|
|
47
|
-
pixels[px + 2] = color;
|
|
48
|
-
pixels[px + 3] = 255;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
ctx.putImageData(imageData, 0, 0);
|
|
53
|
-
}
|
|
54
|
-
main();
|
package/examples/matr-donut.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
The Chalkboard Library ===> https://www.github.com/Zushah/Chalkboard
|
|
3
|
-
Version 2.0.0 al-Khwarizmi Example Program: Matrix Donut
|
|
4
|
-
Authored by Zushah ===> https://www.github.com/Zushah
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
// Get the JavaScript Canvas API
|
|
8
|
-
var ctx = document.getElementById("canvas").getContext("2d");
|
|
9
|
-
canvas.width = window.innerWidth;
|
|
10
|
-
canvas.height = window.innerHeight;
|
|
11
|
-
|
|
12
|
-
var cb = Chalkboard; // Initialize Chalkboard as cb
|
|
13
|
-
|
|
14
|
-
// Generate the donut's points with parametric equations (see: https://en.wikipedia.org/wiki/Torus)
|
|
15
|
-
var points = [];
|
|
16
|
-
for (var u = 0; u < cb.PI(2); u += cb.PI(1/16)) {
|
|
17
|
-
for (var v = 0; v < cb.PI(2); v += cb.PI(1/6)) {
|
|
18
|
-
var x = (75 + 30 * cb.trig.cos(v)) * cb.trig.cos(u);
|
|
19
|
-
var y = (75 + 30 * cb.trig.cos(v)) * cb.trig.sin(u);
|
|
20
|
-
var z = 30 * cb.trig.sin(v);
|
|
21
|
-
points.push(cb.vect.init(x, y, z));
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// Make the donut rotate with a rotation matrix
|
|
26
|
-
var r = cb.matr.rotator(0.01, 0.01, 0.01);
|
|
27
|
-
|
|
28
|
-
function main() {
|
|
29
|
-
ctx.fillStyle = "white";
|
|
30
|
-
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
31
|
-
|
|
32
|
-
ctx.save();
|
|
33
|
-
ctx.translate(canvas.width / 2, canvas.height / 2);
|
|
34
|
-
ctx.strokeStyle = "black";
|
|
35
|
-
for (var i = 0; i < points.length; i++) {
|
|
36
|
-
for (var j = 0; j < points.length; j++) {
|
|
37
|
-
if (cb.vect.distsq(points[i], points[j]) < 625) {
|
|
38
|
-
// Draw lines between the donut's points to draw the donut
|
|
39
|
-
cb.geom.line3D(points[i].x, points[i].y, points[i].z, points[j].x, points[j].y, points[j].z);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
ctx.restore();
|
|
44
|
-
|
|
45
|
-
for (var i = 0; i < points.length; i++) {
|
|
46
|
-
points[i] = cb.matr.mulVector(r, points[i]); // Multiply the rotation matrix with the points' vectors
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
window.requestAnimationFrame(main);
|
|
50
|
-
}
|
|
51
|
-
window.requestAnimationFrame(main);
|
package/examples/newton.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
The Chalkboard Library ===> https://www.github.com/Zushah/Chalkboard
|
|
3
|
-
Version 2.0.0 al-Khwarizmi Example Program: Newton's Method
|
|
4
|
-
Authored by Zushah ===> https://www.github.com/Zushah
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
// Get the JavaScript Canvas API
|
|
8
|
-
var ctx = document.getElementById("canvas").getContext("2d");
|
|
9
|
-
canvas.width = window.innerWidth;
|
|
10
|
-
canvas.height = window.innerHeight;
|
|
11
|
-
|
|
12
|
-
var cb = Chalkboard; // Initialize Chalkboard as cb
|
|
13
|
-
|
|
14
|
-
// Random fourth-degree polynomial function
|
|
15
|
-
var c = cb.stat.random(-3, 3, 5);
|
|
16
|
-
var f = cb.real.define(c[0] + " * x * x * x * x + " + c[1] + " * x * x * x + " + c[2] + " * x * x + " + c[3] + " * x + " + c[4]);
|
|
17
|
-
|
|
18
|
-
// Newton's method's solution and tangent line (see: https://en.wikipedia.org/wiki/Newton's_method)
|
|
19
|
-
var root = cb.calc.Newton(f, [-5, 5]);
|
|
20
|
-
var y = cb.real.define(cb.calc.dfdx(f, root) + " * (x - " + root + ") + " + cb.real.val(f, root));
|
|
21
|
-
|
|
22
|
-
function main() {
|
|
23
|
-
ctx.fillStyle = "rgb(255, 255, 255)";
|
|
24
|
-
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
25
|
-
cb.plot.xyplane({lineWidth: 2});
|
|
26
|
-
|
|
27
|
-
// Draw the polynomial
|
|
28
|
-
cb.plot.definition(f, {strokeStyle: "rgb(100, 100, 255)", lineWidth: 4});
|
|
29
|
-
ctx.fillStyle = "rgb(0, 0, 0)";
|
|
30
|
-
ctx.font = "50px Times New Roman";
|
|
31
|
-
ctx.fillText("f(x) = " + c[0].toFixed(2) + "x⁴ + " + c[1].toFixed(2) + "x³ + " + c[2].toFixed(2) + "x² + " + c[3].toFixed(2) + "x + " + c[4].toFixed(2), 20, 70);
|
|
32
|
-
|
|
33
|
-
// Draw the solution and the tangent line if the solution is correct
|
|
34
|
-
if (cb.real.val(f, root).toFixed(1) == 0) {
|
|
35
|
-
ctx.fillText("y = " + cb.calc.dfdx(f, root).toFixed(2) + "x - " + (cb.calc.dfdx(f, root) * root).toFixed(2), 20, 120);
|
|
36
|
-
ctx.fillText("A possible root is at x = " + root.toFixed(2), 20, 170);
|
|
37
|
-
cb.plot.definition(y, {strokeStyle: "rgb(255, 100, 100)", lineWidth: 4});
|
|
38
|
-
ctx.save();
|
|
39
|
-
ctx.translate(canvas.width / 2, canvas.height / 2);
|
|
40
|
-
ctx.beginPath();
|
|
41
|
-
ctx.fillStyle = "rgb(0, 0, 0)";
|
|
42
|
-
ctx.ellipse(root * 100, 0, 5, 5, 0, 0, cb.PI(2));
|
|
43
|
-
ctx.fill();
|
|
44
|
-
ctx.restore();
|
|
45
|
-
} else {
|
|
46
|
-
ctx.fillText("y = " + cb.calc.dfdx(f, root).toFixed(2) + "x - " + (cb.calc.dfdx(f, root) * root + cb.real.val(f, root)).toFixed(2), 20, 120);
|
|
47
|
-
ctx.fillText("A possible root was not found", 20, 170);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
main();
|
package/examples/quat-donut.js
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
The Chalkboard Library ===> https://www.github.com/Zushah/Chalkboard
|
|
3
|
-
Version 2.0.0 al-Khwarizmi Example Program: Quaternion Donut
|
|
4
|
-
Authored by Zushah ===> https://www.github.com/Zushah
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
// Get the JavaScript Canvas API
|
|
8
|
-
var ctx = document.getElementById("canvas").getContext("2d");
|
|
9
|
-
canvas.width = window.innerWidth;
|
|
10
|
-
canvas.height = window.innerHeight;
|
|
11
|
-
|
|
12
|
-
var cb = Chalkboard; // Initialize Chalkboard as cb
|
|
13
|
-
|
|
14
|
-
// Generate the donut's points with parametric equations (see: https://en.wikipedia.org/wiki/Torus)
|
|
15
|
-
var points = [];
|
|
16
|
-
for (var u = 0; u < cb.PI(2); u += cb.PI(1/16)) {
|
|
17
|
-
for (var v = 0; v < cb.PI(2); v += cb.PI(1/6)) {
|
|
18
|
-
var x = (75 + 30 * cb.trig.cos(v)) * cb.trig.cos(u);
|
|
19
|
-
var y = (75 + 30 * cb.trig.cos(v)) * cb.trig.sin(u);
|
|
20
|
-
var z = 30 * cb.trig.sin(v);
|
|
21
|
-
points.push(cb.vect.init(x, y, z));
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
var theta = 0;
|
|
25
|
-
function main() {
|
|
26
|
-
ctx.fillStyle = "black";
|
|
27
|
-
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
28
|
-
|
|
29
|
-
// Make the donut rotate with a rotation quaternion
|
|
30
|
-
var r = cb.quat.fromAxis(cb.vect.normalize(cb.vect.init(1, 1, 1)), theta);
|
|
31
|
-
var qoints = []; // We'll say that "qoints" are the new rotated points
|
|
32
|
-
for (var i = 0; i < points.length; i++) {
|
|
33
|
-
qoints.push(cb.quat.toRotation(r, points[i]));
|
|
34
|
-
}
|
|
35
|
-
theta += cb.trig.toRad(1);
|
|
36
|
-
|
|
37
|
-
ctx.save();
|
|
38
|
-
ctx.translate(canvas.width / 2, canvas.height / 2);
|
|
39
|
-
ctx.strokeStyle = "white";
|
|
40
|
-
for (var i = 0; i < qoints.length; i++) {
|
|
41
|
-
for (var j = 0; j < qoints.length; j++) {
|
|
42
|
-
if (cb.vect.distsq(qoints[i], qoints[j]) < 625) {
|
|
43
|
-
// Draw lines between the donut's qoints to draw the donut
|
|
44
|
-
cb.geom.line3D(qoints[i].x, qoints[i].y, qoints[i].z, qoints[j].x, qoints[j].y, qoints[j].z);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
ctx.restore();
|
|
49
|
-
|
|
50
|
-
window.requestAnimationFrame(main);
|
|
51
|
-
}
|
|
52
|
-
window.requestAnimationFrame(main);
|