@zushah/chalkboard 1.5.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 +111 -0
- package/LICENSE.md +21 -0
- package/README.md +134 -0
- package/assets/Chalkboard-logo.png +0 -0
- package/docs/README.md +2 -0
- package/examples/README.md +12 -0
- package/examples/fluid.js +80 -0
- package/examples/hyperbolics.js +90 -0
- package/examples/mandelbrot.js +54 -0
- package/examples/matr-donut.js +51 -0
- package/examples/newton.js +54 -0
- package/examples/quat-donut.js +52 -0
- package/package.json +24 -0
- package/src/Chalkboard.js +3481 -0
- package/src/ChalkboardProcessing.js +3437 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# Chalkboard changelog
|
|
2
|
+
All notable changes of every update of the Chalkboard library are recorded in this file.
|
|
3
|
+
|
|
4
|
+
## [v1.5.0](https://www.github.com/Zushah/Chalkboard/releases/tag/v1.5.0) - 12/18/2023
|
|
5
|
+
The seventh release of the Chalkboard library.
|
|
6
|
+
Commits: [`v1.4.0...v1.5.0`](https://github.com/Zushah/Chalkboard/compare/v1.4.0...v1.5.0)
|
|
7
|
+
- Added Chalkboard as an npm package, so now it can be used on the server-side just like the client side
|
|
8
|
+
- Added `comp.function`, `comp.parse`, and `comp.val` to define, parse, and evaluate complex functions
|
|
9
|
+
- Added `calc.dfdz`, `calc.d2fdz2`, and `calc.fzdz` to calculate the first derivatives, second derivatives, and antiderivatives of complex functions
|
|
10
|
+
- Added approximations of functions using Taylor series with `calc.Taylor` and `plot.Taylor`
|
|
11
|
+
- Added `comp.pow` to calculate the exponentiation of a complex number
|
|
12
|
+
- Added `comp.root` to calculate the roots of a complex number
|
|
13
|
+
- Added `comp.invert` (calculates the multiplicative inverse of a complex number) to replace the functionality of `comp.reciprocate`
|
|
14
|
+
- Removed `stat.gte` and `stat.lte` and respectively incorporated their functionalities into `stat.gt` and `stat.lt`
|
|
15
|
+
- Changed `comp.reciprocate` to return the reciprocals of the components of a complex number instead of returning the multiplicative inverse of a complex number
|
|
16
|
+
- Changed `stat.gt` and `stat.lt` to have `includeEnd` parameters which determines whether the conditional they check for is "greater/less than" (input false) or "greater/less than or equal to" (input true)
|
|
17
|
+
- Changed `plot.barplot` and `plot.lineplot` to use the new functionalities of `stat.gt` and `stat.lt` instead of the removed `stat.gte` and `stat.lte`
|
|
18
|
+
- Changed `comp.Re` and `comp.Im` to respectively return the real and imaginary components of complex functions, too, instead of only the real and imaginary components of complex numbers
|
|
19
|
+
- Changed `stat.toObject` to return an object with its properties named based on the array's index instead of the array's element
|
|
20
|
+
- Changed `trig.arctan2` to use `Math.atan` instead of `trig.arctan` to be faster
|
|
21
|
+
- Changed the name of `calc.frdt` to `calc.frds`
|
|
22
|
+
- Changed `plot.function` to accept complex functions as inputs to plot their domain colorings
|
|
23
|
+
- Fixed the error messages in `calc.dfdrt` which were misspelled
|
|
24
|
+
- Fixed `plot.field` to include the upper bounds in its domain
|
|
25
|
+
|
|
26
|
+
## [v1.4.0](https://www.github.com/Zushah/Chalkboard/releases/tag/v1.4.0) - 12/11/2023
|
|
27
|
+
The sixth release of the Chalkboard library.
|
|
28
|
+
Commits: [`v1.3.1...v1.4.0`](https://github.com/Zushah/Chalkboard/compare/v1.3.1...v1.4.0)
|
|
29
|
+
- Added many new commands for arrays: creating an array of random numbers, calculating different types of the norm (and the squared norm) of an array, calculating the normalized array, calculating the array that has the numbers that are equal to, greater than (or equal to), less than (or equal to), or some compound inequality of another number or array, calculating the skewness and kurtosis of an array, calculating the different quartiles of an array, and converting an array to a vector, matrix, object, string, or printing it in the console
|
|
30
|
+
- Added `plot.barplot`, `plot.lineplot`, and `plot.scatterplot` to plot arrays of data
|
|
31
|
+
- Added `.constrain` commands for numbers, arrays, complex numbers, quaternions, vectors, and matrices to constrain their values within a range
|
|
32
|
+
- Added `trig.arctan2` to accurately calculate the angle between a vector and the positive x-axis
|
|
33
|
+
- Added `numb.sum` and `numb.mul` for the calculation of specified summation and product formulae
|
|
34
|
+
- Added `.reflect` and `.refract` commands for vectors to calculate reflection and refraction vectors
|
|
35
|
+
- Added `real.pingpong` to calculate the ping-pong function
|
|
36
|
+
- Added `geom.mid` to calculate the midpoint between two multidimensional points
|
|
37
|
+
- Changed all of the `plot` commands to return an array of data containing all of the ordered pairs of plotted points instead of returning a string with details on what was plotted and how
|
|
38
|
+
- Changed `real.parse` to have an optional command to input initializing code before the return function
|
|
39
|
+
- Changed `vec2.ang`, `comp.arg`, and `comp.ln` to use `trig.arctan2` instead of `trig.arctan`
|
|
40
|
+
- Changed `real.Heaviside`, `real.Dirac`, `real.ramp`, and `real.rect` to have parameters for the `edge` (the starting point) and the `scl` (scalar multiplication) of their respective functions
|
|
41
|
+
- Changed `comp.new` and `quat.new` to have a `type` property (like the `.new` commands for vectors) that's always set to `"comp"` and `"quat"`, respectively
|
|
42
|
+
- Changed `geom.dist` and `geom.distsq` to be written with loops instead of array methods
|
|
43
|
+
- Changed `calc.frdt` to also work for three-dimensional (instead of only two-dimensional) vector fields and parametric curves
|
|
44
|
+
- Fixed `calc.dfrdt` which didn't work due to a slight misspelling in an `if()` statement
|
|
45
|
+
|
|
46
|
+
## [v1.3.1](https://www.github.com/Zushah/Chalkboard/releases/tag/v1.3.1) - 12/04/2023
|
|
47
|
+
The fifth release of the Chalkboard library.
|
|
48
|
+
Commits: [`v1.3.0...v1.3.1`](https://github.com/Zushah/Chalkboard/compare/v1.3.0...v1.3.1)
|
|
49
|
+
- Fixed `matr.translator` which was misspelled
|
|
50
|
+
- Fixed `matr.rotator` which didn't work for two-dimensional rotations and was also misspelled
|
|
51
|
+
- Fixed `calc.grad` which didn't work for vector fields because of another misspelling
|
|
52
|
+
|
|
53
|
+
## [v1.3.0](https://www.github.com/Zushah/Chalkboard/releases/tag/v1.3.0) - 12/04/2023
|
|
54
|
+
The fourth release of the Chalkboard library.
|
|
55
|
+
Commits: [`v1.2.0...v1.3.0`](https://github.com/Zushah/Chalkboard/compare/v1.2.0...v1.3.0)
|
|
56
|
+
- Added multivariable calculus, with commands for directional differentiation, multivariable chain rule, gradient (partial differentiation), double gradient (second-order partial differentiation), divergence, curl, double integration, line/surface integration, arc length, surface area, and curvature
|
|
57
|
+
- Added second-order differentiation with `calc.d2fdx2`, unit tangent vectors for parametric curves with `calc.tangent`, unit normal vectors with `calc.normal`, and unit binormal vectors with `calc.binormal`
|
|
58
|
+
- Functions that previously had the `type` property of `"para"` should now be changed to `"curv"`
|
|
59
|
+
- Added `.field` and `.fromField` commmands for vectors which define vector fields and evaluate them at particular vectors, respectively
|
|
60
|
+
- Added `plot.field` and `plot.d2fdx2` to plot vector fields and second-order derivatives, respectively
|
|
61
|
+
- Added `.proj` and `.oproj` commands for vectors which calculate the vector projections and rejections, respectively
|
|
62
|
+
- Added commands for vectors to convert between coordinate systems (`vec2.fromPolar`, `vec3.toSpherical`, etc)
|
|
63
|
+
- Changed `matr.toVector` to have another optional parameter for the row/column of a matrix to define a vector from
|
|
64
|
+
- Added `comp.scl` and `quat.scl`, and changed all of the `.magset` commands to work using their respective `.scl` commands
|
|
65
|
+
- Added a `type` parameter to `stat.mean` to enable the calculation of different types of means: arithmetic, geometric, and harmonic
|
|
66
|
+
- Removed `geom.dist2D` and `geom.dist3D` and replaced them with `geom.dist` and `geom.distsq` which can calculate the distance between any-dimensional points
|
|
67
|
+
- Removed `vec2.rotate2D`, `vec3.rotatex`, `vec3.rotatey`, and `vec3.rotatez` because all vector rotations can be handled with rotation matrices
|
|
68
|
+
- Removed `matr.rotater2D` and `matr.rotater3D` and replaced them with `matr.rotater` which can do both 2D and 3D rotations depending on the number of parameters inputted (one parameter required for 2D rotations, three parameters required for 3D rotations)
|
|
69
|
+
- Removed `real.sqrt` from returning a complex number when the input is negative because it would sometimes cause bugs
|
|
70
|
+
- Changed `real.function`, `real.parse`, `calc.dfdx`, and `calc.fxdx` to work for the new `"mult"` function type for multivariable functions, the new `"curv"` function type for parametric curves, and the new `"surf"` function type for parametric surfaces
|
|
71
|
+
- Changed all `plot` commands so they can now optionally have their opacity changed with the `rgba` parameter (which previously used to only be `rgb`)
|
|
72
|
+
- Changed `numb.Gaussian` to use `for(;;)` instead of `while(typeof x === "undefined")`
|
|
73
|
+
- Changed the name of `calc.average` to `calc.mean`
|
|
74
|
+
- Fixed `matr.invert` returning `NaN` values when `0` is on the diagonal of a matrix, thanks to [@bhavjitChauhan](https://github.com/bhavjitChauhan).
|
|
75
|
+
|
|
76
|
+
## [v1.2.0](https://www.github.com/Zushah/Chalkboard/releases/tag/v1.2.0) - 11/27/2023
|
|
77
|
+
The third release of the Chalkboard library.
|
|
78
|
+
Commits: [`v1.1.0...v1.2.0`](https://github.com/Zushah/Chalkboard/compare/v1.1.0...v1.2.0)
|
|
79
|
+
- Added new matrix commands for the trace of a matrix, the row space of a matrix, the column space, and null space, the exponentation, the row Echelon form (Gaussian elimination), solving systems of linear equations, and conversion of a matrix to an object
|
|
80
|
+
- Added `plot.comp`, `plot.vec2`, `plot.vec3`, and `plot.matr` to replace `comp.display`, `vec2.display`, `vec3.display`, and `matr.display`
|
|
81
|
+
- Added `numb.binomial` to calculate binomial coefficients
|
|
82
|
+
- Removed `vec2.fromNumber`, `vec3.angcos`, `vec4.angcos`, and `matr.dimension`
|
|
83
|
+
- Removed all of the `.addScl` (scalar addition) commands for vectors
|
|
84
|
+
- Changed all of the `matr` commands to work for matrices of any and all dimensions (previously there was a four-by-four dimension limit)
|
|
85
|
+
- Changed all of the `.mulScl` (scalar multiplication) commands for vectors and matrices to be renamed as `.scl`
|
|
86
|
+
- Changed `numb.random` to have both of its parameters be optional (no parameters inputted returns a random number between 0 and 1)
|
|
87
|
+
- Changed `numb.Gaussian` to use `while(typeof x === "undefined")` instead of `while(true)`
|
|
88
|
+
- Fixed the `vec2.magset` and `vec3.magset` commands which would return vectors in the wrong dimensions
|
|
89
|
+
|
|
90
|
+
## [v1.1.0](https://www.github.com/Zushah/Chalkboard/releases/tag/v1.1.0) - 11/13/2023
|
|
91
|
+
The second release of the Chalkboard library.
|
|
92
|
+
Commits: [`v1.0.0...v1.1.0`](https://github.com/Zushah/Chalkboard/compare/v1.0.0...v1.1.0)
|
|
93
|
+
- Added `numb.prime` to calculate the nth prime number, `numb.nextPrime` to calculate the next prime number, and `numb.primeGap` to calculate the prime gap with an interval
|
|
94
|
+
- Added `numb.factors` to calculate the prime factorization of a number and `numb.divisors` to calculate the divisors of a number
|
|
95
|
+
- Added `numb.Gaussian` to calculate a random number based on a Gaussian distribution
|
|
96
|
+
- Added `.convolution`, `.correlation`, and `.autocorrelation` commands in the `stat`, `calc`, and `plot` categories to calculate and plot the discrete and continuous convolutions, cross-correlations, and autocorrelations of arrays and functions, respectively
|
|
97
|
+
- Added `stat.error` to calculate the standard error of an array
|
|
98
|
+
- Added `matr.cofactor` and `matr.adjugate` to calculate the cofactor and adjugate matrices of a matrix
|
|
99
|
+
- Removed `calc.rOdO` because polar differentation can now be done with `calc.fxdx` instead
|
|
100
|
+
- Changed `calc.fxdx` to work using Simpson's rule instead of the trapezoidal Riemann sum
|
|
101
|
+
- Changed `real.val`, `calc.dfdx`, and `calc.fxdx` to work for all Chalkboard functions regardless of their `type` (before they only worked for `"expl"` functions)
|
|
102
|
+
- Changed `matr.det` and `matr.transpose` to work for all matrices regardless of their dimension
|
|
103
|
+
- Changed `numb.Fibonacci` to have a cache so it works faster
|
|
104
|
+
- Changed the order of the lower and upper values in the `stat.confidenceInterval` return array
|
|
105
|
+
- Changed `plot.function` and `numb.factorial` to be more written slightly more efficiently
|
|
106
|
+
- Changed the name of the `position` parameter in `stat.Gaussian` to `mean`
|
|
107
|
+
- Changed the name of `numb.prime` (the command that checks if a number is prime) to `numb.isPrime`, so parts of `numb.primeArr` and `prime.compositeArr` had to be changed, too
|
|
108
|
+
|
|
109
|
+
## [v1.0.0](https://www.github.com/Zushah/Chalkboard/releases/tag/v1.0.0) - 11/06/2023
|
|
110
|
+
The first release of the Chalkboard library.
|
|
111
|
+
Commits: [`v1.0.0...main`](https://github.com/Zushah/Chalkboard/compare/v1.0.0...main)
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Zushah
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
<p align="center"><a href="https://www.github.com/Zushah/Chalkboard"><img src="./assets/Chalkboard-logo.png" width="50%"></a></p>
|
|
2
|
+
<p align="center">
|
|
3
|
+
<a href="https://www.github.com/Zushah/Chalkboard/releases/tag/v1.5.0"><img src="https://img.shields.io/github/v/release/Zushah/Chalkboard" alt="Latest release"></a>
|
|
4
|
+
<a href="https://www.codefactor.io/repository/github/zushah/chalkboard"><img src="https://www.codefactor.io/repository/github/zushah/chalkboard/badge" alt="CodeFactor grade"></a>
|
|
5
|
+
<a href="https://www.npmjs.com/package/@zushah/chalkboard"><img src="https://img.shields.io/badge/node-v20.10.0-limegreen" alt="Node.js support"></a>
|
|
6
|
+
<a href="https://github.com/Zushah/Chalkboard/blob/main/LICENSE.md"><img src="https://img.shields.io/badge/license-MIT-yellow" alt="MIT License"></a>
|
|
7
|
+
<a href="https://www.jsdelivr.com/package/gh/Zushah/Chalkboard"><img src="https://data.jsdelivr.com/v1/package/gh/Zushah/Chalkboard/badge" alt="JSDelivr requests"></a>
|
|
8
|
+
<br>
|
|
9
|
+
<a href="https://zushah.github.io/Chalkboard/home.html">https://zushah.github.io/Chalkboard/home.html</a>
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
# Contents
|
|
13
|
+
<ol>
|
|
14
|
+
<li><a href="#about">About</a></li>
|
|
15
|
+
<li><a href="#installation">Installation</a></li>
|
|
16
|
+
<li><a href="#documentation">Documentation</a></li>
|
|
17
|
+
<li><a href="#getting-started">Getting Started</a></li>
|
|
18
|
+
<li><a href="#contributing">Contributing</a></li>
|
|
19
|
+
<li><a href="#acknowledgments">Acknowledgments</a></li>
|
|
20
|
+
<li><a href="#contact">Contact</a></li>
|
|
21
|
+
</ol>
|
|
22
|
+
|
|
23
|
+
# About
|
|
24
|
+
The Chalkboard library is a JavaScript namespace that provides a plethora of both practical and abstract mathematical functionalities for its user. It was developed by [Zushah](https://www.github.com/Zushah) during 2022 and 2023 and then [released](https://github.com/Zushah/Chalkboard/releases/tag/v1.0.0) on November 6, 2023. As per the latest release of [v1.5.0](https://www.github.com/Zushah/Chalkboard/releases/tag/v1.5.0), Chalkboard has 431 different commands. The library is available for regular JavaScript on both the client-side and the server-side as well as [Proccesing.js](https://www.processing.org/) (fully compatible with [Khan Academy](https://www.khanacademy.org/cs/chalkboard/6708642430369792)). Chalkboard's website can be visited [here](https://zushah.github.io/Chalkboard/home.html).
|
|
25
|
+
|
|
26
|
+
# Installation
|
|
27
|
+
If your JavaScript project is being run on the client-side within a webpage, you can install Chalkboard with this HTML tag:
|
|
28
|
+
```html
|
|
29
|
+
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/Zushah/Chalkboard@1.5.0/src/Chalkboard.min.js"></script>
|
|
30
|
+
```
|
|
31
|
+
If your JavaScript project is being run on the server-side within the [Node.js](https://nodejs.org/en) environment, you can install Chalkboard with this console command:
|
|
32
|
+
```bash
|
|
33
|
+
npm install @zushah/chalkboard@1.5.0
|
|
34
|
+
```
|
|
35
|
+
Alternatively, you can simply download the [latest release](https://www.github.com/Zushah/Chalkboard/releases/tag/v1.5.0) and put the relevant files in your project's directory.
|
|
36
|
+
|
|
37
|
+
More methods and details about installing Chalkboard can be read [here](https://zushah.github.io/Chalkboard/installation.html).
|
|
38
|
+
|
|
39
|
+
# Documentation
|
|
40
|
+
These are all thirteen Chalkboard categories with a brief description of what they're mostly about, and listed in the order they appear in the source code:
|
|
41
|
+
- `numb` - Number-theory-related operations
|
|
42
|
+
- `real` - Real number and real function operations
|
|
43
|
+
- `comp` - Complex number and complex function operations
|
|
44
|
+
- `quat` - Quaternion operations
|
|
45
|
+
- `plot` - Plotting functions, complex numbers, vectors, matrices, and statistical graphs
|
|
46
|
+
- `geom` - Geometric formulae operations
|
|
47
|
+
- `trig` - Trigonometric function operations
|
|
48
|
+
- `stat` - Statistical array operations
|
|
49
|
+
- `vec2` - Two-dimensional vector operations
|
|
50
|
+
- `vec3` - Three-dimensional vector operations
|
|
51
|
+
- `vec4` - Four-dimensional vector operations
|
|
52
|
+
- `matr` - Multidimensional matrix operations
|
|
53
|
+
- `calc` - Several-variable calculus operations
|
|
54
|
+
|
|
55
|
+
There are also four "global" commands in Chalkboard, which are:
|
|
56
|
+
- `README()` - Prints basic information about Chalkboard in the console
|
|
57
|
+
- `LOGO()` - Draws the Chalkboard logo
|
|
58
|
+
- `PI()` - Computes the number pi
|
|
59
|
+
- `E()` - Computes the number e
|
|
60
|
+
|
|
61
|
+
The comprehensive Chalkboard documentation can be visited [here](https://zushah.github.io/Chalkboard/documentation.html).
|
|
62
|
+
|
|
63
|
+
# Getting Started
|
|
64
|
+
After installing Chalkboard into your program, you can immediately get started with using it. Every Chalkboard command begins with typing "Chalkboard" followed by a period, then the name of the category of the command (all categories are listed above in the [documentation](#documentation) section) with another period, and lastly the desired command itself.
|
|
65
|
+
```js
|
|
66
|
+
Chalkboard.category.command(parameters);
|
|
67
|
+
```
|
|
68
|
+
Here is some code that shows off only a few features of Chalkboard:
|
|
69
|
+
```js
|
|
70
|
+
const cb = Chalkboard; // Chalkboard is a nice name but it's also long
|
|
71
|
+
/*
|
|
72
|
+
Note that this code is written on the client-side
|
|
73
|
+
You can write all the same code on the server-side, too, but you would have to replace the line above with:
|
|
74
|
+
const cb = require("@zushah/chalkboard@1.5.0");
|
|
75
|
+
*/
|
|
76
|
+
|
|
77
|
+
let f = cb.real.function("Math.cos(2 * x)"); // f(x) = cos(2x)
|
|
78
|
+
let dfdx = cb.calc.dfdx(f, 2); // Derivative of f at x = 0
|
|
79
|
+
let fxdx = cb.calc.fxdx(f, 0, 2); // Antiderivative of f from x = 0 to x = 2
|
|
80
|
+
let fourier = cb.calc.Fourier(f, 2); // Fourier transform of f at x = 2
|
|
81
|
+
|
|
82
|
+
let f = cb.vec3.field("x", "y", "z"); // f(x, y, z) = (x, y, z)
|
|
83
|
+
let r = cb.real.function(["Math.cos(s) * Math.cos(t)", "Math.sin(s) * Math.cos(t)", "Math.sin(t)"], "surf"); // r(s, t) = (cos(s)cos(t), sin(s)cos(t), sin(t))
|
|
84
|
+
let fnds = cb.calc.fnds(f, r, cb.PI(-1/2), cb.PI(1/2), 0, cb.PI(2)); // Flux of the radial vector field through the unit sphere
|
|
85
|
+
|
|
86
|
+
let primes = cb.numb.primeArr(0, 100); // Array of prime numbers between 0 and 100
|
|
87
|
+
let midPrime = cb.stat.median(primes); // Median number in the primes array
|
|
88
|
+
cb.plot.barplot(primes, cb.stat.array(0, 100, 11), 5, [[0, 0, 0], [0, 0, 255]]); // Barplot of the primes array with a bin size of 10, a scale of 2, a stroke color of black, and a fill color of blue
|
|
89
|
+
|
|
90
|
+
let z = cb.comp.new(1, 1); // z = 1 + i
|
|
91
|
+
let zsqsqrt = cb.comp.sqrt(cb.comp.sq(z)); // The square root of z squared equals z
|
|
92
|
+
let f = cb.comp.function("a*a - b*b + 1", "2*a*b"); // f(z) = z^2 + 1 for z = a + bi (a complex-valued function)
|
|
93
|
+
cb.plot.function(f); // Plots the domain coloring of f
|
|
94
|
+
|
|
95
|
+
let r = cb.real.function(["Math.cos(t)", "Math.sin(t)"], "curv"); // r(t) = (cos(t), sin(t))
|
|
96
|
+
cb.plot.xyplane(2); // Draws the Cartesian coordinate plane
|
|
97
|
+
cb.plot.function(r, 2, [255, 0, 0] [0, cb.PI(1/2)]); // Plots r(t) colored red from t = 0 to t = π/2
|
|
98
|
+
|
|
99
|
+
let a = cb.vec3.new(1, 2, 3); // Vector a = (1, 2, 3)
|
|
100
|
+
let b = cb.vec3.new(4, 5, 6); // Vector b = (4, 5, 6)
|
|
101
|
+
let c = cb.vec3.new(7, 8, 9); // Vector c = (7, 8, 9)
|
|
102
|
+
let axbxc = cb.vec3.vectorTriple(a, b, c); // Triple cross product between a, b, and c
|
|
103
|
+
cb.vec3.print(axbxc); // Prints axbxc in the console
|
|
104
|
+
|
|
105
|
+
let m = cb.matr.new([0, 1, 1, 1, 1], // m is a 5x5 matrix
|
|
106
|
+
[1, 0, 1, 1, 1],
|
|
107
|
+
[1, 1, 0, 1, 1],
|
|
108
|
+
[1, 1, 1, 0, 1],
|
|
109
|
+
[1, 1, 1, 1, 0]);
|
|
110
|
+
let mi = cb.matr.invert(m); // mi is the inverse of m
|
|
111
|
+
let mmi = cb.matr.mul(m, mi); // mmi is the product of m and mi
|
|
112
|
+
cb.matr.print(mmi); // Prints mmi in the console
|
|
113
|
+
```
|
|
114
|
+
More examples that are more interesting can be seen [here](https://zushah.github.io/Chalkboard/examples.html) with their source code [here](https://github.com/Zushah/Chalkboard/tree/main/examples).
|
|
115
|
+
|
|
116
|
+
# Contributing
|
|
117
|
+
<ol>
|
|
118
|
+
<li>Fork Chalkboard's GitHub repository. Make sure the fork is based on the main branch.</li>
|
|
119
|
+
<li>Make your contributions to your fork. If you're adding new features or modifying the functionalities of old ones, please make sure you do the same for both editions (JavaScript and Processing.js) of the library.</li>
|
|
120
|
+
<li>When you're done, commit the changes to your fork with a detailed description.</li>
|
|
121
|
+
<li>Open a pull request. It will be reviewed soon and then merged to the main branch.</li>
|
|
122
|
+
</ol>
|
|
123
|
+
|
|
124
|
+
The changelog can be read [here](https://github.com/Zushah/Chalkboard/blob/main/CHANGELOG.md).
|
|
125
|
+
|
|
126
|
+
The Chalkboard library is available under the [MIT License](https://github.com/Zushah/Chalkboard/blob/main/LICENSE.md).
|
|
127
|
+
|
|
128
|
+
# Acknowledgements
|
|
129
|
+
Thanks to [Bhavjit Chauhan](https://github.com/bhavjitChauhan) for his [contribution](https://www.github.com/Zushah/Chalkboard/pull/1) to [v1.3.0](https://www.github.com/Zushah/Chalkboard/releases/tag/v1.3.0).\
|
|
130
|
+
Thanks to [G. Yang](https://www.github.com/gyang0) for his [contribution](https://www.github.com/Zushah/zushah.github.io/pull/1) to the [documentation](https://www.github.com/Zushah/zushah.github.io/tree/main/Chalkboard).
|
|
131
|
+
|
|
132
|
+
# Contact
|
|
133
|
+
[GitHub](https://www.github.com/Zushah)\
|
|
134
|
+
[Khan Academy](https://www.khanacademy.org/profile/zushah77)
|
|
Binary file
|
package/docs/README.md
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
# Chalkboard documentation
|
|
2
|
+
The documentation for [Chalkboard v1.5.0](https://www.github.com/Zushah/Chalkboard/releases/tag/v1.5.0) can be visited [here](https://zushah.github.io/Chalkboard/documentation.html/). Its source code is located [here](https://github.com/Zushah/zushah.github.io/blob/main/Chalkboard/documentation.html).
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Chalkboard examples
|
|
2
|
+
Six basic programs made by Zushah that utilize [Chalkboard v1.5.0](https://www.github.com/Zushah/Chalkboard/releases/tag/v1.5.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.html).
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/*
|
|
2
|
+
The Chalkboard Library ===> https://www.github.com/Zushah/Chalkboard
|
|
3
|
+
Version 1.5.0 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.vec2.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.vec2.new(cb.numb.random(-canvas.width / 2, canvas.width / 2), cb.numb.random(-canvas.height / 2, canvas.height / 2)); // Position vector
|
|
21
|
+
this.vel = cb.vec2.new(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.vec2.magset(cb.vec2.fromField(F, cb.vec2.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.vec2.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 = "rgb(0, 0, 0)";
|
|
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);
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/*
|
|
2
|
+
The Chalkboard Library ===> https://www.github.com/Zushah/Chalkboard
|
|
3
|
+
Version 1.5.0 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 = "rgb(255, 255, 255)";
|
|
17
|
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
18
|
+
cb.plot.xyplane(1, [0, 0, 0], [canvas.width / 2, canvas.height / 2], 2);
|
|
19
|
+
|
|
20
|
+
// Plot the unit hyperbola with a parametric function (see: https://en.wikipedia.org/wiki/Hyperbola)
|
|
21
|
+
var f = cb.real.function(["(t * t + 1) / (2 * t)", "(t * t - 1) / (2 * t)"], "curv");
|
|
22
|
+
cb.plot.function(f, 1, [100, 100, 255], [0, 10], [canvas.width / 2, canvas.height / 2], 4);
|
|
23
|
+
cb.plot.function(f, 1, [100, 100, 255], [-10, 0], [canvas.width / 2, canvas.height / 2], 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 = "rgb(0, 0, 0)";
|
|
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 = "rgb(0, 0, 0)";
|
|
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);
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/*
|
|
2
|
+
The Chalkboard Library ===> https://www.github.com/Zushah/Chalkboard
|
|
3
|
+
Version 1.5.0 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.new(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.new(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.mag(z) > 2) {
|
|
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();
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/*
|
|
2
|
+
The Chalkboard Library ===> https://www.github.com/Zushah/Chalkboard
|
|
3
|
+
Version 1.5.0 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 a parametric function (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.vec3.new(x, y, z));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function main() {
|
|
25
|
+
ctx.fillStyle = "rgb(255, 255, 255)";
|
|
26
|
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
27
|
+
|
|
28
|
+
ctx.save();
|
|
29
|
+
ctx.translate(canvas.width / 2, canvas.height / 2);
|
|
30
|
+
ctx.strokeStyle = "rgb(0, 0, 0)";
|
|
31
|
+
for(var i = 0; i < points.length; i++) {
|
|
32
|
+
for(var j = 0; j < points.length; j++) {
|
|
33
|
+
if(cb.vec3.dist(points[i], points[j]) < 25) {
|
|
34
|
+
// Draw lines between the donut's points to draw the donut
|
|
35
|
+
cb.geom.line3D(points[i].x, points[i].y, points[i].z, points[j].x, points[j].y, points[j].z);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
ctx.restore();
|
|
40
|
+
|
|
41
|
+
// Make the donut rotate with a rotation matrix
|
|
42
|
+
var r = cb.matr.rotator(cb.trig.toRad(1), cb.trig.toRad(1), cb.trig.toRad(1));
|
|
43
|
+
for(var i = 0; i < points.length; i++) {
|
|
44
|
+
var buffer = cb.vec3.toMatrix(points[i]); // Create a buffer matrix which has the donut's points
|
|
45
|
+
var rbuffer = cb.matr.mul(r, buffer); // Multiply the rotation matrix with the buffer matrix
|
|
46
|
+
points[i] = cb.matr.toVector(rbuffer, "vec3"); // Reassign the values of the donut's points to the new rotated points
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
window.requestAnimationFrame(main);
|
|
50
|
+
}
|
|
51
|
+
window.requestAnimationFrame(main);
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/*
|
|
2
|
+
The Chalkboard Library ===> https://www.github.com/Zushah/Chalkboard
|
|
3
|
+
Version 1.5.0 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 c1 = cb.numb.random(-3, 3);
|
|
16
|
+
var c2 = cb.numb.random(-3, 3);
|
|
17
|
+
var c3 = cb.numb.random(-3, 3);
|
|
18
|
+
var c4 = cb.numb.random(-3, 3);
|
|
19
|
+
var c5 = cb.numb.random(-3, 3);
|
|
20
|
+
var f = cb.real.function(c1 + " * x * x * x * x + " + c2 + " * x * x * x + " + c3 + " * x * x + " + c4 + " * x + " + c5);
|
|
21
|
+
|
|
22
|
+
// Newton's method's solution and tangent line (see: https://en.wikipedia.org/wiki/Newton's_method)
|
|
23
|
+
var root = cb.calc.Newton(f, [-5, 5]);
|
|
24
|
+
var y = cb.real.function(cb.calc.dfdx(f, root) + " * (x - " + root + ") + " + cb.real.val(f, root));
|
|
25
|
+
|
|
26
|
+
function main() {
|
|
27
|
+
ctx.fillStyle = "rgb(255, 255, 255)";
|
|
28
|
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
29
|
+
cb.plot.xyplane(1, [0, 0, 0], [canvas.width / 2, canvas.height / 2], 2);
|
|
30
|
+
|
|
31
|
+
// Draw the polynomial
|
|
32
|
+
cb.plot.function(f, 1, [100, 100, 255], [-10, 10], [canvas.width / 2, canvas.height / 2], 4);
|
|
33
|
+
ctx.fillStyle = "rgb(0, 0, 0)";
|
|
34
|
+
ctx.font = "50px Times New Roman";
|
|
35
|
+
ctx.fillText("f(x) = " + c1.toFixed(2) + "x⁴ + " + c2.toFixed(2) + "x³ + " + c3.toFixed(2) + "x² + " + c4.toFixed(2) + "x + " + c5.toFixed(2), 20, 70);
|
|
36
|
+
|
|
37
|
+
// Only draw the solution and the tangent line if the solution is correct
|
|
38
|
+
if(cb.real.val(f, root).toFixed(1) == 0) {
|
|
39
|
+
ctx.fillText("y = " + cb.calc.dfdx(f, root).toFixed(2) + "x - " + (cb.calc.dfdx(f, root) * root).toFixed(2), 20, 120);
|
|
40
|
+
ctx.fillText("A possible root is at x = " + root.toFixed(2), 20, 170);
|
|
41
|
+
cb.plot.function(y, 1, [255, 100, 100], [-10, 10], [canvas.width / 2, canvas.height / 2], 4);
|
|
42
|
+
ctx.save();
|
|
43
|
+
ctx.translate(canvas.width / 2, canvas.height / 2);
|
|
44
|
+
ctx.beginPath();
|
|
45
|
+
ctx.fillStyle = "rgb(0, 0, 0)";
|
|
46
|
+
ctx.ellipse(root * 100, 0, 5, 5, 0, 0, cb.PI(2));
|
|
47
|
+
ctx.fill();
|
|
48
|
+
ctx.restore();
|
|
49
|
+
} else {
|
|
50
|
+
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);
|
|
51
|
+
ctx.fillText("A possible root was not found", 20, 170);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
main();
|