@khanacademy/kmath 0.0.5 → 0.0.7
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/.babelrc.js +1 -1
- package/.eslintrc.js +6 -0
- package/CHANGELOG.md +12 -0
- package/README.md +124 -85
- package/dist/es/index.js +499 -1
- package/dist/es/index.js.map +1 -1
- package/dist/index.js +511 -1
- package/dist/index.js.map +1 -1
- package/package.json +14 -6
- package/src/logo.js +5 -3
- package/src/point.js +4 -4
- package/src/ray.js +2 -2
- package/src/vector.js +6 -14
package/package.json
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
{
|
|
2
|
+
"name": "@khanacademy/kmath",
|
|
3
|
+
"description": "Khan Academy's Javascript Numeric Math Utilities",
|
|
4
|
+
"author": "Khan Academy",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"version": "0.0.7",
|
|
2
7
|
"publishConfig": {
|
|
3
8
|
"access": "public"
|
|
4
9
|
},
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/Khan/kmath.git"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/Khan/perseus/issues"
|
|
16
|
+
},
|
|
8
17
|
"module": "dist/es/index.js",
|
|
9
18
|
"main": "dist/index.js",
|
|
10
19
|
"source": "src/index.js",
|
|
@@ -13,12 +22,11 @@
|
|
|
13
22
|
},
|
|
14
23
|
"dependencies": {},
|
|
15
24
|
"devDependencies": {
|
|
16
|
-
"perseus-build-settings": "^0.0.
|
|
25
|
+
"perseus-build-settings": "^0.0.4",
|
|
17
26
|
"underscore": "1.4.4"
|
|
18
27
|
},
|
|
19
28
|
"peerDependencies": {
|
|
20
29
|
"underscore": "1.4.4"
|
|
21
30
|
},
|
|
22
|
-
"
|
|
23
|
-
"license": "MIT"
|
|
31
|
+
"keywords": []
|
|
24
32
|
}
|
package/src/logo.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
|
+
/* eslint-disable import/no-default-export */
|
|
2
|
+
// @flow
|
|
1
3
|
// This file describes the graphie source code of the kmath logo
|
|
2
4
|
// currently used on khan.github.io.
|
|
3
5
|
//
|
|
4
6
|
// Also located at http://ka-perseus-graphie.s3.amazonaws.com/42ef3cbadc3e6464124533191728c3c5c55c7355.svg
|
|
5
7
|
|
|
6
|
-
// eslint-disable-next-line
|
|
8
|
+
// eslint-disable-next-line ft-flow/no-types-missing-file-annotation
|
|
7
9
|
declare var init: $FlowFixMe;
|
|
8
|
-
// eslint-disable-next-line
|
|
10
|
+
// eslint-disable-next-line ft-flow/no-types-missing-file-annotation
|
|
9
11
|
declare var ellipse: $FlowFixMe;
|
|
10
|
-
// eslint-disable-next-line
|
|
12
|
+
// eslint-disable-next-line ft-flow/no-types-missing-file-annotation
|
|
11
13
|
declare var line: $FlowFixMe;
|
|
12
14
|
|
|
13
15
|
const GREEN = "#28AE7B";
|
package/src/point.js
CHANGED
|
@@ -6,14 +6,14 @@
|
|
|
6
6
|
|
|
7
7
|
import _ from "underscore";
|
|
8
8
|
|
|
9
|
-
import * as kvector from "./vector.js";
|
|
10
9
|
import * as knumber from "./number.js";
|
|
10
|
+
import * as kvector from "./vector.js";
|
|
11
11
|
|
|
12
12
|
// A point, in 2D, 3D, or nD space.
|
|
13
13
|
export type Point = $ReadOnlyArray<number>;
|
|
14
14
|
|
|
15
15
|
// Rotate point (around origin unless a center is specified)
|
|
16
|
-
export function rotateRad(point: Point, theta: number, center
|
|
16
|
+
export function rotateRad(point: Point, theta: number, center?: Point): Point {
|
|
17
17
|
if (center === undefined) {
|
|
18
18
|
return kvector.rotateRad(point, theta);
|
|
19
19
|
} else {
|
|
@@ -24,7 +24,7 @@ export function rotateRad(point: Point, theta: number, center: Point): Point {
|
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
export function rotateDeg(point: Point, theta: number, center
|
|
27
|
+
export function rotateDeg(point: Point, theta: number, center?: Point): Point {
|
|
28
28
|
if (center === undefined) {
|
|
29
29
|
return kvector.rotateDeg(point, theta);
|
|
30
30
|
} else {
|
|
@@ -50,7 +50,7 @@ export function distanceToLine(point: Point, line: [Point, Point]): number {
|
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
// Reflect point over line
|
|
53
|
-
export function reflectOverLine(point:
|
|
53
|
+
export function reflectOverLine<P: Point>(point: P, line: [P, P]): P {
|
|
54
54
|
const lv = kvector.subtract(line[1], line[0]);
|
|
55
55
|
const pv = kvector.subtract(point, line[0]);
|
|
56
56
|
const projectedPv = kvector.projection(pv, lv);
|
package/src/ray.js
CHANGED
|
@@ -6,14 +6,14 @@
|
|
|
6
6
|
* traveling along the positive x-axis.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import * as kvector from "./vector.js";
|
|
10
9
|
import * as kpoint from "./point.js";
|
|
10
|
+
import * as kvector from "./vector.js";
|
|
11
11
|
|
|
12
12
|
import type {Point} from "./point";
|
|
13
13
|
|
|
14
14
|
export type Ray = [Point, Point];
|
|
15
15
|
|
|
16
|
-
export function equal(ray1: Ray, ray2: Ray, tolerance
|
|
16
|
+
export function equal(ray1: Ray, ray2: Ray, tolerance?: number): boolean {
|
|
17
17
|
// Compare the directions of the rays
|
|
18
18
|
const v1 = kvector.subtract(ray1[1], ray1[0]);
|
|
19
19
|
const v2 = kvector.subtract(ray2[1], ray2[0]);
|
package/src/vector.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import _ from "underscore";
|
|
8
|
+
|
|
8
9
|
import * as knumber from "./number.js";
|
|
9
10
|
|
|
10
11
|
type Vector = $ReadOnlyArray<number>;
|
|
@@ -121,6 +122,8 @@ export function collinear(v1: Vector, v2: Vector, tolerance?: number): boolean {
|
|
|
121
122
|
);
|
|
122
123
|
}
|
|
123
124
|
|
|
125
|
+
// TODO(jeremy) These coordinate conversion functions really only handle 2D points (ie. [number, number])
|
|
126
|
+
|
|
124
127
|
// Convert a cartesian coordinate into a radian polar coordinate
|
|
125
128
|
export function polarRadFromCart(
|
|
126
129
|
v: $ReadOnlyArray<number>,
|
|
@@ -148,7 +151,6 @@ export function polarDegFromCart(
|
|
|
148
151
|
*
|
|
149
152
|
* Examples:
|
|
150
153
|
* cartFromPolarRad(5, Math.PI)
|
|
151
|
-
* cartFromPolarRad([5, Math.PI])
|
|
152
154
|
*/
|
|
153
155
|
export function cartFromPolarRad(
|
|
154
156
|
radius: number,
|
|
@@ -161,7 +163,6 @@ export function cartFromPolarRad(
|
|
|
161
163
|
*
|
|
162
164
|
* Examples:
|
|
163
165
|
* cartFromPolarDeg(5, 30)
|
|
164
|
-
* cartFromPolarDeg([5, 30])
|
|
165
166
|
*/
|
|
166
167
|
export function cartFromPolarDeg(
|
|
167
168
|
radius: number,
|
|
@@ -190,25 +191,16 @@ export function rotateDeg(
|
|
|
190
191
|
}
|
|
191
192
|
|
|
192
193
|
// Angle between two vectors
|
|
193
|
-
export function angleRad(
|
|
194
|
-
v1: $ReadOnlyArray<number>,
|
|
195
|
-
v2: $ReadOnlyArray<number>,
|
|
196
|
-
): number {
|
|
194
|
+
export function angleRad(v1: Vector, v2: Vector): number {
|
|
197
195
|
return Math.acos(dot(v1, v2) / (length(v1) * length(v2)));
|
|
198
196
|
}
|
|
199
197
|
|
|
200
|
-
export function angleDeg(
|
|
201
|
-
v1: $ReadOnlyArray<number>,
|
|
202
|
-
v2: $ReadOnlyArray<number>,
|
|
203
|
-
): number {
|
|
198
|
+
export function angleDeg(v1: Vector, v2: Vector): number {
|
|
204
199
|
return (angleRad(v1, v2) * 180) / Math.PI;
|
|
205
200
|
}
|
|
206
201
|
|
|
207
202
|
// Vector projection of v1 onto v2
|
|
208
|
-
export function projection(
|
|
209
|
-
v1: $ReadOnlyArray<number>,
|
|
210
|
-
v2: $ReadOnlyArray<number>,
|
|
211
|
-
): $ReadOnlyArray<number> {
|
|
203
|
+
export function projection<V: Vector>(v1: V, v2: V): V {
|
|
212
204
|
const scalar = dot(v1, v2) / dot(v2, v2);
|
|
213
205
|
return scale(v2, scalar);
|
|
214
206
|
}
|