geometric-library 1.3.1 → 1.4.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.
Files changed (143) hide show
  1. package/README.md +39 -430
  2. package/dist/cjs/abstracts/{Angle.js → angle/Angle.js} +11 -11
  3. package/dist/cjs/abstracts/angle/Angle.types.js +2 -0
  4. package/dist/cjs/abstracts/angle/index.js +18 -0
  5. package/dist/cjs/abstracts/{Figure.js → figure/Figure.js} +45 -30
  6. package/dist/cjs/abstracts/figure/Figure.types.js +2 -0
  7. package/dist/cjs/abstracts/figure/index.js +18 -0
  8. package/dist/cjs/abstracts/{Flag.js → flag/Flag.js} +1 -0
  9. package/dist/cjs/abstracts/flag/Flag.types.js +2 -0
  10. package/dist/cjs/abstracts/flag/index.js +18 -0
  11. package/dist/cjs/abstracts/index.js +22 -0
  12. package/dist/cjs/abstracts/{Magnitude.js → magnitude/Magnitude.js} +3 -2
  13. package/dist/cjs/abstracts/magnitude/Magnitude.types.js +2 -0
  14. package/dist/cjs/abstracts/magnitude/index.js +18 -0
  15. package/dist/cjs/abstracts/{Point.js → point/Point.js} +8 -7
  16. package/dist/cjs/abstracts/point/Point.types.js +2 -0
  17. package/dist/cjs/abstracts/point/index.js +18 -0
  18. package/dist/cjs/abstracts/{Vector.js → vector/Vector.js} +16 -15
  19. package/dist/cjs/abstracts/vector/Vector.types.js +2 -0
  20. package/dist/cjs/abstracts/vector/index.js +18 -0
  21. package/dist/cjs/figures/arc-curve/ArcCurve.js +174 -0
  22. package/dist/cjs/figures/arc-curve/ArcCurve.types.js +2 -0
  23. package/dist/cjs/figures/arc-curve/index.js +18 -0
  24. package/dist/cjs/figures/{Circle.js → circle/Circle.js} +8 -9
  25. package/dist/cjs/figures/circle/Circle.types.js +2 -0
  26. package/dist/cjs/figures/circle/index.js +18 -0
  27. package/dist/cjs/figures/{CubicBezierCurve.js → cubic-bezier-curve/CubicBezierCurve.js} +22 -17
  28. package/dist/cjs/figures/cubic-bezier-curve/CubicBezierCurve.types.js +2 -0
  29. package/dist/cjs/figures/cubic-bezier-curve/index.js +18 -0
  30. package/dist/cjs/figures/ellipse/Ellipse.js +129 -0
  31. package/dist/cjs/figures/ellipse/Ellipse.types.js +2 -0
  32. package/dist/cjs/figures/ellipse/index.js +18 -0
  33. package/dist/cjs/figures/index.js +23 -0
  34. package/dist/cjs/figures/{Line.js → line/Line.js} +78 -59
  35. package/dist/cjs/figures/line/Line.types.js +2 -0
  36. package/dist/cjs/figures/line/index.js +18 -0
  37. package/dist/cjs/figures/polygon/Polygon.js +51 -0
  38. package/dist/cjs/figures/polygon/Polygon.types.js +2 -0
  39. package/dist/cjs/figures/polygon/index.js +18 -0
  40. package/dist/cjs/figures/{QuadraticBezierCurve.js → quadratic-bezier-curve/QuadraticBezierCurve.js} +12 -8
  41. package/dist/cjs/figures/quadratic-bezier-curve/QuadraticBezierCurve.types.js +2 -0
  42. package/dist/cjs/figures/quadratic-bezier-curve/index.js +18 -0
  43. package/dist/cjs/index.js +2 -29
  44. package/dist/cjs/utilities/{Calculator.js → calculator/Calculator.js} +9 -1
  45. package/dist/cjs/utilities/calculator/index.js +17 -0
  46. package/dist/cjs/utilities/index.js +20 -8
  47. package/dist/esm/abstracts/{Angle.js → angle/Angle.js} +4 -4
  48. package/dist/esm/abstracts/angle/Angle.types.js +1 -0
  49. package/dist/esm/abstracts/angle/index.js +2 -0
  50. package/dist/esm/abstracts/{Figure.js → figure/Figure.js} +37 -22
  51. package/dist/esm/abstracts/figure/Figure.types.js +1 -0
  52. package/dist/esm/abstracts/figure/index.js +2 -0
  53. package/dist/esm/abstracts/{Flag.js → flag/Flag.js} +1 -0
  54. package/dist/esm/abstracts/flag/Flag.types.js +1 -0
  55. package/dist/esm/abstracts/flag/index.js +2 -0
  56. package/dist/esm/abstracts/index.js +6 -0
  57. package/dist/esm/abstracts/{Magnitude.js → magnitude/Magnitude.js} +2 -1
  58. package/dist/esm/abstracts/magnitude/Magnitude.types.js +1 -0
  59. package/dist/esm/abstracts/magnitude/index.js +2 -0
  60. package/dist/esm/abstracts/{Point.js → point/Point.js} +2 -1
  61. package/dist/esm/abstracts/point/Point.types.js +1 -0
  62. package/dist/esm/abstracts/point/index.js +2 -0
  63. package/dist/esm/abstracts/{Vector.js → vector/Vector.js} +3 -2
  64. package/dist/esm/abstracts/vector/Vector.types.js +1 -0
  65. package/dist/esm/abstracts/vector/index.js +2 -0
  66. package/dist/esm/figures/{ArcCurve.js → arc-curve/ArcCurve.js} +50 -8
  67. package/dist/esm/figures/arc-curve/ArcCurve.types.js +1 -0
  68. package/dist/esm/figures/arc-curve/index.js +2 -0
  69. package/dist/esm/figures/{Circle.js → circle/Circle.js} +3 -4
  70. package/dist/esm/figures/circle/Circle.types.js +1 -0
  71. package/dist/esm/figures/circle/index.js +2 -0
  72. package/dist/esm/figures/{CubicBezierCurve.js → cubic-bezier-curve/CubicBezierCurve.js} +11 -6
  73. package/dist/esm/figures/cubic-bezier-curve/CubicBezierCurve.types.js +1 -0
  74. package/dist/esm/figures/cubic-bezier-curve/index.js +2 -0
  75. package/dist/esm/figures/{Ellipse.js → ellipse/Ellipse.js} +24 -6
  76. package/dist/esm/figures/ellipse/Ellipse.types.js +1 -0
  77. package/dist/esm/figures/ellipse/index.js +2 -0
  78. package/dist/esm/figures/index.js +7 -0
  79. package/dist/esm/figures/{Line.js → line/Line.js} +57 -38
  80. package/dist/esm/figures/line/Line.types.js +1 -0
  81. package/dist/esm/figures/line/index.js +2 -0
  82. package/dist/esm/figures/polygon/Polygon.js +47 -0
  83. package/dist/esm/figures/polygon/Polygon.types.js +1 -0
  84. package/dist/esm/figures/polygon/index.js +2 -0
  85. package/dist/esm/figures/{QuadraticBezierCurve.js → quadratic-bezier-curve/QuadraticBezierCurve.js} +7 -3
  86. package/dist/esm/figures/quadratic-bezier-curve/QuadraticBezierCurve.types.js +1 -0
  87. package/dist/esm/figures/quadratic-bezier-curve/index.js +2 -0
  88. package/dist/esm/index.js +2 -14
  89. package/dist/esm/utilities/{Calculator.js → calculator/Calculator.js} +9 -1
  90. package/dist/esm/utilities/calculator/index.js +1 -0
  91. package/dist/esm/utilities/index.js +4 -6
  92. package/dist/types/abstracts/{Angle.d.ts → angle/Angle.d.ts} +2 -1
  93. package/dist/types/abstracts/angle/Angle.types.d.ts +16 -0
  94. package/dist/types/abstracts/angle/index.d.ts +2 -0
  95. package/dist/types/abstracts/{Figure.d.ts → figure/Figure.d.ts} +4 -4
  96. package/dist/types/abstracts/figure/Figure.types.d.ts +14 -0
  97. package/dist/types/abstracts/figure/index.d.ts +2 -0
  98. package/dist/types/abstracts/{Flag.d.ts → flag/Flag.d.ts} +2 -1
  99. package/dist/types/abstracts/flag/Flag.types.d.ts +8 -0
  100. package/dist/types/abstracts/flag/index.d.ts +2 -0
  101. package/dist/types/abstracts/index.d.ts +6 -0
  102. package/dist/types/abstracts/{Magnitude.d.ts → magnitude/Magnitude.d.ts} +2 -1
  103. package/dist/types/abstracts/magnitude/Magnitude.types.d.ts +8 -0
  104. package/dist/types/abstracts/magnitude/index.d.ts +2 -0
  105. package/dist/types/abstracts/{Point.d.ts → point/Point.d.ts} +2 -1
  106. package/dist/types/abstracts/point/Point.types.d.ts +13 -0
  107. package/dist/types/abstracts/point/index.d.ts +2 -0
  108. package/dist/types/abstracts/{Vector.d.ts → vector/Vector.d.ts} +3 -1
  109. package/dist/types/abstracts/vector/Vector.types.d.ts +19 -0
  110. package/dist/types/abstracts/vector/index.d.ts +2 -0
  111. package/dist/types/figures/{ArcCurve.d.ts → arc-curve/ArcCurve.d.ts} +7 -2
  112. package/dist/types/figures/arc-curve/ArcCurve.types.d.ts +11 -0
  113. package/dist/types/figures/arc-curve/index.d.ts +2 -0
  114. package/dist/types/figures/{Circle.d.ts → circle/Circle.d.ts} +3 -2
  115. package/dist/types/figures/circle/Circle.types.d.ts +9 -0
  116. package/dist/types/figures/circle/index.d.ts +2 -0
  117. package/dist/types/figures/{CubicBezierCurve.d.ts → cubic-bezier-curve/CubicBezierCurve.d.ts} +4 -2
  118. package/dist/types/figures/cubic-bezier-curve/CubicBezierCurve.types.d.ts +8 -0
  119. package/dist/types/figures/cubic-bezier-curve/index.d.ts +2 -0
  120. package/dist/types/figures/{Ellipse.d.ts → ellipse/Ellipse.d.ts} +5 -2
  121. package/dist/types/figures/ellipse/Ellipse.types.d.ts +16 -0
  122. package/dist/types/figures/ellipse/index.d.ts +2 -0
  123. package/dist/types/figures/index.d.ts +7 -0
  124. package/dist/types/figures/{Line.d.ts → line/Line.d.ts} +6 -4
  125. package/dist/types/figures/line/Line.types.d.ts +30 -0
  126. package/dist/types/figures/line/index.d.ts +2 -0
  127. package/dist/types/figures/polygon/Polygon.d.ts +17 -0
  128. package/dist/types/figures/polygon/Polygon.types.d.ts +8 -0
  129. package/dist/types/figures/polygon/index.d.ts +2 -0
  130. package/dist/types/figures/{QuadraticBezierCurve.d.ts → quadratic-bezier-curve/QuadraticBezierCurve.d.ts} +4 -2
  131. package/dist/types/figures/quadratic-bezier-curve/QuadraticBezierCurve.types.d.ts +8 -0
  132. package/dist/types/figures/quadratic-bezier-curve/index.d.ts +2 -0
  133. package/dist/types/index.d.ts +2 -14
  134. package/dist/types/types/index.d.ts +0 -129
  135. package/dist/types/utilities/{Calculator.d.ts → calculator/Calculator.d.ts} +4 -0
  136. package/dist/types/utilities/calculator/index.d.ts +1 -0
  137. package/dist/types/utilities/index.d.ts +2 -4
  138. package/package.json +24 -18
  139. package/dist/cjs/figures/ArcCurve.js +0 -132
  140. package/dist/cjs/figures/Ellipse.js +0 -111
  141. package/dist/cjs/figures/Polygon.js +0 -17
  142. package/dist/esm/figures/Polygon.js +0 -13
  143. package/dist/types/figures/Polygon.d.ts +0 -7
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./Circle"), exports);
18
+ __exportStar(require("./Circle.types"), exports);
@@ -1,10 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CubicBezierCurve = void 0;
4
- const Calculator_1 = require("../utilities/Calculator");
5
- const Point_1 = require("../abstracts/Point");
6
- const Figure_1 = require("../abstracts/Figure");
7
- class CubicBezierCurve extends Figure_1.Figure {
4
+ const _abstracts_1 = require("../../abstracts");
5
+ const calculator_1 = require("../../utilities/calculator");
6
+ class CubicBezierCurve extends _abstracts_1.Figure {
8
7
  _criticalPoints;
9
8
  constructor(values) {
10
9
  super(values);
@@ -13,7 +12,7 @@ class CubicBezierCurve extends Figure_1.Figure {
13
12
  get boundingBox() {
14
13
  const [P0, , , P3] = this.values;
15
14
  const points = [P0, P3, ...this._criticalPoints];
16
- return Figure_1.Figure.computeBoundingBox(points);
15
+ return _abstracts_1.Figure.computeBoundingBox(points);
17
16
  }
18
17
  get criticalPoints() {
19
18
  return this._criticalPoints;
@@ -61,6 +60,11 @@ class CubicBezierCurve extends Figure_1.Figure {
61
60
  this.recompute();
62
61
  return this;
63
62
  }
63
+ scaleXY(factorX, factorY, about) {
64
+ super.scaleXY(factorX, factorY, about);
65
+ this.recompute();
66
+ return this;
67
+ }
64
68
  translate(vector) {
65
69
  super.translate(vector);
66
70
  this.recompute();
@@ -73,7 +77,8 @@ class CubicBezierCurve extends Figure_1.Figure {
73
77
  }
74
78
  const criticalPoints = tValues.reduce((criticalPoints, t) => {
75
79
  const criticalPoint = this.getPointAtParameter(t);
76
- criticalPoint && criticalPoints.push(criticalPoint);
80
+ if (criticalPoint)
81
+ criticalPoints.push(criticalPoint);
77
82
  return criticalPoints;
78
83
  }, []);
79
84
  return criticalPoints;
@@ -85,17 +90,17 @@ class CubicBezierCurve extends Figure_1.Figure {
85
90
  const tValues = [];
86
91
  for (let i = 0; i < axii.length; i++) {
87
92
  const axis = axii[i];
88
- const p0 = new Calculator_1.Calculator(P0[axis]);
89
- const p1 = new Calculator_1.Calculator(P1[axis]);
90
- const p2 = new Calculator_1.Calculator(P2[axis]);
91
- const p3 = new Calculator_1.Calculator(P3[axis]);
93
+ const p0 = new calculator_1.Calculator(P0[axis]);
94
+ const p1 = new calculator_1.Calculator(P1[axis]);
95
+ const p2 = new calculator_1.Calculator(P2[axis]);
96
+ const p3 = new calculator_1.Calculator(P3[axis]);
92
97
  const a = p3.sub(p2.mul(3)).add(p1.mul(3)).sub(p0).mul(3);
93
98
  const b = p2.sub(p1.mul(2)).add(p0).mul(6);
94
99
  const c = p1.sub(p0).mul(3);
95
100
  for (let n = 0; n < signs.length; n++) {
96
101
  const sign = signs[n];
97
102
  let tValue;
98
- if (+a === 0) {
103
+ if (calculator_1.Calculator.isNearZero(+a)) {
99
104
  // 1st degree equation to avoid n/0
100
105
  tValue = c.neg().div(b);
101
106
  }
@@ -103,7 +108,7 @@ class CubicBezierCurve extends Figure_1.Figure {
103
108
  // 2nd degree equation
104
109
  tValue = b
105
110
  .neg()
106
- .add(Calculator_1.Calculator.mul(sign, b.pow(2).sub(a.mul(c).mul(4)).sqrt()))
111
+ .add(calculator_1.Calculator.mul(sign, b.pow(2).sub(a.mul(c).mul(4)).sqrt()))
107
112
  .div(a.mul(2));
108
113
  }
109
114
  if (this.isValidParameter(tValue)) {
@@ -114,7 +119,7 @@ class CubicBezierCurve extends Figure_1.Figure {
114
119
  if (!tValues.length) {
115
120
  return;
116
121
  }
117
- return tValues.filter((value, index, array) => index === array.findIndex((v) => +value === +v));
122
+ return tValues.filter((value, index, array) => index === array.findIndex((v) => calculator_1.Calculator.isEqual(+value, +v)));
118
123
  }
119
124
  getCoordinateAtParameter(t, axis) {
120
125
  const { P0, P1, P2, P3 } = this;
@@ -123,11 +128,11 @@ class CubicBezierCurve extends Figure_1.Figure {
123
128
  const p2 = P2[axis];
124
129
  const p3 = P3[axis];
125
130
  // parametric cubic bezier equation
126
- const coordinate = Calculator_1.Calculator.sub(1, t)
131
+ const coordinate = calculator_1.Calculator.sub(1, t)
127
132
  .pow(3)
128
133
  .mul(p0)
129
- .add(Calculator_1.Calculator.sub(1, t).pow(2).mul(3).mul(t).mul(p1))
130
- .add(Calculator_1.Calculator.sub(1, t).mul(3).mul(t.pow(2)).mul(p2))
134
+ .add(calculator_1.Calculator.sub(1, t).pow(2).mul(3).mul(t).mul(p1))
135
+ .add(calculator_1.Calculator.sub(1, t).mul(3).mul(t.pow(2)).mul(p2))
131
136
  .add(t.pow(3).mul(p3));
132
137
  if (!coordinate.isFinite()) {
133
138
  return;
@@ -140,7 +145,7 @@ class CubicBezierCurve extends Figure_1.Figure {
140
145
  if (typeof x === 'undefined' || typeof y === 'undefined') {
141
146
  return;
142
147
  }
143
- return new Point_1.Point([+x, +y]);
148
+ return new _abstracts_1.Point([+x, +y]);
144
149
  }
145
150
  isValidParameter(t) {
146
151
  return t.isFinite() && +t > 0 && +t < 1;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./CubicBezierCurve"), exports);
18
+ __exportStar(require("./CubicBezierCurve.types"), exports);
@@ -0,0 +1,129 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Ellipse = void 0;
4
+ const _abstracts_1 = require("../../abstracts");
5
+ const CubicBezierCurve_1 = require("../cubic-bezier-curve/CubicBezierCurve");
6
+ const calculator_1 = require("../../utilities/calculator");
7
+ const Line_1 = require("../line/Line");
8
+ const KAPPA = 0.5522847498307936;
9
+ const xAxis = new Line_1.Line([new _abstracts_1.Point([0, 0]), new _abstracts_1.Point([1, 0])]);
10
+ const unitCircleCubicBezierCurves = [
11
+ new CubicBezierCurve_1.CubicBezierCurve([new _abstracts_1.Point([1, 0]), new _abstracts_1.Point([1, KAPPA]), new _abstracts_1.Point([KAPPA, 1]), new _abstracts_1.Point([0, 1])]),
12
+ new CubicBezierCurve_1.CubicBezierCurve([new _abstracts_1.Point([0, 1]), new _abstracts_1.Point([-KAPPA, 1]), new _abstracts_1.Point([-1, KAPPA]), new _abstracts_1.Point([-1, 0])]),
13
+ new CubicBezierCurve_1.CubicBezierCurve([new _abstracts_1.Point([-1, 0]), new _abstracts_1.Point([-1, -KAPPA]), new _abstracts_1.Point([-KAPPA, -1]), new _abstracts_1.Point([0, -1])]),
14
+ new CubicBezierCurve_1.CubicBezierCurve([new _abstracts_1.Point([0, -1]), new _abstracts_1.Point([KAPPA, -1]), new _abstracts_1.Point([1, -KAPPA]), new _abstracts_1.Point([1, 0])])
15
+ ];
16
+ class Ellipse extends _abstracts_1.Figure {
17
+ _center;
18
+ _criticalPoints;
19
+ _phi;
20
+ _rx;
21
+ _ry;
22
+ constructor(values) {
23
+ super(values);
24
+ const [center, rx, ry, phi] = values;
25
+ this._center = center;
26
+ this._rx = rx;
27
+ this._ry = ry;
28
+ this._phi = phi;
29
+ this._criticalPoints = this.computeCriticalPoints({ center, rx, ry, phi });
30
+ }
31
+ get boundingBox() {
32
+ return _abstracts_1.Figure.computeBoundingBox(this.criticalPoints);
33
+ }
34
+ get center() {
35
+ return this._center;
36
+ }
37
+ get criticalPoints() {
38
+ return this._criticalPoints;
39
+ }
40
+ get isCircle() {
41
+ return calculator_1.Calculator.isEqual(+this.rx, +this.ry);
42
+ }
43
+ get phi() {
44
+ return this._phi;
45
+ }
46
+ get rx() {
47
+ return this._rx;
48
+ }
49
+ get ry() {
50
+ return this._ry;
51
+ }
52
+ clone() {
53
+ const values = this.values.map((value) => value.clone());
54
+ return new Ellipse(values);
55
+ }
56
+ computePointForTheta(theta, ellipse = this) {
57
+ const { center, rx, ry, phi } = ellipse;
58
+ const x = +calculator_1.Calculator.mul(+rx, phi.cos).mul(theta.cos).sub(calculator_1.Calculator.mul(+ry, phi.sin).mul(theta.sin)).add(center.x);
59
+ const y = +calculator_1.Calculator.mul(+rx, phi.sin).mul(theta.cos).add(calculator_1.Calculator.mul(+ry, phi.cos).mul(theta.sin)).add(center.y);
60
+ return new _abstracts_1.Point([x, y]);
61
+ }
62
+ computeThetaForPoint({ x, y }) {
63
+ const { center, phi, rx, ry } = this;
64
+ const { x: cx, y: cy } = center;
65
+ const dy = calculator_1.Calculator.sub(y, cy);
66
+ const dx = calculator_1.Calculator.sub(x, cx);
67
+ const sinTheta = dy.mul(phi.cos).sub(dx.mul(phi.sin)).div(+ry);
68
+ const cosTheta = dx.mul(phi.cos).add(dy.mul(phi.sin)).div(+rx);
69
+ const theta = new _abstracts_1.Angle(+calculator_1.Calculator.atan2(sinTheta, cosTheta), 'radians').normalize();
70
+ return theta;
71
+ }
72
+ reflect(about) {
73
+ super.reflect(about);
74
+ if (!this.isCircle && 'V' in about) {
75
+ const alpha = about.angleTo(xAxis);
76
+ this._phi.replace(+calculator_1.Calculator.mul(2, alpha.radians).sub(this.phi.radians), 'radians');
77
+ this._phi.normalize();
78
+ }
79
+ this.recompute();
80
+ return this;
81
+ }
82
+ rotate(alpha, about) {
83
+ this._center.rotate(alpha, about);
84
+ if (!this.isCircle) {
85
+ this._phi.replace(+calculator_1.Calculator.add(this.phi.radians, alpha.radians), 'radians');
86
+ this._phi.normalize();
87
+ }
88
+ this.recompute();
89
+ return this;
90
+ }
91
+ scale(factor, about = new _abstracts_1.Point([0, 0])) {
92
+ super.scale(factor, about);
93
+ this.recompute();
94
+ return this;
95
+ }
96
+ toCubicBezierCurves() {
97
+ const { rx, ry, phi, center } = this;
98
+ const curves = unitCircleCubicBezierCurves.map((curve) => {
99
+ return curve
100
+ .clone()
101
+ .scaleXY(+rx, +ry)
102
+ .rotate(phi)
103
+ .translate(new _abstracts_1.Vector([center.x, center.y]));
104
+ });
105
+ return curves;
106
+ }
107
+ translate(vector) {
108
+ this._center.translate(vector);
109
+ this.recompute();
110
+ return this;
111
+ }
112
+ computeCriticalPoints(ellipse = this) {
113
+ const { rx, ry, phi } = ellipse;
114
+ const { computePointForTheta } = this;
115
+ const xThetaPrincipal = new _abstracts_1.Angle(+calculator_1.Calculator.neg(+ry).mul(phi.tan).div(+rx).atan(), 'radians');
116
+ const yThetaPrincipal = new _abstracts_1.Angle(+calculator_1.Calculator.mul(+ry, phi.cot).div(+rx).atan(), 'radians');
117
+ const xThetaSecondary = new _abstracts_1.Angle(+calculator_1.Calculator.add(+xThetaPrincipal, Math.PI), 'radians');
118
+ const yThetaSecondary = new _abstracts_1.Angle(+calculator_1.Calculator.add(+yThetaPrincipal, Math.PI), 'radians');
119
+ const firstPoint = computePointForTheta(xThetaPrincipal, ellipse);
120
+ const secondPoint = computePointForTheta(yThetaPrincipal, ellipse);
121
+ const thirdPoint = computePointForTheta(xThetaSecondary, ellipse);
122
+ const fourthPoint = computePointForTheta(yThetaSecondary, ellipse);
123
+ return [firstPoint, secondPoint, thirdPoint, fourthPoint];
124
+ }
125
+ recompute() {
126
+ this._criticalPoints = this.computeCriticalPoints();
127
+ }
128
+ }
129
+ exports.Ellipse = Ellipse;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./Ellipse"), exports);
18
+ __exportStar(require("./Ellipse.types"), exports);
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./arc-curve"), exports);
18
+ __exportStar(require("./circle"), exports);
19
+ __exportStar(require("./cubic-bezier-curve"), exports);
20
+ __exportStar(require("./ellipse"), exports);
21
+ __exportStar(require("./line"), exports);
22
+ __exportStar(require("./polygon"), exports);
23
+ __exportStar(require("./quadratic-bezier-curve"), exports);
@@ -1,15 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Line = void 0;
4
- const Calculator_1 = require("../utilities/Calculator");
5
- const Angle_1 = require("../abstracts/Angle");
6
- const Point_1 = require("../abstracts/Point");
7
- const Vector_1 = require("../abstracts/Vector");
8
- const Figure_1 = require("../abstracts/Figure");
9
- class Line extends Figure_1.Figure {
4
+ const _abstracts_1 = require("../../abstracts");
5
+ const calculator_1 = require("../../utilities/calculator");
6
+ class Line extends _abstracts_1.Figure {
10
7
  _P0;
11
8
  _P1;
12
9
  _V;
10
+ _dirty = true;
13
11
  _reciprocal;
14
12
  _slope;
15
13
  _xIntercept;
@@ -17,20 +15,16 @@ class Line extends Figure_1.Figure {
17
15
  constructor(values) {
18
16
  super(values);
19
17
  const [P0, anchor] = values;
20
- if (anchor instanceof Vector_1.Vector) {
18
+ if (anchor instanceof _abstracts_1.Vector) {
21
19
  this._V = anchor;
22
20
  this._P1 = P0.clone().translate(anchor);
23
21
  // anchor instanceof Point
24
22
  }
25
23
  else {
26
- this._V = new Vector_1.Vector([P0, anchor]);
24
+ this._V = new _abstracts_1.Vector([P0, anchor]);
27
25
  this._P1 = anchor;
28
26
  }
29
27
  this._P0 = P0;
30
- this._slope = this.computeSlope();
31
- this._yIntercept = this.computeYIntercept();
32
- this._reciprocal = this.computeReciprocal();
33
- this._xIntercept = this.computeXIntercept();
34
28
  this.points = [this.P0, this.P1];
35
29
  this.vectors = [this.V];
36
30
  }
@@ -83,33 +77,41 @@ class Line extends Figure_1.Figure {
83
77
  return typeof slope === 'undefined';
84
78
  }
85
79
  get reciprocal() {
80
+ this.ensureComputed();
86
81
  return this._reciprocal;
87
82
  }
88
83
  get slope() {
84
+ this.ensureComputed();
89
85
  return this._slope;
90
86
  }
91
87
  get xIntercept() {
88
+ this.ensureComputed();
92
89
  return this._xIntercept;
93
90
  }
94
91
  get yIntercept() {
92
+ this.ensureComputed();
95
93
  return this._yIntercept;
96
94
  }
97
- angleTo(line) {
98
- if (this.isParallelTo(line)) {
99
- return new Angle_1.Angle(0, 'radians');
100
- }
101
- if (this.isPerpendicularTo(line)) {
102
- return new Angle_1.Angle(+Calculator_1.Calculator.div(Math.PI, 2), 'radians');
103
- }
104
- if (typeof this.slope === 'undefined') {
105
- return new Angle_1.Angle(+Calculator_1.Calculator.atan(line.slope), 'radians');
106
- }
107
- if (typeof line.slope === 'undefined') {
108
- return new Angle_1.Angle(+Calculator_1.Calculator.atan(this.slope), 'radians');
109
- }
110
- const thisSlope = new Calculator_1.Calculator(this.slope);
111
- const lineSlope = new Calculator_1.Calculator(line.slope);
112
- return new Angle_1.Angle(+lineSlope.sub(thisSlope).div(thisSlope.mul(lineSlope).add(1)).abs().atan(), 'radians');
95
+ angleTo(reference) {
96
+ if ('slope' in reference) {
97
+ const line = reference;
98
+ if (this.isParallelTo(line)) {
99
+ return new _abstracts_1.Angle(0, 'radians');
100
+ }
101
+ if (this.isPerpendicularTo(line)) {
102
+ return new _abstracts_1.Angle(+calculator_1.Calculator.div(Math.PI, 2), 'radians');
103
+ }
104
+ if (typeof this.slope === 'undefined') {
105
+ return new _abstracts_1.Angle(+calculator_1.Calculator.atan(line.slope), 'radians');
106
+ }
107
+ if (typeof line.slope === 'undefined') {
108
+ return new _abstracts_1.Angle(+calculator_1.Calculator.atan(this.slope), 'radians');
109
+ }
110
+ const thisSlope = new calculator_1.Calculator(this.slope);
111
+ const lineSlope = new calculator_1.Calculator(line.slope);
112
+ return new _abstracts_1.Angle(+lineSlope.sub(thisSlope).div(thisSlope.mul(lineSlope).add(1)).abs().atan(), 'radians');
113
+ }
114
+ return this.V.angleTo(reference);
113
115
  }
114
116
  clone() {
115
117
  const values = this.values.map((value) => value.clone());
@@ -119,10 +121,10 @@ class Line extends Figure_1.Figure {
119
121
  if (this.isParallelTo(line)) {
120
122
  return;
121
123
  }
122
- const denominator = Calculator_1.Calculator.mul(this.a, line.b).sub(Calculator_1.Calculator.mul(line.a, this.b));
123
- const x = +Calculator_1.Calculator.mul(this.b, line.c).sub(Calculator_1.Calculator.mul(line.b, this.c)).div(denominator);
124
- const y = +Calculator_1.Calculator.mul(this.c, line.a).sub(Calculator_1.Calculator.mul(line.c, this.a)).div(denominator);
125
- return new Point_1.Point([x, y]);
124
+ const denominator = calculator_1.Calculator.mul(this.a, line.b).sub(calculator_1.Calculator.mul(line.a, this.b));
125
+ const x = +calculator_1.Calculator.mul(this.b, line.c).sub(calculator_1.Calculator.mul(line.b, this.c)).div(denominator);
126
+ const y = +calculator_1.Calculator.mul(this.c, line.a).sub(calculator_1.Calculator.mul(line.c, this.a)).div(denominator);
127
+ return new _abstracts_1.Point([x, y]);
126
128
  }
127
129
  getPerpendicularProjection(point) {
128
130
  const { P0, slope, isVertical, isHorizontal } = this;
@@ -131,30 +133,32 @@ class Line extends Figure_1.Figure {
131
133
  return point.clone();
132
134
  }
133
135
  if (isVertical) {
134
- perpendicularProjection = new Point_1.Point([P0.x, point.y]);
136
+ perpendicularProjection = new _abstracts_1.Point([P0.x, point.y]);
135
137
  }
136
138
  else if (isHorizontal) {
137
- perpendicularProjection = new Point_1.Point([point.x, P0.y]);
139
+ perpendicularProjection = new _abstracts_1.Point([point.x, P0.y]);
138
140
  }
139
141
  else {
140
- const x = new Calculator_1.Calculator(1);
141
- const y = x.mul(1).div(slope).neg().add(Calculator_1.Calculator.div(point.x, slope)).add(point.y);
142
- perpendicularProjection = new Point_1.Point([+x, +y]);
142
+ const perpendicularSlope = +calculator_1.Calculator.div(-1, slope);
143
+ const perpendicularYIntercept = +calculator_1.Calculator.sub(point.y, calculator_1.Calculator.mul(perpendicularSlope, point.x));
144
+ const x = +calculator_1.Calculator.sub(perpendicularYIntercept, this.yIntercept).div(calculator_1.Calculator.sub(slope, perpendicularSlope));
145
+ const y = +calculator_1.Calculator.mul(slope, x).add(this.yIntercept);
146
+ perpendicularProjection = new _abstracts_1.Point([x, y]);
143
147
  }
144
148
  return perpendicularProjection;
145
149
  }
146
150
  getPerpendicularThrough(point) {
147
151
  if (this.hasPoint(point)) {
148
- const phi = new Angle_1.Angle(+Calculator_1.Calculator.div(Math.PI, 2), 'radians');
152
+ const phi = new _abstracts_1.Angle(+calculator_1.Calculator.div(Math.PI, 2), 'radians');
149
153
  return this.clone().rotate(phi, point);
150
154
  }
151
155
  return new Line([point, this.getPerpendicularProjection(point)]);
152
156
  }
153
157
  getPointAtParameter(t) {
154
158
  const { P0, V } = this;
155
- const x = +Calculator_1.Calculator.add(P0.x, Calculator_1.Calculator.mul(V.dx, t));
156
- const y = +Calculator_1.Calculator.add(P0.y, Calculator_1.Calculator.mul(V.dy, t));
157
- return new Point_1.Point([x, y]);
159
+ const x = +calculator_1.Calculator.add(P0.x, calculator_1.Calculator.mul(V.dx, t));
160
+ const y = +calculator_1.Calculator.add(P0.y, calculator_1.Calculator.mul(V.dy, t));
161
+ return new _abstracts_1.Point([x, y]);
158
162
  }
159
163
  getXValueAtY(y) {
160
164
  const { reciprocal, xIntercept } = this;
@@ -162,9 +166,9 @@ class Line extends Figure_1.Figure {
162
166
  return;
163
167
  }
164
168
  if (typeof xIntercept === 'undefined') {
165
- return +Calculator_1.Calculator.mul(reciprocal, y);
169
+ return +calculator_1.Calculator.mul(reciprocal, y);
166
170
  }
167
- return +Calculator_1.Calculator.mul(reciprocal, y).add(xIntercept);
171
+ return +calculator_1.Calculator.mul(reciprocal, y).add(xIntercept);
168
172
  }
169
173
  getYValueAtX(x) {
170
174
  const { slope, yIntercept } = this;
@@ -172,16 +176,22 @@ class Line extends Figure_1.Figure {
172
176
  return;
173
177
  }
174
178
  if (typeof yIntercept === 'undefined') {
175
- return +Calculator_1.Calculator.mul(slope, x);
179
+ return +calculator_1.Calculator.mul(slope, x);
176
180
  }
177
- return +Calculator_1.Calculator.mul(slope, x).add(yIntercept);
181
+ return +calculator_1.Calculator.mul(slope, x).add(yIntercept);
178
182
  }
179
183
  hasPoint(P) {
184
+ if (this.isVertical) {
185
+ return calculator_1.Calculator.isEqual(P.x, this.P0.x);
186
+ }
180
187
  const potentialY = this.getYValueAtX(P.x);
181
- return typeof potentialY === 'number' && potentialY === P.y;
188
+ return typeof potentialY === 'number' && calculator_1.Calculator.isEqual(potentialY, P.y);
182
189
  }
183
190
  isParallelTo(line) {
184
- return this.slope == line.slope;
191
+ if (typeof this.slope === 'undefined' || typeof line.slope === 'undefined') {
192
+ return typeof this.slope === typeof line.slope;
193
+ }
194
+ return calculator_1.Calculator.isEqual(this.slope, line.slope);
185
195
  }
186
196
  isPerpendicularTo(line) {
187
197
  if (this.isVertical) {
@@ -190,46 +200,51 @@ class Line extends Figure_1.Figure {
190
200
  if (this.isHorizontal) {
191
201
  return line.isVertical;
192
202
  }
203
+ /* v8 ignore next 3 */
193
204
  if (line.isVertical) {
194
205
  return this.isHorizontal;
195
206
  }
207
+ /* v8 ignore next 3 */
196
208
  if (line.isHorizontal) {
197
209
  return this.isVertical;
198
210
  }
199
- const thisSlope = new Calculator_1.Calculator(this.slope);
200
- const lineSlope = new Calculator_1.Calculator(line.slope);
201
- return +thisSlope === +Calculator_1.Calculator.div(-1, lineSlope);
211
+ return calculator_1.Calculator.isEqual(this.slope, +calculator_1.Calculator.div(-1, line.slope));
202
212
  }
203
213
  reflect(about) {
204
214
  super.reflect(about);
205
- this.recompute();
215
+ this._dirty = true;
206
216
  return this;
207
217
  }
208
218
  rotate(phi, about) {
209
219
  super.rotate(phi, about);
210
- this.recompute();
220
+ this._dirty = true;
211
221
  return this;
212
222
  }
213
223
  scale(factor, about) {
214
224
  super.scale(factor, about);
215
- this.recompute();
225
+ this._dirty = true;
226
+ return this;
227
+ }
228
+ scaleXY(factorX, factorY, about) {
229
+ super.scaleXY(factorX, factorY, about);
230
+ this._dirty = true;
216
231
  return this;
217
232
  }
218
233
  translate(vector) {
219
234
  super.translate(vector);
220
- this.recompute();
235
+ this._dirty = true;
221
236
  return this;
222
237
  }
223
238
  computeReciprocal() {
224
239
  const { P0, P1 } = this;
225
- const reciprocal = Calculator_1.Calculator.sub(P1.x, P0.x).div(Calculator_1.Calculator.sub(P1.y, P0.y));
240
+ const reciprocal = calculator_1.Calculator.sub(P1.x, P0.x).div(calculator_1.Calculator.sub(P1.y, P0.y));
226
241
  if (reciprocal.isFinite()) {
227
242
  return +reciprocal;
228
243
  }
229
244
  }
230
245
  computeSlope() {
231
246
  const { P0, P1 } = this;
232
- const slope = Calculator_1.Calculator.sub(P1.y, P0.y).div(Calculator_1.Calculator.sub(P1.x, P0.x));
247
+ const slope = calculator_1.Calculator.sub(P1.y, P0.y).div(calculator_1.Calculator.sub(P1.x, P0.x));
233
248
  if (slope.isFinite()) {
234
249
  return +slope;
235
250
  }
@@ -242,7 +257,7 @@ class Line extends Figure_1.Figure {
242
257
  if (isHorizontal) {
243
258
  return;
244
259
  }
245
- return +Calculator_1.Calculator.sub(P0.x, Calculator_1.Calculator.mul(reciprocal, P0.y));
260
+ return +calculator_1.Calculator.sub(P0.x, calculator_1.Calculator.mul(reciprocal, P0.y));
246
261
  }
247
262
  computeYIntercept() {
248
263
  const { P0, slope, isVertical, isHorizontal } = this;
@@ -252,9 +267,13 @@ class Line extends Figure_1.Figure {
252
267
  if (isHorizontal) {
253
268
  return P0.y;
254
269
  }
255
- return +Calculator_1.Calculator.sub(P0.y, Calculator_1.Calculator.mul(slope, P0.x));
270
+ return +calculator_1.Calculator.sub(P0.y, calculator_1.Calculator.mul(slope, P0.x));
256
271
  }
257
- recompute() {
272
+ ensureComputed() {
273
+ if (!this._dirty) {
274
+ return;
275
+ }
276
+ this._dirty = false;
258
277
  this._slope = this.computeSlope();
259
278
  this._yIntercept = this.computeYIntercept();
260
279
  this._reciprocal = this.computeReciprocal();
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./Line"), exports);
18
+ __exportStar(require("./Line.types"), exports);