@khanacademy/kmath 0.1.11 → 0.1.13

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/README.md CHANGED
@@ -1,7 +1,6 @@
1
- <h1 align="center">
2
- <img alt="kmath" src="logo.svg" width="50" /> <br />
3
- kmath - Javascript Numeric Math Utilities
4
- </h1>
1
+ # @khanacademy/kmath
2
+
3
+ Javascript Numeric Math Utilities
5
4
 
6
5
  ## Overview
7
6
 
@@ -34,7 +33,7 @@ pure and allocates a new array for the return value.
34
33
  ## Getting started
35
34
 
36
35
  After cloning or downloading kmath, you can install it by running
37
- `npm install` or `make install`.
36
+ `yarn` or `make install`.
38
37
 
39
38
  To play around with the available interfaces, you can load kmath
40
39
  into a Node repl:
package/dist/es/index.js CHANGED
@@ -3,7 +3,7 @@ import _ from 'underscore';
3
3
 
4
4
  // This file is processed by a Rollup plugin (replace) to inject the production
5
5
  const libName = "@khanacademy/kmath";
6
- const libVersion = "0.1.11";
6
+ const libVersion = "0.1.13";
7
7
  addLibraryVersionToPerseusDebug(libName, libVersion);
8
8
 
9
9
  /**
package/dist/index.js CHANGED
@@ -11,7 +11,7 @@ var ___default = /*#__PURE__*/_interopDefaultLegacy(_);
11
11
 
12
12
  // This file is processed by a Rollup plugin (replace) to inject the production
13
13
  const libName = "@khanacademy/kmath";
14
- const libVersion = "0.1.11";
14
+ const libVersion = "0.1.13";
15
15
  perseusCore.addLibraryVersionToPerseusDebug(libName, libVersion);
16
16
 
17
17
  /**
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.1.11",
6
+ "version": "0.1.13",
7
7
  "publishConfig": {
8
8
  "access": "public"
9
9
  },
@@ -17,14 +17,17 @@
17
17
  "module": "dist/es/index.js",
18
18
  "main": "dist/index.js",
19
19
  "source": "src/index.ts",
20
+ "files": [
21
+ "dist"
22
+ ],
20
23
  "scripts": {
21
24
  "test": "bash -c 'yarn --silent --cwd \"../..\" test ${@:0} $($([[ ${@: -1} = -* ]] || [[ ${@: -1} = bash ]]) && echo $PWD)'"
22
25
  },
23
26
  "dependencies": {
24
- "@khanacademy/perseus-core": "1.4.1"
27
+ "@khanacademy/perseus-core": "1.5.0"
25
28
  },
26
29
  "devDependencies": {
27
- "perseus-build-settings": "^0.3.0",
30
+ "perseus-build-settings": "^0.4.1",
28
31
  "underscore": "1.4.4"
29
32
  },
30
33
  "peerDependencies": {
package/.babelrc.js DELETED
@@ -1,8 +0,0 @@
1
- /**
2
- * HACK(somewhatabstract): Due to https://github.com/facebook/jest/issues/11741,
3
- * we need to have this file, or updating inline snapshots can fail rather
4
- * cryptically.
5
- *
6
- * We should remove this when jest is fixed.
7
- */
8
- module.exports = require("../../config/build/babel.config");
package/.eslintrc.js DELETED
@@ -1,12 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-var-requires */
2
- /* eslint-disable import/no-commonjs */
3
- const path = require("path");
4
-
5
- module.exports = {
6
- rules: {
7
- "import/no-extraneous-dependencies": [
8
- "error",
9
- {packageDir: [__dirname, path.join(__dirname, "../../")]},
10
- ],
11
- },
12
- };
package/CHANGELOG.md DELETED
@@ -1,112 +0,0 @@
1
- # @khanacademy/kmath
2
-
3
- ## 0.1.11
4
-
5
- ### Patch Changes
6
-
7
- - [#964](https://github.com/Khan/perseus/pull/964) [`d8fbc251`](https://github.com/Khan/perseus/commit/d8fbc25170bd671ad984893553f79f44e3a0d048) Thanks [@benchristel](https://github.com/benchristel)! - Internal: Refactor Movable to be an ES6 class
8
-
9
- ## 0.1.10
10
-
11
- ### Patch Changes
12
-
13
- - [#954](https://github.com/Khan/perseus/pull/954) [`43820ff9`](https://github.com/Khan/perseus/commit/43820ff9ce391682b951279a4c6fb9e3db966eb9) Thanks [@benchristel](https://github.com/benchristel)! - Internal: Fix @ts-expect-errors related to Graphie
14
-
15
- ## 0.1.9
16
-
17
- ### Patch Changes
18
-
19
- - [#949](https://github.com/Khan/perseus/pull/949) [`59ce1e09`](https://github.com/Khan/perseus/commit/59ce1e0990cecd1123d7b9a671b032fcd03ce1b1) Thanks [@benchristel](https://github.com/benchristel)! - Internal: use built-in JS functions instead of underscore
20
-
21
- ## 0.1.8
22
-
23
- ### Patch Changes
24
-
25
- - Updated dependencies [[`1f4e17ba`](https://github.com/Khan/perseus/commit/1f4e17ba77e1491523813655af18a70285a25989), [`8857950b`](https://github.com/Khan/perseus/commit/8857950bdeeb6e13bc3766b1c6545289b21cbe2a)]:
26
- - @khanacademy/perseus-core@1.4.1
27
-
28
- ## 0.1.7
29
-
30
- ### Patch Changes
31
-
32
- - [#814](https://github.com/Khan/perseus/pull/814) [`105d2060`](https://github.com/Khan/perseus/commit/105d20603d935d35cff237b17f0bfb57ca751e4c) Thanks [@jeremywiebe](https://github.com/jeremywiebe)! - Minor build change to how we provide Typescript type definitions (should be no change to build output).
33
-
34
- - Updated dependencies [[`a91c84fe`](https://github.com/Khan/perseus/commit/a91c84fe53827ff4333220777a9918882b7fe9f0), [`105d2060`](https://github.com/Khan/perseus/commit/105d20603d935d35cff237b17f0bfb57ca751e4c)]:
35
- - @khanacademy/perseus-core@1.4.0
36
-
37
- ## 0.1.6
38
-
39
- ### Patch Changes
40
-
41
- - Updated dependencies [[`79403e06`](https://github.com/Khan/perseus/commit/79403e06eedb597d7818d6c858bbba6f51ff3fe1)]:
42
- - @khanacademy/perseus-core@1.3.0
43
-
44
- ## 0.1.5
45
-
46
- ### Patch Changes
47
-
48
- - Updated dependencies [[`376eb0e4`](https://github.com/Khan/perseus/commit/376eb0e4aaaa4c7a90fd6107a84bb74d382b077c)]:
49
- - @khanacademy/perseus-core@1.2.0
50
-
51
- ## 0.1.4
52
-
53
- ### Patch Changes
54
-
55
- - Updated dependencies [22a9c408]
56
- - @khanacademy/perseus-core@1.1.2
57
-
58
- ## 0.1.3
59
-
60
- ### Patch Changes
61
-
62
- - 55d4cd00: Print package name and version when loaded in the page
63
- - Updated dependencies [55d4cd00]
64
- - @khanacademy/perseus-core@1.1.1
65
-
66
- ## 0.1.2
67
-
68
- ### Patch Changes
69
-
70
- - ce5e6297: Upgrade wonder-blocks deps to package versions without Flow types
71
-
72
- ## 0.1.1
73
-
74
- ### Patch Changes
75
-
76
- - 1f062e98: Bump all package versions since the build settings have been updated
77
-
78
- ## 0.1.0
79
-
80
- ### Minor Changes
81
-
82
- - 53fd3768: Migrate source code to TypeScript
83
-
84
- ## 0.0.8
85
-
86
- ### Patch Changes
87
-
88
- - f567f660: Update the eslint config to look at both the package.json for the package and the one from the root
89
-
90
- ## 0.0.7
91
-
92
- ### Patch Changes
93
-
94
- - bf180fe1: Fix our use of import/no-extraneous-dependencies
95
-
96
- ## 0.0.6
97
-
98
- ### Patch Changes
99
-
100
- - 98d283ff: Fix storybook
101
-
102
- ## 0.0.5
103
-
104
- ### Patch Changes
105
-
106
- - 1c1f7717: Enhance Flow types for kmath's vector and point
107
-
108
- ## 0.0.4
109
-
110
- ### Patch Changes
111
-
112
- - a46d62bf: Remove .tgz file that shouldn't be packaged
package/logo.svg DELETED
@@ -1 +0,0 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="400" height="400" viewBox="0 0 400 400"><defs/><ellipse cx="200" cy="200" rx="200" ry="200" fill="#28ae7b" stroke="none" style="stroke-width: 2px" stroke-width="2"/><path fill="#ffffff" stroke="#ffffff" d="M 293.05,242.4 C 295.7,226.5 324.85,202.65 332.8,200 C 324.85,197.35 295.7,173.5 293.05,157.6" transform="" style="stroke-width: 25px;stroke-linejoin: round;stroke-linecap: round" stroke-width="25" stroke-linejoin="round" stroke-linecap="round"/><path fill="#ffffff" stroke="#ffffff" d="M 80,200 S 80,200 332.05,200" style="stroke-width: 25px" stroke-width="25"/><path fill="#ffffff" stroke="#ffffff" d="M 200,320 L 200,80" style="stroke-width: 25px" stroke-width="25"/></svg>
@@ -1,117 +0,0 @@
1
- import * as line from "../line";
2
-
3
- describe("kline", function () {
4
- it("finds distance to point", () => {
5
- const result = line.distanceToPoint(
6
- [
7
- [-5, 0],
8
- [5, 0],
9
- ],
10
- [0, 5],
11
- );
12
- expect(result).toBe(5);
13
- });
14
-
15
- it("reflects a point", () => {
16
- const result = line.reflectPoint(
17
- [
18
- [-5, -5],
19
- [5, 5],
20
- ],
21
- [0, 5],
22
- );
23
- expect(result).toEqual([5, 0]);
24
- });
25
-
26
- it("finds the midpoint", () => {
27
- const result = line.midpoint([
28
- [-5, -5],
29
- [5, 5],
30
- ]);
31
- expect(result).toEqual([0, 0]);
32
- });
33
-
34
- it("two identical lines should be equal", function () {
35
- const result = line.equal(
36
- [
37
- [1, 1],
38
- [3, 3],
39
- ],
40
- [
41
- [1, 1],
42
- [3, 3],
43
- ],
44
- );
45
- expect(result).toBe(true);
46
- });
47
-
48
- it("two parallel lines should not be equal", function () {
49
- const result = line.equal(
50
- [
51
- [1, 1],
52
- [3, 3],
53
- ],
54
- [
55
- [1, 2],
56
- [3, 4],
57
- ],
58
- );
59
- expect(result).toBe(false);
60
- });
61
-
62
- it("two intersecting lines should not be equal", function () {
63
- const result = line.equal(
64
- [
65
- [1, 1],
66
- [3, 3],
67
- ],
68
- [
69
- [1, 1],
70
- [3, 4],
71
- ],
72
- );
73
- expect(result).toBe(false);
74
- });
75
-
76
- it("two collinear lines should be equal #1", function () {
77
- const result = line.equal(
78
- [
79
- [1, 1],
80
- [3, 3],
81
- ],
82
- [
83
- [0, 0],
84
- [5, 5],
85
- ],
86
- );
87
- expect(result).toBe(true);
88
- });
89
-
90
- it("two collinear lines should be equal #2", function () {
91
- const result = line.equal(
92
- [
93
- [4, 4],
94
- [5, 5],
95
- ],
96
- [
97
- [0, 0],
98
- [1, 1],
99
- ],
100
- );
101
- expect(result).toBe(true);
102
- });
103
-
104
- it("two collinear lines should be equal #3", function () {
105
- const result = line.equal(
106
- [
107
- [0, 0],
108
- [1, 1],
109
- ],
110
- [
111
- [3, 3],
112
- [6, 6],
113
- ],
114
- );
115
- expect(result).toBe(true);
116
- });
117
- });
@@ -1,119 +0,0 @@
1
- import {describe, it} from "@jest/globals";
2
-
3
- import * as number from "../number";
4
-
5
- describe("knumber", function () {
6
- it.each([3, Math.PI, 6.28, 5e10, 1 / 0])("is a number: %s", (num: any) => {
7
- expect(number.is(num)).toBe(true);
8
- });
9
-
10
- it.each(["10", 0 / 0, NaN])("is not a number:%s", (num: any) => {
11
- expect(number.is(num)).toBe(false);
12
- });
13
-
14
- it("two equal numbers should be equal", function () {
15
- const result = number.equal(1 / 3, (1 / 90) * 30);
16
- expect(result).toBe(true);
17
- });
18
-
19
- it("two different numbers should not be equal", function () {
20
- const result = number.equal(1 / 3, 1.333333);
21
- expect(result).toBe(false);
22
- });
23
-
24
- it("Infinity should equal Infinity", function () {
25
- const result = number.equal(
26
- Number.POSITIVE_INFINITY,
27
- Number.POSITIVE_INFINITY,
28
- );
29
- expect(result).toBe(true);
30
- });
31
-
32
- it("+Infinity should not equal -Infinity", function () {
33
- const result = number.equal(
34
- Number.POSITIVE_INFINITY,
35
- Number.NEGATIVE_INFINITY,
36
- );
37
- expect(result).toBe(false);
38
- });
39
-
40
- it("sign(0) should be 0", function () {
41
- expect(number.sign(0)).toBe(0);
42
- });
43
-
44
- it("sign(-0.0) should be 0", function () {
45
- expect(number.sign(-0.0)).toBe(0);
46
- });
47
-
48
- it("sign(3.2) should be 1", function () {
49
- expect(number.sign(3.2)).toBe(1);
50
- });
51
-
52
- it("sign(-2.8) should be -1", function () {
53
- expect(number.sign(-2.8)).toBe(-1);
54
- });
55
-
56
- it("isInteger(-2.8) should be false", function () {
57
- expect(number.isInteger(-2.8)).toBe(false);
58
- });
59
-
60
- it("isInteger(-2) should be true", function () {
61
- expect(number.isInteger(-2)).toBe(true);
62
- });
63
-
64
- it("isInteger(10.0) should be true", () => {
65
- expect(number.isInteger(10)).toBe(true);
66
- });
67
-
68
- it("rounds to correct precision", () => {
69
- expect(number.round(0.06793, 4)).toBe(0.0679);
70
- expect(number.round(0.06793, 3)).toBe(0.068);
71
- });
72
-
73
- it("rounds to correct interval", () => {
74
- expect(number.roundTo(83, 5)).toBe(85);
75
- expect(number.roundTo(2.3, 0.5)).toBe(2.5);
76
- });
77
-
78
- it("floors to the correct interval", () => {
79
- expect(number.floorTo(83, 5)).toBe(80);
80
- expect(number.floorTo(2.3, 0.5)).toBe(2);
81
- });
82
-
83
- it("ceils to the correct interval", () => {
84
- expect(number.ceilTo(81, 5)).toBe(85);
85
- expect(number.ceilTo(2.1, 0.5)).toBe(2.5);
86
- });
87
-
88
- it("toFraction(-2) should be -2/1", function () {
89
- expect(number.toFraction(-2)).toStrictEqual([-2, 1]);
90
- });
91
-
92
- it("toFraction(-2.5) should be -5/2", function () {
93
- expect(number.toFraction(-2.5)).toStrictEqual([-5, 2]);
94
- });
95
-
96
- it("toFraction(2/3) should be 2/3", function () {
97
- expect(number.toFraction(2 / 3)).toStrictEqual([2, 3]);
98
- });
99
-
100
- it("toFraction(283.33...) should be 850/3", function () {
101
- expect(number.toFraction(283 + 1 / 3)).toStrictEqual([850, 3]);
102
- });
103
-
104
- it("toFraction(0) should be 0/1", function () {
105
- expect(number.toFraction(0)).toStrictEqual([0, 1]);
106
- });
107
-
108
- it("toFraction(pi) should be pi/1", function () {
109
- expect(number.toFraction(Math.PI)).toStrictEqual([Math.PI, 1]);
110
- });
111
-
112
- it("toFraction(0.66) should be 33/50", function () {
113
- expect(number.toFraction(0.66)).toStrictEqual([33, 50]);
114
- });
115
-
116
- it("toFraction(0.66, 0.01) should be 2/3", function () {
117
- expect(number.toFraction(0.66, 0.01)).toStrictEqual([2, 3]);
118
- });
119
- });
@@ -1,48 +0,0 @@
1
- import * as point from "../point";
2
-
3
- describe("kpoint", function () {
4
- it("point.compare should return positive if the first element is larger", function () {
5
- const result = point.compare([5, 2], [3, 4]);
6
- expect(result).toBeGreaterThan(0);
7
- });
8
-
9
- it("point.compare should return negative if the first element is smaller", function () {
10
- const result = point.compare([2, 2], [4, 0]);
11
- expect(result).toBeLessThan(0);
12
- });
13
-
14
- it("point.compare should return positive if the second element is larger", function () {
15
- const result = point.compare([5, 2], [5, 1]);
16
- expect(result).toBeGreaterThan(0);
17
- });
18
-
19
- it("point.compare should return negative if the second element is smaller", function () {
20
- const result = point.compare([2, 2], [2, 4]);
21
- expect(result).toBeLessThan(0);
22
- });
23
-
24
- it("point.compare should return positive if the third element is larger", function () {
25
- const result = point.compare([5, 3, -2], [5, 3, -4]);
26
- expect(result).toBeGreaterThan(0);
27
- });
28
-
29
- it("point.compare should return negative if the third element is smaller", function () {
30
- const result = point.compare([2, -1, -4], [2, -1, -2]);
31
- expect(result).toBeLessThan(0);
32
- });
33
-
34
- it("point.compare should return 0 if the vectors are equal", function () {
35
- const result = point.compare([2, 4, 3], [2, 4, 3]);
36
- expect(result).toBe(0);
37
- });
38
-
39
- it("point.compare should return negative if v1 is shorter than v2", function () {
40
- const result = point.compare([2, 4], [2, 4, 3]);
41
- expect(result).toBeLessThan(0);
42
- });
43
-
44
- it("point.compare should return positive if v1 is longer than v2", function () {
45
- const result = point.compare([2, 4, -2], [2, 2]);
46
- expect(result).toBeGreaterThan(0);
47
- });
48
- });
@@ -1,145 +0,0 @@
1
- import * as vector from "../vector";
2
-
3
- describe("kvector", function () {
4
- it("vector.zip should return empty given empty", function () {
5
- expect(vector.zip([], [])).toEqual([]);
6
- });
7
-
8
- it("vector.zip should create vectors from x and y coords", function () {
9
- expect(vector.zip([1, 2], [3, 4])).toEqual([
10
- [1, 3],
11
- [2, 4],
12
- ]);
13
- });
14
-
15
- it("vector.zip should ignore extra xs", function () {
16
- expect(vector.zip([1, 2, 9], [3, 4])).toEqual([
17
- [1, 3],
18
- [2, 4],
19
- ]);
20
- });
21
-
22
- it("vector.zip should ignore extra ys", function () {
23
- expect(vector.zip([1, 2], [3, 4, 9])).toEqual([
24
- [1, 3],
25
- [2, 4],
26
- ]);
27
- });
28
-
29
- it("vector.map should transform a 2D vector", function () {
30
- const double = (x) => x * 2;
31
- expect(vector.map([3, 4], double)).toEqual([6, 8]);
32
- });
33
-
34
- it("vector.map should pass the index of each element to the callback", function () {
35
- expect(vector.map([3, 4], (_, i) => i)).toEqual([0, 1]);
36
- });
37
-
38
- it("vector.add should add two 2D vectors", function () {
39
- const result = vector.add([1, 2], [3, 4]);
40
- expect(result).toStrictEqual([4, 6]);
41
- });
42
-
43
- it("vector.add should add two 3D vectors", function () {
44
- const result = vector.add([1, 2, 3], [4, 5, 6]);
45
- expect(result).toStrictEqual([5, 7, 9]);
46
- });
47
-
48
- it("vector.add should add three 2D vectors", function () {
49
- const result = vector.add([1, 2], [3, 4], [5, 6]);
50
- expect(result).toStrictEqual([9, 12]);
51
- });
52
-
53
- it("vector.subtract should subtract two 2D vectors", function () {
54
- const result = vector.subtract([1, 2], [3, 4]);
55
- expect(result).toStrictEqual([-2, -2]);
56
- });
57
-
58
- it("vector.subtract should subtract two 3D vectors", function () {
59
- const result = vector.subtract([1, 2, 3], [4, 5, 6]);
60
- expect(result).toStrictEqual([-3, -3, -3]);
61
- });
62
-
63
- it("vector.dot should take the dot product of 2 2D vectors", function () {
64
- const result = vector.dot([1, 2], [3, 4]);
65
- expect(result).toBe(3 + 8);
66
- });
67
-
68
- it("vector.dot should take the dot product of 2 3D vectors", function () {
69
- const result = vector.dot([1, 2, 3], [4, 5, 6]);
70
- expect(result).toBe(4 + 10 + 18);
71
- });
72
-
73
- it("vector.scale should scale a 2D vector", function () {
74
- const result = vector.scale([4, 2], 0.5);
75
- expect(result).toStrictEqual([2, 1]);
76
- });
77
-
78
- it("vector.scale should scale a 3D vector", function () {
79
- const result = vector.scale([1, 2, 3], 2);
80
- expect(result).toStrictEqual([2, 4, 6]);
81
- });
82
-
83
- it("vector.length should take the length of a 2D vector", function () {
84
- const result = vector.length([3, 4]);
85
- expect(result).toBe(5);
86
- });
87
-
88
- it("vector.length should take the length of a 3D vector", function () {
89
- const result = vector.length([4, 0, 3]);
90
- expect(result).toBe(5);
91
- });
92
-
93
- it("vector.equal should return true on two equal 3D vectors", function () {
94
- const result = vector.equal([6, 3, 4], [6, 3, 4]);
95
- expect(result).toBe(true);
96
- });
97
-
98
- it("vector.equal should return false on two inequal 3D vectors", function () {
99
- const result = vector.equal([6, 3, 4], [6, 4, 4]);
100
- expect(result).toBe(false);
101
- });
102
-
103
- it("vector.equal should return false on a 2D and 3D vector", function () {
104
- const result = vector.equal([6, 4], [6, 4, 4]);
105
- expect(result).toBe(false);
106
- });
107
-
108
- it("vector.equal should return false on a 2D and 3D vector", function () {
109
- const result = vector.equal([6, 3, 4], [6, 3]);
110
- expect(result).toBe(false);
111
- });
112
-
113
- it("vector.equal should return false on a 2D and 3D vector with a trailing 0", function () {
114
- const result = vector.equal([6, 3, 0], [6, 3]);
115
- expect(result).toBe(false);
116
- });
117
-
118
- it(
119
- "vector.collinear should return true on two collinear vectors of " +
120
- "the same magnitude but different direction",
121
- function () {
122
- const result = vector.collinear([3, 3], [-3, -3]);
123
- expect(result).toBe(true);
124
- },
125
- );
126
-
127
- it(
128
- "vector.collinear should return true on two collinear vectors of " +
129
- "different magnitudes",
130
- function () {
131
- const result = vector.collinear([2, 1], [6, 3]);
132
- expect(result).toBe(true);
133
- },
134
- );
135
-
136
- it("vector.collinear should return false on non-collinear vectors", function () {
137
- const result = vector.collinear([1, 2], [-1, 2]);
138
- expect(result).toBe(false);
139
- });
140
-
141
- it("vector.negate of [-2, 2] is [2, -2]", function () {
142
- const result = vector.negate([-2, 2]);
143
- expect(result).toStrictEqual([2, -2]);
144
- });
145
- });
package/src/index.ts DELETED
@@ -1,7 +0,0 @@
1
- export {libVersion} from "./version";
2
-
3
- export * as number from "./number";
4
- export * as vector from "./vector";
5
- export * as point from "./point";
6
- export * as line from "./line";
7
- export * as ray from "./ray";
package/src/line.ts DELETED
@@ -1,42 +0,0 @@
1
- /**
2
- * Line Utils
3
- * A line is an array of two points e.g. [[-5, 0], [5, 0]].
4
- */
5
-
6
- import * as kpoint from "./point";
7
- import * as kvector from "./vector";
8
-
9
- import type {Point} from "./point";
10
-
11
- export type Line = [Point, Point];
12
-
13
- export function distanceToPoint(line: Line, point: Point): number {
14
- return kpoint.distanceToLine(point, line);
15
- }
16
-
17
- export function reflectPoint(line: Line, point: Point): Point {
18
- return kpoint.reflectOverLine(point, line);
19
- }
20
-
21
- export function midpoint(line: Line): Point {
22
- return [(line[0][0] + line[1][0]) / 2, (line[0][1] + line[1][1]) / 2];
23
- }
24
-
25
- export function equal(line1: Line, line2: Line, tolerance?: number): boolean {
26
- // TODO: A nicer implementation might just check collinearity of
27
- // vectors using underscore magick
28
- // Compare the directions of the lines
29
- const v1 = kvector.subtract(line1[1], line1[0]);
30
- const v2 = kvector.subtract(line2[1], line2[0]);
31
- if (!kvector.collinear(v1, v2, tolerance)) {
32
- return false;
33
- }
34
- // If the start point is the same for the two lines, then they are the same
35
- if (kpoint.equal(line1[0], line2[0])) {
36
- return true;
37
- }
38
- // Make sure that the direction to get from line1 to
39
- // line2 is the same as the direction of the lines
40
- const line1ToLine2Vector = kvector.subtract(line2[0], line1[0]);
41
- return kvector.collinear(v1, line1ToLine2Vector, tolerance);
42
- }