js-draw 0.18.2 → 0.19.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 (227) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/bundle.js +2 -2
  3. package/dist/bundledStyles.js +1 -0
  4. package/dist/cjs/src/Color4.d.ts +8 -0
  5. package/dist/cjs/src/Color4.js +67 -0
  6. package/dist/cjs/src/Editor.d.ts +2 -2
  7. package/dist/cjs/src/Editor.js +2 -2
  8. package/dist/cjs/src/SVGLoader.js +8 -5
  9. package/dist/cjs/src/components/AbstractComponent.d.ts +2 -2
  10. package/dist/cjs/src/components/AbstractComponent.js +3 -3
  11. package/dist/cjs/src/components/ImageBackground.js +1 -1
  12. package/dist/cjs/src/components/RestylableComponent.d.ts +21 -2
  13. package/dist/cjs/src/components/Stroke.d.ts +35 -0
  14. package/dist/cjs/src/components/Stroke.js +36 -2
  15. package/dist/cjs/src/components/TextComponent.d.ts +26 -4
  16. package/dist/cjs/src/components/TextComponent.js +22 -0
  17. package/dist/cjs/src/components/lib.d.ts +3 -2
  18. package/dist/cjs/src/lib.d.ts +30 -0
  19. package/dist/cjs/src/lib.js +30 -0
  20. package/dist/cjs/src/rendering/RenderingStyle.d.ts +4 -4
  21. package/dist/cjs/src/rendering/TextRenderingStyle.d.ts +10 -10
  22. package/dist/cjs/src/rendering/lib.d.ts +2 -0
  23. package/dist/cjs/src/rendering/renderers/AbstractRenderer.d.ts +2 -2
  24. package/dist/cjs/src/rendering/renderers/CanvasRenderer.d.ts +2 -2
  25. package/dist/cjs/src/rendering/renderers/DummyRenderer.d.ts +2 -2
  26. package/dist/cjs/src/rendering/renderers/SVGRenderer.d.ts +2 -2
  27. package/dist/cjs/src/rendering/renderers/TextOnlyRenderer.d.ts +2 -2
  28. package/dist/cjs/src/toolbar/IconProvider.d.ts +2 -2
  29. package/dist/cjs/src/toolbar/localization.js +1 -1
  30. package/dist/cjs/src/toolbar/widgets/BaseWidget.js +1 -1
  31. package/dist/cjs/src/tools/SoundUITool.d.ts +24 -0
  32. package/dist/cjs/src/tools/SoundUITool.js +164 -0
  33. package/dist/cjs/src/tools/TextTool.d.ts +2 -2
  34. package/dist/cjs/src/tools/ToolController.js +6 -1
  35. package/dist/cjs/src/tools/lib.d.ts +1 -0
  36. package/dist/cjs/src/tools/lib.js +3 -1
  37. package/dist/cjs/src/tools/localization.d.ts +3 -0
  38. package/dist/cjs/src/tools/localization.js +3 -0
  39. package/dist/mjs/src/Color4.d.ts +8 -0
  40. package/dist/mjs/src/Color4.mjs +64 -0
  41. package/dist/mjs/src/Editor.d.ts +2 -2
  42. package/dist/mjs/src/Editor.mjs +2 -2
  43. package/dist/mjs/src/SVGLoader.mjs +8 -5
  44. package/dist/mjs/src/components/AbstractComponent.d.ts +2 -2
  45. package/dist/mjs/src/components/AbstractComponent.mjs +3 -3
  46. package/dist/mjs/src/components/ImageBackground.mjs +1 -1
  47. package/dist/mjs/src/components/RestylableComponent.d.ts +21 -2
  48. package/dist/mjs/src/components/Stroke.d.ts +35 -0
  49. package/dist/mjs/src/components/Stroke.mjs +36 -2
  50. package/dist/mjs/src/components/TextComponent.d.ts +26 -4
  51. package/dist/mjs/src/components/TextComponent.mjs +22 -0
  52. package/dist/mjs/src/components/lib.d.ts +3 -2
  53. package/dist/mjs/src/lib.d.ts +30 -0
  54. package/dist/mjs/src/lib.mjs +30 -0
  55. package/dist/mjs/src/rendering/RenderingStyle.d.ts +4 -4
  56. package/dist/mjs/src/rendering/TextRenderingStyle.d.ts +10 -10
  57. package/dist/mjs/src/rendering/lib.d.ts +2 -0
  58. package/dist/mjs/src/rendering/renderers/AbstractRenderer.d.ts +2 -2
  59. package/dist/mjs/src/rendering/renderers/CanvasRenderer.d.ts +2 -2
  60. package/dist/mjs/src/rendering/renderers/DummyRenderer.d.ts +2 -2
  61. package/dist/mjs/src/rendering/renderers/SVGRenderer.d.ts +2 -2
  62. package/dist/mjs/src/rendering/renderers/TextOnlyRenderer.d.ts +2 -2
  63. package/dist/mjs/src/toolbar/IconProvider.d.ts +2 -2
  64. package/dist/mjs/src/toolbar/localization.mjs +1 -1
  65. package/dist/mjs/src/toolbar/widgets/BaseWidget.mjs +1 -1
  66. package/dist/mjs/src/tools/SoundUITool.d.ts +24 -0
  67. package/dist/mjs/src/tools/SoundUITool.mjs +158 -0
  68. package/dist/mjs/src/tools/TextTool.d.ts +2 -2
  69. package/dist/mjs/src/tools/ToolController.mjs +6 -1
  70. package/dist/mjs/src/tools/lib.d.ts +1 -0
  71. package/dist/mjs/src/tools/lib.mjs +1 -0
  72. package/dist/mjs/src/tools/localization.d.ts +3 -0
  73. package/dist/mjs/src/tools/localization.mjs +3 -0
  74. package/package.json +6 -4
  75. package/src/Editor.css +2 -2
  76. package/src/tools/SoundUITool.css +15 -0
  77. package/src/tools/tools.css +4 -0
  78. package/src/Color4.test.ts +0 -40
  79. package/src/Color4.ts +0 -236
  80. package/src/Editor.loadFrom.test.ts +0 -24
  81. package/src/Editor.toSVG.test.ts +0 -111
  82. package/src/Editor.ts +0 -1122
  83. package/src/EditorImage.test.ts +0 -120
  84. package/src/EditorImage.ts +0 -603
  85. package/src/EventDispatcher.test.ts +0 -123
  86. package/src/EventDispatcher.ts +0 -71
  87. package/src/Pointer.ts +0 -127
  88. package/src/SVGLoader.test.ts +0 -114
  89. package/src/SVGLoader.ts +0 -511
  90. package/src/UndoRedoHistory.test.ts +0 -33
  91. package/src/UndoRedoHistory.ts +0 -102
  92. package/src/Viewport.ts +0 -319
  93. package/src/bundle/bundled.ts +0 -7
  94. package/src/commands/Command.ts +0 -45
  95. package/src/commands/Duplicate.ts +0 -48
  96. package/src/commands/Erase.ts +0 -74
  97. package/src/commands/SerializableCommand.ts +0 -49
  98. package/src/commands/UnresolvedCommand.ts +0 -37
  99. package/src/commands/invertCommand.ts +0 -51
  100. package/src/commands/lib.ts +0 -16
  101. package/src/commands/localization.ts +0 -47
  102. package/src/commands/uniteCommands.test.ts +0 -23
  103. package/src/commands/uniteCommands.ts +0 -135
  104. package/src/components/AbstractComponent.transformBy.test.ts +0 -22
  105. package/src/components/AbstractComponent.ts +0 -364
  106. package/src/components/ImageBackground.test.ts +0 -35
  107. package/src/components/ImageBackground.ts +0 -176
  108. package/src/components/ImageComponent.ts +0 -171
  109. package/src/components/RestylableComponent.ts +0 -142
  110. package/src/components/SVGGlobalAttributesObject.ts +0 -81
  111. package/src/components/Stroke.test.ts +0 -139
  112. package/src/components/Stroke.ts +0 -245
  113. package/src/components/TextComponent.test.ts +0 -99
  114. package/src/components/TextComponent.ts +0 -315
  115. package/src/components/UnknownSVGObject.test.ts +0 -10
  116. package/src/components/UnknownSVGObject.ts +0 -60
  117. package/src/components/builders/ArrowBuilder.ts +0 -107
  118. package/src/components/builders/FreehandLineBuilder.ts +0 -212
  119. package/src/components/builders/LineBuilder.ts +0 -77
  120. package/src/components/builders/PressureSensitiveFreehandLineBuilder.ts +0 -454
  121. package/src/components/builders/RectangleBuilder.ts +0 -74
  122. package/src/components/builders/types.ts +0 -15
  123. package/src/components/lib.ts +0 -25
  124. package/src/components/localization.ts +0 -22
  125. package/src/components/util/StrokeSmoother.ts +0 -293
  126. package/src/components/util/describeComponentList.ts +0 -18
  127. package/src/lib.ts +0 -37
  128. package/src/localization.ts +0 -34
  129. package/src/localizations/de.ts +0 -98
  130. package/src/localizations/en.ts +0 -8
  131. package/src/localizations/es.ts +0 -74
  132. package/src/localizations/getLocalizationTable.test.ts +0 -27
  133. package/src/localizations/getLocalizationTable.ts +0 -55
  134. package/src/math/LineSegment2.test.ts +0 -99
  135. package/src/math/LineSegment2.ts +0 -160
  136. package/src/math/Mat33.test.ts +0 -244
  137. package/src/math/Mat33.ts +0 -437
  138. package/src/math/Path.fromString.test.ts +0 -223
  139. package/src/math/Path.test.ts +0 -198
  140. package/src/math/Path.toString.test.ts +0 -77
  141. package/src/math/Path.ts +0 -790
  142. package/src/math/Rect2.test.ts +0 -204
  143. package/src/math/Rect2.ts +0 -315
  144. package/src/math/Triangle.ts +0 -29
  145. package/src/math/Vec2.test.ts +0 -30
  146. package/src/math/Vec2.ts +0 -18
  147. package/src/math/Vec3.test.ts +0 -44
  148. package/src/math/Vec3.ts +0 -218
  149. package/src/math/lib.ts +0 -15
  150. package/src/math/rounding.test.ts +0 -65
  151. package/src/math/rounding.ts +0 -156
  152. package/src/rendering/Display.ts +0 -249
  153. package/src/rendering/RenderingStyle.test.ts +0 -68
  154. package/src/rendering/RenderingStyle.ts +0 -55
  155. package/src/rendering/TextRenderingStyle.ts +0 -45
  156. package/src/rendering/caching/CacheRecord.test.ts +0 -49
  157. package/src/rendering/caching/CacheRecord.ts +0 -77
  158. package/src/rendering/caching/CacheRecordManager.ts +0 -71
  159. package/src/rendering/caching/RenderingCache.test.ts +0 -44
  160. package/src/rendering/caching/RenderingCache.ts +0 -66
  161. package/src/rendering/caching/RenderingCacheNode.ts +0 -405
  162. package/src/rendering/caching/testUtils.ts +0 -35
  163. package/src/rendering/caching/types.ts +0 -34
  164. package/src/rendering/lib.ts +0 -6
  165. package/src/rendering/localization.ts +0 -20
  166. package/src/rendering/renderers/AbstractRenderer.ts +0 -222
  167. package/src/rendering/renderers/CanvasRenderer.ts +0 -296
  168. package/src/rendering/renderers/DummyRenderer.test.ts +0 -42
  169. package/src/rendering/renderers/DummyRenderer.ts +0 -136
  170. package/src/rendering/renderers/SVGRenderer.ts +0 -354
  171. package/src/rendering/renderers/TextOnlyRenderer.ts +0 -70
  172. package/src/testing/beforeEachFile.ts +0 -8
  173. package/src/testing/createEditor.ts +0 -11
  174. package/src/testing/global.d.ts +0 -17
  175. package/src/testing/lib.ts +0 -3
  176. package/src/testing/loadExpectExtensions.ts +0 -25
  177. package/src/testing/sendPenEvent.ts +0 -31
  178. package/src/testing/sendTouchEvent.ts +0 -78
  179. package/src/toolbar/HTMLToolbar.ts +0 -492
  180. package/src/toolbar/IconProvider.ts +0 -736
  181. package/src/toolbar/lib.ts +0 -4
  182. package/src/toolbar/localization.ts +0 -106
  183. package/src/toolbar/makeColorInput.ts +0 -145
  184. package/src/toolbar/types.ts +0 -5
  185. package/src/toolbar/widgets/ActionButtonWidget.ts +0 -39
  186. package/src/toolbar/widgets/BaseToolWidget.ts +0 -56
  187. package/src/toolbar/widgets/BaseWidget.ts +0 -377
  188. package/src/toolbar/widgets/DocumentPropertiesWidget.ts +0 -167
  189. package/src/toolbar/widgets/EraserToolWidget.ts +0 -85
  190. package/src/toolbar/widgets/HandToolWidget.ts +0 -250
  191. package/src/toolbar/widgets/InsertImageWidget.ts +0 -223
  192. package/src/toolbar/widgets/OverflowWidget.ts +0 -92
  193. package/src/toolbar/widgets/PenToolWidget.ts +0 -288
  194. package/src/toolbar/widgets/SelectionToolWidget.ts +0 -190
  195. package/src/toolbar/widgets/TextToolWidget.ts +0 -145
  196. package/src/toolbar/widgets/lib.ts +0 -13
  197. package/src/tools/BaseTool.ts +0 -76
  198. package/src/tools/Eraser.test.ts +0 -103
  199. package/src/tools/Eraser.ts +0 -139
  200. package/src/tools/FindTool.ts +0 -152
  201. package/src/tools/PanZoom.test.ts +0 -310
  202. package/src/tools/PanZoom.ts +0 -520
  203. package/src/tools/PasteHandler.ts +0 -95
  204. package/src/tools/Pen.test.ts +0 -194
  205. package/src/tools/Pen.ts +0 -226
  206. package/src/tools/PipetteTool.ts +0 -55
  207. package/src/tools/SelectionTool/SelectAllShortcutHandler.ts +0 -28
  208. package/src/tools/SelectionTool/Selection.ts +0 -607
  209. package/src/tools/SelectionTool/SelectionHandle.ts +0 -108
  210. package/src/tools/SelectionTool/SelectionTool.test.ts +0 -261
  211. package/src/tools/SelectionTool/SelectionTool.ts +0 -480
  212. package/src/tools/SelectionTool/TransformMode.ts +0 -114
  213. package/src/tools/SelectionTool/types.ts +0 -11
  214. package/src/tools/TextTool.ts +0 -326
  215. package/src/tools/ToolController.ts +0 -178
  216. package/src/tools/ToolEnabledGroup.ts +0 -14
  217. package/src/tools/ToolSwitcherShortcut.ts +0 -39
  218. package/src/tools/ToolbarShortcutHandler.ts +0 -34
  219. package/src/tools/UndoRedoShortcut.test.ts +0 -56
  220. package/src/tools/UndoRedoShortcut.ts +0 -25
  221. package/src/tools/lib.ts +0 -21
  222. package/src/tools/localization.ts +0 -66
  223. package/src/types.ts +0 -234
  224. package/src/util/assertions.ts +0 -55
  225. package/src/util/fileToBase64.ts +0 -18
  226. package/src/util/untilNextAnimationFrame.ts +0 -9
  227. package/src/util/waitForTimeout.ts +0 -9
@@ -1,99 +0,0 @@
1
- import LineSegment2 from './LineSegment2';
2
- import { Vec2 } from './Vec2';
3
- import Mat33 from './Mat33';
4
-
5
-
6
- describe('Line2', () => {
7
- it('x and y axes should intersect at (0, 0)', () => {
8
- const xAxis = new LineSegment2(Vec2.of(-10, 0), Vec2.of(10, 0));
9
- const yAxis = new LineSegment2(Vec2.of(0, -10), Vec2.of(0, 10));
10
- expect(xAxis.intersection(yAxis)?.point).objEq(Vec2.zero);
11
- expect(yAxis.intersection(xAxis)?.point).objEq(Vec2.zero);
12
- });
13
-
14
- it('y = -2x + 2 and y = 2x - 2 should intersect at (1,0)', () => {
15
- // y = -4x + 2
16
- const line1 = new LineSegment2(Vec2.of(0, 2), Vec2.of(1, -2));
17
- // y = 4x - 2
18
- const line2 = new LineSegment2(Vec2.of(0, -2), Vec2.of(1, 2));
19
-
20
- expect(line1.intersection(line2)?.point).objEq(Vec2.of(0.5, 0));
21
- expect(line2.intersection(line1)?.point).objEq(Vec2.of(0.5, 0));
22
- });
23
-
24
- it('line from (10, 10) to (-100, 10) should intersect with the y-axis at t = 10', () => {
25
- const line1 = new LineSegment2(Vec2.of(10, 10), Vec2.of(-10, 10));
26
- // y = 2x - 2
27
- const line2 = new LineSegment2(Vec2.of(0, -2), Vec2.of(0, 200));
28
-
29
- expect(line1.intersection(line2)?.point).objEq(Vec2.of(0, 10));
30
-
31
- // t=10 implies 10 units along he line from (10, 10) to (-10, 10)
32
- expect(line1.intersection(line2)?.t).toBe(10);
33
-
34
- // Similarly, t = 12 implies 12 units above (0, -2) in the direction of (0, 200)
35
- expect(line2.intersection(line1)?.t).toBe(12);
36
- });
37
-
38
- it('y=2 and y=0 should not intersect', () => {
39
- const line1 = new LineSegment2(Vec2.of(-10, 2), Vec2.of(10, 2));
40
- const line2 = new LineSegment2(Vec2.of(-10, 0), Vec2.of(10, 0));
41
- expect(line1.intersection(line2)).toBeNull();
42
- expect(line2.intersection(line1)).toBeNull();
43
- });
44
-
45
- it('x=2 and x=-1 should not intersect', () => {
46
- const line1 = new LineSegment2(Vec2.of(2, -10), Vec2.of(2, 10));
47
- const line2 = new LineSegment2(Vec2.of(-1, 10), Vec2.of(-1, -10));
48
- expect(line1.intersection(line2)).toBeNull();
49
- expect(line2.intersection(line1)).toBeNull();
50
- });
51
-
52
- it('Line from (0, 0) to (1, 0) should not intersect line from (1.1, 0) to (2, 0)', () => {
53
- const line1 = new LineSegment2(Vec2.of(0, 0), Vec2.of(1, 0));
54
- const line2 = new LineSegment2(Vec2.of(1.1, 0), Vec2.of(2, 0));
55
- expect(line1.intersection(line2)).toBeNull();
56
- expect(line2.intersection(line1)).toBeNull();
57
- });
58
-
59
- it('Line segment from (1, 1) to (3, 1) should have length 2', () => {
60
- const segment = new LineSegment2(Vec2.of(1, 1), Vec2.of(3, 1));
61
- expect(segment.length).toBe(2);
62
- });
63
-
64
- it('(769.612,221.037)->(770.387,224.962) should not intersect (763.359,223.667)->(763.5493, 223.667)', () => {
65
- // Points taken from issue observed directly in editor
66
- const p1 = Vec2.of(769.6126045442547, 221.037877485765);
67
- const p2 = Vec2.of(770.3873954557453, 224.962122514235);
68
- const p3 = Vec2.of( 763.3590010920082, 223.66723995850086);
69
- const p4 = Vec2.of(763.5494167642871, 223.66723995850086);
70
-
71
- const line1 = new LineSegment2(p1, p2);
72
- const line2 = new LineSegment2(p3, p4);
73
- expect(line1.intersection(line2)).toBeNull();
74
- expect(line2.intersection(line1)).toBeNull();
75
- });
76
-
77
- it('Closest point to (0,0) on the line x = 1 should be (1,0)', () => {
78
- const line = new LineSegment2(Vec2.of(1, 100), Vec2.of(1, -100));
79
- expect(line.closestPointTo(Vec2.zero)).objEq(Vec2.of(1, 0));
80
- });
81
-
82
- it('Closest point from (-1,-2) to segment((1,1) -> (2,4)) should be (1,1)', () => {
83
- const line = new LineSegment2(Vec2.of(1, 1), Vec2.of(2, 4));
84
- expect(line.closestPointTo(Vec2.of(-1, -2))).objEq(Vec2.of(1, 1));
85
- });
86
-
87
- it('Closest point from (5,8) to segment((1,1) -> (2,4)) should be (2,4)', () => {
88
- const line = new LineSegment2(Vec2.of(1, 1), Vec2.of(2, 4));
89
- expect(line.closestPointTo(Vec2.of(5, 8))).objEq(Vec2.of(2, 4));
90
- });
91
-
92
- it('Should translate when translated by a translation matrix', () => {
93
- const line = new LineSegment2(Vec2.of(-1, 1), Vec2.of(2, 100));
94
- expect(line.transformedBy(Mat33.translation(Vec2.of(1, -2)))).toMatchObject({
95
- p1: Vec2.of(0, -1),
96
- p2: Vec2.of(3, 98),
97
- });
98
- });
99
- });
@@ -1,160 +0,0 @@
1
- import Mat33 from './Mat33';
2
- import Rect2 from './Rect2';
3
- import { Vec2, Point2 } from './Vec2';
4
-
5
- interface IntersectionResult {
6
- point: Point2;
7
- t: number;
8
- }
9
-
10
- export default class LineSegment2 {
11
- // invariant: ||direction|| = 1
12
- public readonly direction: Vec2;
13
- public readonly length: number;
14
- public readonly bbox;
15
-
16
- public constructor(
17
- private readonly point1: Point2,
18
- private readonly point2: Point2
19
- ) {
20
- this.bbox = Rect2.bboxOf([point1, point2]);
21
-
22
- this.direction = point2.minus(point1);
23
- this.length = this.direction.magnitude();
24
-
25
- // Normalize
26
- if (this.length > 0) {
27
- this.direction = this.direction.times(1 / this.length);
28
- }
29
- }
30
-
31
- // Accessors to make LineSegment2 compatible with bezier-js's
32
- // interface
33
- public get p1(): Point2 {
34
- return this.point1;
35
- }
36
-
37
- public get p2(): Point2 {
38
- return this.point2;
39
- }
40
-
41
- public get(t: number): Point2 {
42
- return this.point1.plus(this.direction.times(t));
43
- }
44
-
45
- public intersection(other: LineSegment2): IntersectionResult|null {
46
- // We want x₁(t) = x₂(t) and y₁(t) = y₂(t)
47
- // Observe that
48
- // x = this.point1.x + this.direction.x · t₁
49
- // = other.point1.x + other.direction.x · t₂
50
- // Thus,
51
- // t₁ = (x - this.point1.x) / this.direction.x
52
- // = (y - this.point1.y) / this.direction.y
53
- // and
54
- // t₂ = (x - other.point1.x) / other.direction.x
55
- // (and similarly for y)
56
- //
57
- // Letting o₁ₓ = this.point1.x, o₂ₓ = other.point1.x,
58
- // d₁ᵧ = this.direction.y, ...
59
- //
60
- // We can substitute these into the equations for y:
61
- // y = o₁ᵧ + d₁ᵧ · (x - o₁ₓ) / d₁ₓ
62
- // = o₂ᵧ + d₂ᵧ · (x - o₂ₓ) / d₂ₓ
63
- // ⇒ o₁ᵧ - o₂ᵧ = d₂ᵧ · (x - o₂ₓ) / d₂ₓ - d₁ᵧ · (x - o₁ₓ) / d₁ₓ
64
- // = (d₂ᵧ/d₂ₓ)(x) - (d₂ᵧ/d₂ₓ)(o₂ₓ) - (d₁ᵧ/d₁ₓ)(x) + (d₁ᵧ/d₁ₓ)(o₁ₓ)
65
- // = (x)(d₂ᵧ/d₂ₓ - d₁ᵧ/d₁ₓ) - (d₂ᵧ/d₂ₓ)(o₂ₓ) + (d₁ᵧ/d₁ₓ)(o₁ₓ)
66
- // ⇒ (x)(d₂ᵧ/d₂ₓ - d₁ᵧ/d₁ₓ) = o₁ᵧ - o₂ᵧ + (d₂ᵧ/d₂ₓ)(o₂ₓ) - (d₁ᵧ/d₁ₓ)(o₁ₓ)
67
- // ⇒ x = (o₁ᵧ - o₂ᵧ + (d₂ᵧ/d₂ₓ)(o₂ₓ) - (d₁ᵧ/d₁ₓ)(o₁ₓ))/(d₂ᵧ/d₂ₓ - d₁ᵧ/d₁ₓ)
68
- // = (d₁ₓd₂ₓ)(o₁ᵧ - o₂ᵧ + (d₂ᵧ/d₂ₓ)(o₂ₓ) - (d₁ᵧ/d₁ₓ)(o₁ₓ))/(d₂ᵧd₁ₓ - d₁ᵧd₂ₓ)
69
- // = ((o₁ᵧ - o₂ᵧ)((d₁ₓd₂ₓ)) + (d₂ᵧd₁ₓ)(o₂ₓ) - (d₁ᵧd₂ₓ)(o₁ₓ))/(d₂ᵧd₁ₓ - d₁ᵧd₂ₓ)
70
- // ⇒ y = o₁ᵧ + d₁ᵧ · (x - o₁ₓ) / d₁ₓ = ...
71
- let resultPoint, resultT;
72
- if (this.direction.x === 0) {
73
- // Vertical line: Where does the other have x = this.point1.x?
74
- // x = o₁ₓ = o₂ₓ + d₂ₓ · (y - o₂ᵧ) / d₂ᵧ
75
- // ⇒ (o₁ₓ - o₂ₓ)(d₂ᵧ/d₂ₓ) + o₂ᵧ = y
76
-
77
- // Avoid division by zero
78
- if (other.direction.x === 0 || this.direction.y === 0) {
79
- return null;
80
- }
81
-
82
- const xIntersect = this.point1.x;
83
- const yIntersect =
84
- (this.point1.x - other.point1.x) * other.direction.y / other.direction.x + other.point1.y;
85
- resultPoint = Vec2.of(xIntersect, yIntersect);
86
- resultT = (yIntersect - this.point1.y) / this.direction.y;
87
- } else {
88
- // From above,
89
- // x = ((o₁ᵧ - o₂ᵧ)(d₁ₓd₂ₓ) + (d₂ᵧd₁ₓ)(o₂ₓ) - (d₁ᵧd₂ₓ)(o₁ₓ))/(d₂ᵧd₁ₓ - d₁ᵧd₂ₓ)
90
- const numerator = (
91
- (this.point1.y - other.point1.y) * this.direction.x * other.direction.x
92
- + this.direction.x * other.direction.y * other.point1.x
93
- - this.direction.y * other.direction.x * this.point1.x
94
- );
95
- const denominator = (
96
- other.direction.y * this.direction.x
97
- - this.direction.y * other.direction.x
98
- );
99
-
100
- // Avoid dividing by zero. It means there is no intersection
101
- if (denominator === 0) {
102
- return null;
103
- }
104
-
105
- const xIntersect = numerator / denominator;
106
- const t1 = (xIntersect - this.point1.x) / this.direction.x;
107
- const yIntersect = this.point1.y + this.direction.y * t1;
108
- resultPoint = Vec2.of(xIntersect, yIntersect);
109
- resultT = (xIntersect - this.point1.x) / this.direction.x;
110
- }
111
-
112
- // Ensure the result is in this/the other segment.
113
- const resultToP1 = resultPoint.minus(this.point1).magnitude();
114
- const resultToP2 = resultPoint.minus(this.point2).magnitude();
115
- const resultToP3 = resultPoint.minus(other.point1).magnitude();
116
- const resultToP4 = resultPoint.minus(other.point2).magnitude();
117
- if (resultToP1 > this.length
118
- || resultToP2 > this.length
119
- || resultToP3 > other.length
120
- || resultToP4 > other.length) {
121
- return null;
122
- }
123
-
124
- return {
125
- point: resultPoint,
126
- t: resultT,
127
- };
128
- }
129
-
130
- public intersects(other: LineSegment2) {
131
- return this.intersection(other) !== null;
132
- }
133
-
134
- // Returns the closest point on this to [target]
135
- public closestPointTo(target: Point2) {
136
- // Distance from P1 along this' direction.
137
- const projectedDistFromP1 = target.minus(this.p1).dot(this.direction);
138
- const projectedDistFromP2 = this.length - projectedDistFromP1;
139
-
140
- const projection = this.p1.plus(this.direction.times(projectedDistFromP1));
141
-
142
- if (projectedDistFromP1 > 0 && projectedDistFromP1 < this.length) {
143
- return projection;
144
- }
145
-
146
- if (Math.abs(projectedDistFromP2) < Math.abs(projectedDistFromP1)) {
147
- return this.p2;
148
- } else {
149
- return this.p1;
150
- }
151
- }
152
-
153
- public transformedBy(affineTransfm: Mat33): LineSegment2 {
154
- return new LineSegment2(affineTransfm.transformVec2(this.p1), affineTransfm.transformVec2(this.p2));
155
- }
156
-
157
- public toString() {
158
- return `LineSegment(${this.p1.toString()}, ${this.p2.toString()})`;
159
- }
160
- }
@@ -1,244 +0,0 @@
1
- import Mat33 from './Mat33';
2
- import { Point2, Vec2 } from './Vec2';
3
- import Vec3 from './Vec3';
4
-
5
-
6
- describe('Mat33 tests', () => {
7
- it('equality', () => {
8
- expect(Mat33.identity).objEq(Mat33.identity);
9
- expect(new Mat33(
10
- 0.1, 0.2, 0.3,
11
- 0.4, 0.5, 0.6,
12
- 0.7, 0.8, -0.9
13
- )).objEq(new Mat33(
14
- 0.2, 0.1, 0.4,
15
- 0.5, 0.5, 0.7,
16
- 0.7, 0.8, -0.9
17
- ), 0.2);
18
- });
19
-
20
- it('transposition', () => {
21
- expect(Mat33.identity.transposed()).objEq(Mat33.identity);
22
- expect(new Mat33(
23
- 1, 2, 0,
24
- 0, 0, 0,
25
- 0, 1, 0
26
- ).transposed()).objEq(new Mat33(
27
- 1, 0, 0,
28
- 2, 0, 1,
29
- 0, 0, 0
30
- ));
31
- });
32
-
33
- it('multiplication', () => {
34
- const M = new Mat33(
35
- 1, 2, 3,
36
- 4, 5, 6,
37
- 7, 8, 9
38
- );
39
-
40
- expect(Mat33.identity.rightMul(Mat33.identity)).objEq(Mat33.identity);
41
- expect(M.rightMul(Mat33.identity)).objEq(M);
42
- expect(M.rightMul(new Mat33(
43
- 1, 0, 0,
44
- 0, 2, 0,
45
- 0, 0, 1
46
- ))).objEq(new Mat33(
47
- 1, 4, 3,
48
- 4, 10, 6,
49
- 7, 16, 9
50
- ));
51
- expect(M.rightMul(new Mat33(
52
- 2, 0, 1,
53
- 0, 1, 0,
54
- 0, 0, 3
55
- ))).objEq(new Mat33(
56
- 2, 2, 10,
57
- 8, 5, 22,
58
- 14, 8, 34
59
- ));
60
- });
61
-
62
- it('the inverse of the identity matrix should be the identity matrix', () => {
63
- const fuzz = 0.01;
64
- expect(Mat33.identity.inverse()).objEq(Mat33.identity, fuzz);
65
-
66
- const M = new Mat33(
67
- 1, 2, 3,
68
- 4, 1, 0,
69
- 2, 3, 0
70
- );
71
- expect(M.inverse().rightMul(M)).objEq(Mat33.identity, fuzz);
72
- });
73
-
74
- it('90 degree z-rotation matricies should rotate 90 degrees counter clockwise', () => {
75
- const fuzz = 0.001;
76
-
77
- const M = Mat33.zRotation(Math.PI / 2);
78
- const rotated = M.transformVec2(Vec2.unitX);
79
- expect(rotated).objEq(Vec2.unitY, fuzz);
80
- expect(M.transformVec2(rotated)).objEq(Vec2.unitX.times(-1), fuzz);
81
- });
82
-
83
- it('z-rotation matricies should preserve the given origin', () => {
84
- const testPairs: Array<[number, Vec2]> = [
85
- [ Math.PI / 2, Vec2.zero ],
86
- [ -Math.PI / 2, Vec2.zero ],
87
- [ -Math.PI / 2, Vec2.of(10, 10) ],
88
- ];
89
-
90
- for (const [ angle, center ] of testPairs) {
91
- expect(Mat33.zRotation(angle, center).transformVec2(center)).objEq(center);
92
- }
93
- });
94
-
95
- it('translation matricies should translate Vec2s', () => {
96
- const fuzz = 0.01;
97
-
98
- const M = Mat33.translation(Vec2.of(1, -4));
99
- expect(M.transformVec2(Vec2.of(0, 0))).objEq(Vec2.of(1, -4), fuzz);
100
- expect(M.transformVec2(Vec2.of(-1, 3))).objEq(Vec2.of(0, -1), fuzz);
101
- });
102
-
103
- it('scaling matricies should scale about the provided center', () => {
104
- const fuzz = 0.01;
105
-
106
- const center = Vec2.of(1, -4);
107
- const M = Mat33.scaling2D(2, center);
108
- expect(M.transformVec2(center)).objEq(center, fuzz);
109
- expect(M.transformVec2(Vec2.of(0, 0))).objEq(Vec2.of(-1, 4), fuzz);
110
- });
111
-
112
- it('calling inverse on singular matricies should result in the identity matrix', () => {
113
- const fuzz = 0.001;
114
- const singularMat = Mat33.ofRows(
115
- Vec3.of(0, 0, 1),
116
- Vec3.of(0, 1, 0),
117
- Vec3.of(0, 1, 1)
118
- );
119
- expect(singularMat.invertable()).toBe(false);
120
- expect(singularMat.inverse()).objEq(Mat33.identity, fuzz);
121
- });
122
-
123
- it('z-rotation matricies should be invertable', () => {
124
- const fuzz = 0.01;
125
- const M = Mat33.zRotation(-0.2617993877991494, Vec2.of(481, 329.5));
126
- expect(
127
- M.inverse().transformVec2(M.transformVec2(Vec2.unitX))
128
- ).objEq(Vec2.unitX, fuzz);
129
- expect(M.invertable());
130
-
131
- const starterTransform = new Mat33(
132
- -0.2588190451025205, -0.9659258262890688, 923.7645204565603,
133
- 0.9659258262890688, -0.2588190451025205, -49.829447083761465,
134
- 0, 0, 1
135
- );
136
- expect(starterTransform.invertable()).toBe(true);
137
-
138
- const fullTransform = starterTransform.rightMul(M);
139
- const fullTransformInverse = fullTransform.inverse();
140
- expect(fullTransform.invertable()).toBe(true);
141
-
142
- expect(
143
- fullTransformInverse.rightMul(fullTransform)
144
- ).objEq(Mat33.identity, fuzz);
145
-
146
- expect(
147
- fullTransform.transformVec2(fullTransformInverse.transformVec2(Vec2.unitX))
148
- ).objEq(Vec2.unitX, fuzz);
149
-
150
- expect(
151
- fullTransformInverse.transformVec2(fullTransform.transformVec2(Vec2.unitX))
152
- ).objEq(Vec2.unitX, fuzz);
153
- });
154
-
155
- it('z-rotation matrix inverses should undo the z-rotation', () => {
156
- const testCases: Array<[ number, Point2 ]> = [
157
- [ Math.PI / 2, Vec2.zero ],
158
- [ Math.PI, Vec2.of(1, 1) ],
159
- [ -Math.PI, Vec2.of(1, 1) ],
160
- [ -Math.PI * 2, Vec2.of(1, 1) ],
161
- [ -Math.PI * 2, Vec2.of(123, 456) ],
162
- [ -Math.PI / 4, Vec2.of(123, 456) ],
163
- [ 0.1, Vec2.of(1, 2) ],
164
- ];
165
-
166
- const fuzz = 0.00001;
167
- for (const [ angle, center ] of testCases) {
168
- const mat = Mat33.zRotation(angle, center);
169
- expect(mat.inverse().rightMul(mat)).objEq(Mat33.identity, fuzz);
170
- expect(mat.rightMul(mat.inverse())).objEq(Mat33.identity, fuzz);
171
- }
172
- });
173
-
174
- it('z-rotation should preserve given origin', () => {
175
- const testCases: Array<[ number, Point2 ]> = [
176
- [ 6.205048847547065, Vec2.of(75.16363373235318, 104.29870408043762) ],
177
- [ 1.234, Vec2.of(-56, 789) ],
178
- [ -Math.PI, Vec2.of(-56, 789) ],
179
- [ -Math.PI / 2, Vec2.of(-0.001, 1.0002) ],
180
- ];
181
-
182
- for (const [angle, rotationOrigin] of testCases) {
183
- expect(Mat33.zRotation(angle, rotationOrigin).transformVec2(rotationOrigin)).objEq(rotationOrigin);
184
- }
185
- });
186
-
187
- it('should correctly apply a mapping to all components', () => {
188
- expect(
189
- new Mat33(
190
- 1, 2, 3,
191
- 4, 5, 6,
192
- 7, 8, 9,
193
- ).mapEntries(component => component - 1)
194
- ).toMatchObject(new Mat33(
195
- 0, 1, 2,
196
- 3, 4, 5,
197
- 6, 7, 8,
198
- ));
199
- });
200
-
201
- it('should convert CSS matrix(...) strings to matricies', () => {
202
- // From MDN:
203
- // ⎡ a c e ⎤
204
- // ⎢ b d f ⎥ = matrix(a,b,c,d,e,f)
205
- // ⎣ 0 0 1 ⎦
206
- const identity = Mat33.fromCSSMatrix('matrix(1, 0, 0, 1, 0, 0)');
207
- expect(identity).objEq(Mat33.identity);
208
- expect(Mat33.fromCSSMatrix('matrix(1, 2, 3, 4, 5, 6)')).objEq(new Mat33(
209
- 1, 3, 5,
210
- 2, 4, 6,
211
- 0, 0, 1,
212
- ));
213
- expect(Mat33.fromCSSMatrix('matrix(1e2, 2, 3, 4, 5, 6)')).objEq(new Mat33(
214
- 1e2, 3, 5,
215
- 2, 4, 6,
216
- 0, 0, 1,
217
- ));
218
- expect(Mat33.fromCSSMatrix('matrix(1.6, 2, .3, 4, 5, 6)')).objEq(new Mat33(
219
- 1.6, .3, 5,
220
- 2, 4, 6,
221
- 0, 0, 1,
222
- ));
223
- expect(Mat33.fromCSSMatrix('matrix(-1, 2, 3.E-2, 4, -5.123, -6.5)')).objEq(new Mat33(
224
- -1, 0.03, -5.123,
225
- 2, 4, -6.5,
226
- 0, 0, 1,
227
- ));
228
- expect(Mat33.fromCSSMatrix('matrix(1.6,\n\t2, .3, 4, 5, 6)')).objEq(new Mat33(
229
- 1.6, .3, 5,
230
- 2, 4, 6,
231
- 0, 0, 1,
232
- ));
233
- expect(Mat33.fromCSSMatrix('matrix(1.6,2, .3E-2, 4, 5, 6)')).objEq(new Mat33(
234
- 1.6, 3e-3, 5,
235
- 2, 4, 6,
236
- 0, 0, 1,
237
- ));
238
- expect(Mat33.fromCSSMatrix('matrix(-1, 2e6, 3E-2,-5.123, -6.5e-1, 0.01)')).objEq(new Mat33(
239
- -1, 3E-2, -6.5e-1,
240
- 2e6, -5.123, 0.01,
241
- 0, 0, 1,
242
- ));
243
- });
244
- });