@khanacademy/kmath 0.0.8 → 0.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/.babelrc.js +1 -1
- package/.eslintrc.js +1 -0
- package/CHANGELOG.md +6 -0
- package/dist/es/index.js +84 -85
- package/dist/es/index.js.map +1 -1
- package/dist/index.d.ts +5 -2
- package/dist/index.js +84 -85
- package/dist/index.js.flow +11 -2
- package/dist/index.js.map +1 -1
- package/dist/line.d.ts +10 -0
- package/dist/line.js.flow +16 -0
- package/dist/logo.d.ts +2 -0
- package/dist/logo.js.flow +8 -0
- package/dist/number.d.ts +29 -0
- package/dist/number.js.flow +44 -0
- package/dist/point.d.ts +34 -0
- package/dist/point.js.flow +52 -0
- package/dist/ray.d.ts +9 -0
- package/dist/ray.js.flow +13 -0
- package/dist/vector.d.ts +38 -0
- package/dist/vector.js.flow +74 -0
- package/package.json +2 -2
- package/src/__tests__/{line.test.js → line.test.ts} +1 -3
- package/src/__tests__/{number.test.js → number.test.ts} +4 -4
- package/src/__tests__/{point.test.js → point.test.ts} +1 -3
- package/src/__tests__/{vector.test.js → vector.test.ts} +1 -3
- package/src/index.ts +5 -0
- package/src/{line.js → line.ts} +3 -4
- package/src/{logo.js → logo.ts} +3 -7
- package/src/{number.js → number.ts} +4 -4
- package/src/{point.js → point.ts} +4 -5
- package/src/{ray.js → ray.ts} +2 -3
- package/src/{vector.js → vector.ts} +35 -38
- package/tsconfig.json +9 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/src/index.js +0 -7
package/dist/number.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Number Utils
|
|
3
|
+
* A number is a js-number, e.g. 5.12
|
|
4
|
+
*/
|
|
5
|
+
export declare const DEFAULT_TOLERANCE = 1e-9;
|
|
6
|
+
export declare const EPSILON: number;
|
|
7
|
+
export declare function is(x: any): boolean;
|
|
8
|
+
export declare function equal(x: number, y: number, tolerance?: number): boolean;
|
|
9
|
+
export declare function sign(x: number, tolerance?: number): number;
|
|
10
|
+
export declare function isInteger(num: number, tolerance?: number): boolean;
|
|
11
|
+
export declare function round(num: number, precision: number): number;
|
|
12
|
+
export declare function roundTo(num: number, increment: number): number;
|
|
13
|
+
export declare function floorTo(num: number, increment: number): number;
|
|
14
|
+
export declare function ceilTo(num: number, increment: number): number;
|
|
15
|
+
/**
|
|
16
|
+
* toFraction
|
|
17
|
+
*
|
|
18
|
+
* Returns a [numerator, denominator] array rational representation
|
|
19
|
+
* of `decimal`
|
|
20
|
+
*
|
|
21
|
+
* See http://en.wikipedia.org/wiki/Continued_fraction for implementation
|
|
22
|
+
* details
|
|
23
|
+
*
|
|
24
|
+
* toFraction(4/8) => [1, 2]
|
|
25
|
+
* toFraction(0.66) => [33, 50]
|
|
26
|
+
* toFraction(0.66, 0.01) => [2/3]
|
|
27
|
+
* toFraction(283 + 1/3) => [850, 3]
|
|
28
|
+
*/
|
|
29
|
+
export declare function toFraction(decimal: number, tolerance?: number, maxDenominator?: number): [number, number];
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flowtype definitions for data
|
|
3
|
+
* Generated by Flowgen from a Typescript Definition
|
|
4
|
+
* Flowgen v1.21.0
|
|
5
|
+
* @flow
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Number Utils
|
|
9
|
+
* A number is a js-number, e.g. 5.12
|
|
10
|
+
*/
|
|
11
|
+
declare export var DEFAULT_TOLERANCE: 1e-9;
|
|
12
|
+
declare export var EPSILON: number;
|
|
13
|
+
declare export function is(x: any): boolean;
|
|
14
|
+
declare export function equal(
|
|
15
|
+
x: number,
|
|
16
|
+
y: number,
|
|
17
|
+
tolerance?: number
|
|
18
|
+
): boolean;
|
|
19
|
+
declare export function sign(x: number, tolerance?: number): number;
|
|
20
|
+
declare export function isInteger(num: number, tolerance?: number): boolean;
|
|
21
|
+
declare export function round(num: number, precision: number): number;
|
|
22
|
+
declare export function roundTo(num: number, increment: number): number;
|
|
23
|
+
declare export function floorTo(num: number, increment: number): number;
|
|
24
|
+
declare export function ceilTo(num: number, increment: number): number;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* toFraction
|
|
28
|
+
*
|
|
29
|
+
* Returns a [numerator, denominator] array rational representation
|
|
30
|
+
* of `decimal`
|
|
31
|
+
*
|
|
32
|
+
* See http://en.wikipedia.org/wiki/Continued_fraction for implementation
|
|
33
|
+
* details
|
|
34
|
+
*
|
|
35
|
+
* toFraction(4/8) => [1, 2]
|
|
36
|
+
* toFraction(0.66) => [33, 50]
|
|
37
|
+
* toFraction(0.66, 0.01) => [2/3]
|
|
38
|
+
* toFraction(283 + 1/3) => [850, 3]
|
|
39
|
+
*/
|
|
40
|
+
declare export function toFraction(
|
|
41
|
+
decimal: number,
|
|
42
|
+
tolerance?: number,
|
|
43
|
+
maxDenominator?: number
|
|
44
|
+
): [number, number];
|
package/dist/point.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Point Utils
|
|
3
|
+
* A point is an array of two numbers e.g. [0, 0].
|
|
4
|
+
*/
|
|
5
|
+
import * as kvector from "./vector";
|
|
6
|
+
export type Point = ReadonlyArray<number>;
|
|
7
|
+
export declare function rotateRad(point: Point, theta: number, center?: Point): Point;
|
|
8
|
+
export declare function rotateDeg(point: Point, theta: number, center?: Point): Point;
|
|
9
|
+
export declare function distanceToPoint(point1: Point, point2: Point): number;
|
|
10
|
+
export declare function distanceToLine(point: Point, line: [Point, Point]): number;
|
|
11
|
+
export declare function reflectOverLine<P extends Point>(point: P, line: [P, P]): P;
|
|
12
|
+
/**
|
|
13
|
+
* Compares two points, returning -1, 0, or 1, for use with
|
|
14
|
+
* Array.prototype.sort
|
|
15
|
+
*
|
|
16
|
+
* Note: This technically doesn't satisfy the total-ordering
|
|
17
|
+
* requirements of Array.prototype.sort unless equalityTolerance
|
|
18
|
+
* is 0. In some cases very close points that compare within a
|
|
19
|
+
* few equalityTolerances could appear in the wrong order.
|
|
20
|
+
*/
|
|
21
|
+
export declare function compare(point1: Point, point2: Point, equalityTolerance?: number): number;
|
|
22
|
+
export declare const is: typeof kvector.is;
|
|
23
|
+
export declare const addVector: typeof kvector.add;
|
|
24
|
+
export declare const addVectors: typeof kvector.add;
|
|
25
|
+
export declare const subtractVector: typeof kvector.subtract;
|
|
26
|
+
export declare const equal: typeof kvector.equal;
|
|
27
|
+
export declare const polarRadFromCart: typeof kvector.polarRadFromCart;
|
|
28
|
+
export declare const polarDegFromCart: typeof kvector.polarDegFromCart;
|
|
29
|
+
export declare const cartFromPolarRad: typeof kvector.cartFromPolarRad;
|
|
30
|
+
export declare const cartFromPolarDeg: typeof kvector.cartFromPolarDeg;
|
|
31
|
+
export declare const round: typeof kvector.round;
|
|
32
|
+
export declare const roundTo: typeof kvector.roundTo;
|
|
33
|
+
export declare const floorTo: typeof kvector.floorTo;
|
|
34
|
+
export declare const ceilTo: typeof kvector.ceilTo;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flowtype definitions for data
|
|
3
|
+
* Generated by Flowgen from a Typescript Definition
|
|
4
|
+
* Flowgen v1.21.0
|
|
5
|
+
* @flow
|
|
6
|
+
*/
|
|
7
|
+
import * as kvector from "./vector";
|
|
8
|
+
export type Point = $ReadOnlyArray<number>;
|
|
9
|
+
declare export function rotateRad(
|
|
10
|
+
point: Point,
|
|
11
|
+
theta: number,
|
|
12
|
+
center?: Point
|
|
13
|
+
): Point;
|
|
14
|
+
declare export function rotateDeg(
|
|
15
|
+
point: Point,
|
|
16
|
+
theta: number,
|
|
17
|
+
center?: Point
|
|
18
|
+
): Point;
|
|
19
|
+
declare export function distanceToPoint(point1: Point, point2: Point): number;
|
|
20
|
+
declare export function distanceToLine(
|
|
21
|
+
point: Point,
|
|
22
|
+
line: [Point, Point]
|
|
23
|
+
): number;
|
|
24
|
+
declare export function reflectOverLine<P: Point>(point: P, line: [P, P]): P;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Compares two points, returning -1, 0, or 1, for use with
|
|
28
|
+
* Array.prototype.sort
|
|
29
|
+
*
|
|
30
|
+
* Note: This technically doesn't satisfy the total-ordering
|
|
31
|
+
* requirements of Array.prototype.sort unless equalityTolerance
|
|
32
|
+
* is 0. In some cases very close points that compare within a
|
|
33
|
+
* few equalityTolerances could appear in the wrong order.
|
|
34
|
+
*/
|
|
35
|
+
declare export function compare(
|
|
36
|
+
point1: Point,
|
|
37
|
+
point2: Point,
|
|
38
|
+
equalityTolerance?: number
|
|
39
|
+
): number;
|
|
40
|
+
declare export var is: typeof kvector.is;
|
|
41
|
+
declare export var addVector: typeof kvector.add;
|
|
42
|
+
declare export var addVectors: typeof kvector.add;
|
|
43
|
+
declare export var subtractVector: typeof kvector.subtract;
|
|
44
|
+
declare export var equal: typeof kvector.equal;
|
|
45
|
+
declare export var polarRadFromCart: typeof kvector.polarRadFromCart;
|
|
46
|
+
declare export var polarDegFromCart: typeof kvector.polarDegFromCart;
|
|
47
|
+
declare export var cartFromPolarRad: typeof kvector.cartFromPolarRad;
|
|
48
|
+
declare export var cartFromPolarDeg: typeof kvector.cartFromPolarDeg;
|
|
49
|
+
declare export var round: typeof kvector.round;
|
|
50
|
+
declare export var roundTo: typeof kvector.roundTo;
|
|
51
|
+
declare export var floorTo: typeof kvector.floorTo;
|
|
52
|
+
declare export var ceilTo: typeof kvector.ceilTo;
|
package/dist/ray.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ray Utils
|
|
3
|
+
* A ray (→) is an array of an endpoint and another point along the ray.
|
|
4
|
+
* For example, [[0, 0], [1, 0]] is the ray starting at the origin and
|
|
5
|
+
* traveling along the positive x-axis.
|
|
6
|
+
*/
|
|
7
|
+
import type { Point } from "./point";
|
|
8
|
+
export type Ray = [Point, Point];
|
|
9
|
+
export declare function equal(ray1: Ray, ray2: Ray, tolerance?: number): boolean;
|
package/dist/ray.js.flow
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flowtype definitions for data
|
|
3
|
+
* Generated by Flowgen from a Typescript Definition
|
|
4
|
+
* Flowgen v1.21.0
|
|
5
|
+
* @flow
|
|
6
|
+
*/
|
|
7
|
+
import type { Point } from "./point";
|
|
8
|
+
export type Ray = [Point, Point];
|
|
9
|
+
declare export function equal(
|
|
10
|
+
ray1: Ray,
|
|
11
|
+
ray2: Ray,
|
|
12
|
+
tolerance?: number
|
|
13
|
+
): boolean;
|
package/dist/vector.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vector Utils
|
|
3
|
+
* A vector is an array of numbers e.g. [0, 3, 4].
|
|
4
|
+
*/
|
|
5
|
+
type Vector = ReadonlyArray<number>;
|
|
6
|
+
/**
|
|
7
|
+
* Checks if the given vector contains only numbers and, optionally, is of the
|
|
8
|
+
* right dimension (length).
|
|
9
|
+
*
|
|
10
|
+
* is([1, 2, 3]) -> true
|
|
11
|
+
* is([1, "Hello", 3]) -> false
|
|
12
|
+
* is([1, 2, 3], 1) -> false
|
|
13
|
+
*/
|
|
14
|
+
export declare function is<T>(vec: ReadonlyArray<T>, dimension?: number): boolean;
|
|
15
|
+
export declare function normalize<V extends Vector>(v: V): V;
|
|
16
|
+
export declare function length(v: Vector): number;
|
|
17
|
+
export declare function dot(a: Vector, b: Vector): number;
|
|
18
|
+
export declare function add<V extends Vector>(...vecs: ReadonlyArray<V>): V;
|
|
19
|
+
export declare function subtract<V extends Vector>(v1: V, v2: V): V;
|
|
20
|
+
export declare function negate<V extends Vector>(v: V): V;
|
|
21
|
+
export declare function scale<V extends Vector>(v1: V, scalar: number): V;
|
|
22
|
+
export declare function equal(v1: Vector, v2: Vector, tolerance?: number): boolean;
|
|
23
|
+
export declare function codirectional(v1: Vector, v2: Vector, tolerance?: number): boolean;
|
|
24
|
+
export declare function collinear(v1: Vector, v2: Vector, tolerance?: number): boolean;
|
|
25
|
+
export declare function polarRadFromCart(v: ReadonlyArray<number>): ReadonlyArray<number>;
|
|
26
|
+
export declare function polarDegFromCart(v: ReadonlyArray<number>): ReadonlyArray<number>;
|
|
27
|
+
export declare function cartFromPolarRad(radius: number, theta?: number): ReadonlyArray<number>;
|
|
28
|
+
export declare function cartFromPolarDeg(radius: number, theta?: number): ReadonlyArray<number>;
|
|
29
|
+
export declare function rotateRad(v: ReadonlyArray<number>, theta: number): ReadonlyArray<number>;
|
|
30
|
+
export declare function rotateDeg(v: ReadonlyArray<number>, theta: number): ReadonlyArray<number>;
|
|
31
|
+
export declare function angleRad(v1: Vector, v2: Vector): number;
|
|
32
|
+
export declare function angleDeg(v1: Vector, v2: Vector): number;
|
|
33
|
+
export declare function projection<V extends Vector>(v1: V, v2: V): V;
|
|
34
|
+
export declare function round<V extends Vector>(vec: V, precision: V | number): V;
|
|
35
|
+
export declare function roundTo<V extends Vector>(vec: V, increment: V | number): V;
|
|
36
|
+
export declare function floorTo<V extends Vector>(vec: V, increment: V | number): V;
|
|
37
|
+
export declare function ceilTo<V extends Vector>(vec: V, increment: V | number): V;
|
|
38
|
+
export {};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flowtype definitions for data
|
|
3
|
+
* Generated by Flowgen from a Typescript Definition
|
|
4
|
+
* Flowgen v1.21.0
|
|
5
|
+
* @flow
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Vector Utils
|
|
9
|
+
* A vector is an array of numbers e.g. [0, 3, 4].
|
|
10
|
+
*/
|
|
11
|
+
declare type Vector = $ReadOnlyArray<number>;
|
|
12
|
+
/**
|
|
13
|
+
* Checks if the given vector contains only numbers and, optionally, is of the
|
|
14
|
+
* right dimension (length).
|
|
15
|
+
*
|
|
16
|
+
* is([1, 2, 3]) -> true
|
|
17
|
+
* is([1, "Hello", 3]) -> false
|
|
18
|
+
* is([1, 2, 3], 1) -> false
|
|
19
|
+
*/
|
|
20
|
+
declare export function is<T>(
|
|
21
|
+
vec: $ReadOnlyArray<T>,
|
|
22
|
+
dimension?: number
|
|
23
|
+
): boolean;
|
|
24
|
+
declare export function normalize<V: Vector>(v: V): V;
|
|
25
|
+
declare export function length(v: Vector): number;
|
|
26
|
+
declare export function dot(a: Vector, b: Vector): number;
|
|
27
|
+
declare export function add<V: Vector>(...vecs: $ReadOnlyArray<V>): V;
|
|
28
|
+
declare export function subtract<V: Vector>(v1: V, v2: V): V;
|
|
29
|
+
declare export function negate<V: Vector>(v: V): V;
|
|
30
|
+
declare export function scale<V: Vector>(v1: V, scalar: number): V;
|
|
31
|
+
declare export function equal(
|
|
32
|
+
v1: Vector,
|
|
33
|
+
v2: Vector,
|
|
34
|
+
tolerance?: number
|
|
35
|
+
): boolean;
|
|
36
|
+
declare export function codirectional(
|
|
37
|
+
v1: Vector,
|
|
38
|
+
v2: Vector,
|
|
39
|
+
tolerance?: number
|
|
40
|
+
): boolean;
|
|
41
|
+
declare export function collinear(
|
|
42
|
+
v1: Vector,
|
|
43
|
+
v2: Vector,
|
|
44
|
+
tolerance?: number
|
|
45
|
+
): boolean;
|
|
46
|
+
declare export function polarRadFromCart(
|
|
47
|
+
v: $ReadOnlyArray<number>
|
|
48
|
+
): $ReadOnlyArray<number>;
|
|
49
|
+
declare export function polarDegFromCart(
|
|
50
|
+
v: $ReadOnlyArray<number>
|
|
51
|
+
): $ReadOnlyArray<number>;
|
|
52
|
+
declare export function cartFromPolarRad(
|
|
53
|
+
radius: number,
|
|
54
|
+
theta?: number
|
|
55
|
+
): $ReadOnlyArray<number>;
|
|
56
|
+
declare export function cartFromPolarDeg(
|
|
57
|
+
radius: number,
|
|
58
|
+
theta?: number
|
|
59
|
+
): $ReadOnlyArray<number>;
|
|
60
|
+
declare export function rotateRad(
|
|
61
|
+
v: $ReadOnlyArray<number>,
|
|
62
|
+
theta: number
|
|
63
|
+
): $ReadOnlyArray<number>;
|
|
64
|
+
declare export function rotateDeg(
|
|
65
|
+
v: $ReadOnlyArray<number>,
|
|
66
|
+
theta: number
|
|
67
|
+
): $ReadOnlyArray<number>;
|
|
68
|
+
declare export function angleRad(v1: Vector, v2: Vector): number;
|
|
69
|
+
declare export function angleDeg(v1: Vector, v2: Vector): number;
|
|
70
|
+
declare export function projection<V: Vector>(v1: V, v2: V): V;
|
|
71
|
+
declare export function round<V: Vector>(vec: V, precision: V | number): V;
|
|
72
|
+
declare export function roundTo<V: Vector>(vec: V, increment: V | number): V;
|
|
73
|
+
declare export function floorTo<V: Vector>(vec: V, increment: V | number): V;
|
|
74
|
+
declare export function ceilTo<V: Vector>(vec: V, increment: V | number): V;
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "Khan Academy's Javascript Numeric Math Utilities",
|
|
4
4
|
"author": "Khan Academy",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"version": "0.0
|
|
6
|
+
"version": "0.1.0",
|
|
7
7
|
"publishConfig": {
|
|
8
8
|
"access": "public"
|
|
9
9
|
},
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
},
|
|
17
17
|
"module": "dist/es/index.js",
|
|
18
18
|
"main": "dist/index.js",
|
|
19
|
-
"source": "src/index.
|
|
19
|
+
"source": "src/index.ts",
|
|
20
20
|
"scripts": {
|
|
21
21
|
"test": "bash -c 'yarn --silent --cwd \"../..\" test ${@:0} $($([[ ${@: -1} = -* ]] || [[ ${@: -1} = bash ]]) && echo $PWD)'"
|
|
22
22
|
},
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
import {describe, it} from "@jest/globals";
|
|
2
2
|
|
|
3
|
-
import * as number from "../number
|
|
3
|
+
import * as number from "../number";
|
|
4
4
|
|
|
5
5
|
describe("knumber", function () {
|
|
6
|
-
it.each([3, Math.PI, 6.28, 5e10, 1 / 0])("is a number: %s", (num) => {
|
|
6
|
+
it.each([3, Math.PI, 6.28, 5e10, 1 / 0])("is a number: %s", (num: any) => {
|
|
7
7
|
expect(number.is(num)).toBe(true);
|
|
8
8
|
});
|
|
9
9
|
|
|
10
|
-
it.each(["10", 0 / 0, NaN])("is not a number:%s", (num) => {
|
|
10
|
+
it.each(["10", 0 / 0, NaN])("is not a number:%s", (num: any) => {
|
|
11
11
|
expect(number.is(num)).toBe(false);
|
|
12
12
|
});
|
|
13
13
|
|
package/src/index.ts
ADDED
package/src/{line.js → line.ts}
RENAMED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
/**
|
|
3
2
|
* Line Utils
|
|
4
3
|
* A line is an array of two points e.g. [[-5, 0], [5, 0]].
|
|
5
4
|
*/
|
|
6
5
|
|
|
7
|
-
import * as kpoint from "./point
|
|
8
|
-
import * as kvector from "./vector
|
|
6
|
+
import * as kpoint from "./point";
|
|
7
|
+
import * as kvector from "./vector";
|
|
9
8
|
|
|
10
|
-
import type {Point} from "./point
|
|
9
|
+
import type {Point} from "./point";
|
|
11
10
|
|
|
12
11
|
export type Line = [Point, Point];
|
|
13
12
|
|
package/src/{logo.js → logo.ts}
RENAMED
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
/* eslint-disable import/no-default-export */
|
|
2
|
-
// @flow
|
|
3
2
|
// This file describes the graphie source code of the kmath logo
|
|
4
3
|
// currently used on khan.github.io.
|
|
5
4
|
//
|
|
6
5
|
// Also located at http://ka-perseus-graphie.s3.amazonaws.com/42ef3cbadc3e6464124533191728c3c5c55c7355.svg
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
declare
|
|
10
|
-
|
|
11
|
-
declare var ellipse: $FlowFixMe;
|
|
12
|
-
// eslint-disable-next-line ft-flow/no-types-missing-file-annotation
|
|
13
|
-
declare var line: $FlowFixMe;
|
|
7
|
+
declare let init: any;
|
|
8
|
+
declare let ellipse: any;
|
|
9
|
+
declare let line: any;
|
|
14
10
|
|
|
15
11
|
const GREEN = "#28AE7B";
|
|
16
12
|
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
/**
|
|
3
2
|
* Number Utils
|
|
4
3
|
* A number is a js-number, e.g. 5.12
|
|
@@ -6,7 +5,7 @@
|
|
|
6
5
|
|
|
7
6
|
import _ from "underscore";
|
|
8
7
|
|
|
9
|
-
export const DEFAULT_TOLERANCE
|
|
8
|
+
export const DEFAULT_TOLERANCE = 1e-9;
|
|
10
9
|
|
|
11
10
|
// TODO: Should this just be Number.Epsilon
|
|
12
11
|
export const EPSILON: number = Math.pow(2, -42);
|
|
@@ -78,8 +77,9 @@ export function ceilTo(num: number, increment: number): number {
|
|
|
78
77
|
*/
|
|
79
78
|
export function toFraction(
|
|
80
79
|
decimal: number,
|
|
81
|
-
|
|
82
|
-
|
|
80
|
+
// can't be 0
|
|
81
|
+
tolerance: number = EPSILON,
|
|
82
|
+
maxDenominator = 1000,
|
|
83
83
|
): [number, number] {
|
|
84
84
|
// Initialize everything to compute successive terms of
|
|
85
85
|
// continued-fraction approximations via recurrence relation
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
/**
|
|
3
2
|
* Point Utils
|
|
4
3
|
* A point is an array of two numbers e.g. [0, 0].
|
|
@@ -6,11 +5,11 @@
|
|
|
6
5
|
|
|
7
6
|
import _ from "underscore";
|
|
8
7
|
|
|
9
|
-
import * as knumber from "./number
|
|
10
|
-
import * as kvector from "./vector
|
|
8
|
+
import * as knumber from "./number";
|
|
9
|
+
import * as kvector from "./vector";
|
|
11
10
|
|
|
12
11
|
// A point, in 2D, 3D, or nD space.
|
|
13
|
-
export type Point =
|
|
12
|
+
export type Point = ReadonlyArray<number>;
|
|
14
13
|
|
|
15
14
|
// Rotate point (around origin unless a center is specified)
|
|
16
15
|
export function rotateRad(point: Point, theta: number, center?: Point): Point {
|
|
@@ -50,7 +49,7 @@ export function distanceToLine(point: Point, line: [Point, Point]): number {
|
|
|
50
49
|
}
|
|
51
50
|
|
|
52
51
|
// Reflect point over line
|
|
53
|
-
export function reflectOverLine<P
|
|
52
|
+
export function reflectOverLine<P extends Point>(point: P, line: [P, P]): P {
|
|
54
53
|
const lv = kvector.subtract(line[1], line[0]);
|
|
55
54
|
const pv = kvector.subtract(point, line[0]);
|
|
56
55
|
const projectedPv = kvector.projection(pv, lv);
|
package/src/{ray.js → ray.ts}
RENAMED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
/**
|
|
3
2
|
* Ray Utils
|
|
4
3
|
* A ray (→) is an array of an endpoint and another point along the ray.
|
|
@@ -6,8 +5,8 @@
|
|
|
6
5
|
* traveling along the positive x-axis.
|
|
7
6
|
*/
|
|
8
7
|
|
|
9
|
-
import * as kpoint from "./point
|
|
10
|
-
import * as kvector from "./vector
|
|
8
|
+
import * as kpoint from "./point";
|
|
9
|
+
import * as kvector from "./vector";
|
|
11
10
|
|
|
12
11
|
import type {Point} from "./point";
|
|
13
12
|
|