@excalidraw/math 0.18.0-9036812b6 → 0.18.0-9ba0f5d

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 (226) hide show
  1. package/dist/dev/index.js +168 -157
  2. package/dist/dev/index.js.map +4 -4
  3. package/dist/prod/index.js +1 -1
  4. package/dist/types/common/src/bounds.d.ts +10 -0
  5. package/dist/types/common/src/colors.d.ts +59 -39
  6. package/dist/types/common/src/constants.d.ts +33 -26
  7. package/dist/types/common/src/editorInterface.d.ts +34 -0
  8. package/dist/types/common/src/font-metadata.d.ts +1 -3
  9. package/dist/types/common/src/index.d.ts +2 -0
  10. package/dist/types/common/src/keys.d.ts +1 -1
  11. package/dist/types/common/src/utility-types.d.ts +0 -1
  12. package/dist/types/common/src/utils.d.ts +49 -34
  13. package/dist/types/element/src/Scene.d.ts +9 -5
  14. package/dist/types/element/src/binding.d.ts +58 -44
  15. package/dist/types/element/src/bounds.d.ts +3 -11
  16. package/dist/types/element/src/collision.d.ts +6 -2
  17. package/dist/types/element/src/comparisons.d.ts +7 -7
  18. package/dist/types/element/src/delta.d.ts +6 -3
  19. package/dist/types/element/src/dragElements.d.ts +3 -3
  20. package/dist/types/element/src/duplicate.d.ts +3 -3
  21. package/dist/types/element/src/fractionalIndex.d.ts +2 -2
  22. package/dist/types/element/src/frame.d.ts +5 -1
  23. package/dist/types/element/src/heading.d.ts +2 -1
  24. package/dist/types/element/src/image.d.ts +1 -11
  25. package/dist/types/element/src/index.d.ts +2 -3
  26. package/dist/types/element/src/linearElementEditor.d.ts +16 -20
  27. package/dist/types/element/src/mutateElement.d.ts +3 -1
  28. package/dist/types/element/src/newElement.d.ts +6 -6
  29. package/dist/types/element/src/positionElementsOnGrid.d.ts +2 -0
  30. package/dist/types/element/src/renderElement.d.ts +4 -7
  31. package/dist/types/element/src/resizeElements.d.ts +10 -10
  32. package/dist/types/element/src/resizeTest.d.ts +6 -5
  33. package/dist/types/element/src/selection.d.ts +3 -7
  34. package/dist/types/element/src/shape.d.ts +8 -7
  35. package/dist/types/element/src/store.d.ts +6 -1
  36. package/dist/types/element/src/textElement.d.ts +1 -1
  37. package/dist/types/element/src/textMeasurements.d.ts +1 -3
  38. package/dist/types/{excalidraw/data → element/src}/transform.d.ts +3 -3
  39. package/dist/types/element/src/transformHandles.d.ts +8 -27
  40. package/dist/types/element/src/typeChecks.d.ts +4 -7
  41. package/dist/types/element/src/types.d.ts +7 -11
  42. package/dist/types/element/src/utils.d.ts +5 -2
  43. package/dist/types/element/src/zindex.d.ts +7 -1
  44. package/dist/types/excalidraw/actions/actionAddToLibrary.d.ts +138 -181
  45. package/dist/types/excalidraw/actions/actionAlign.d.ts +6 -7
  46. package/dist/types/excalidraw/actions/actionBoundText.d.ts +67 -96
  47. package/dist/types/excalidraw/actions/actionCanvas.d.ts +402 -758
  48. package/dist/types/excalidraw/actions/actionClipboard.d.ts +103 -873
  49. package/dist/types/excalidraw/actions/actionCropEditor.d.ts +32 -47
  50. package/dist/types/excalidraw/actions/actionDeleteSelected.d.ts +107 -156
  51. package/dist/types/excalidraw/actions/actionDistribute.d.ts +2 -3
  52. package/dist/types/excalidraw/actions/actionDuplicateSelection.d.ts +3 -4
  53. package/dist/types/excalidraw/actions/actionElementLink.d.ts +47 -64
  54. package/dist/types/excalidraw/actions/actionElementLock.d.ts +66 -95
  55. package/dist/types/excalidraw/actions/actionEmbeddable.d.ts +49 -64
  56. package/dist/types/excalidraw/actions/actionExport.d.ts +144 -1104
  57. package/dist/types/excalidraw/actions/actionFinalize.d.ts +10 -394
  58. package/dist/types/excalidraw/actions/actionFlip.d.ts +2 -3
  59. package/dist/types/excalidraw/actions/actionFrame.d.ts +213 -308
  60. package/dist/types/excalidraw/actions/actionGroup.d.ts +70 -107
  61. package/dist/types/excalidraw/actions/actionLinearEditor.d.ts +123 -178
  62. package/dist/types/excalidraw/actions/actionLink.d.ts +49 -64
  63. package/dist/types/excalidraw/actions/actionMenu.d.ts +42 -425
  64. package/dist/types/excalidraw/actions/actionNavigate.d.ts +18 -359
  65. package/dist/types/excalidraw/actions/actionProperties.d.ts +129 -2488
  66. package/dist/types/excalidraw/actions/actionSelectAll.d.ts +35 -54
  67. package/dist/types/excalidraw/actions/actionStyles.d.ts +32 -47
  68. package/dist/types/excalidraw/actions/actionTextAutoResize.d.ts +3 -3
  69. package/dist/types/excalidraw/actions/actionToggleGridMode.d.ts +49 -64
  70. package/dist/types/excalidraw/actions/actionToggleObjectsSnapMode.d.ts +49 -64
  71. package/dist/types/excalidraw/actions/actionToggleSearchMenu.d.ts +41 -58
  72. package/dist/types/excalidraw/actions/actionToggleShapeSwitch.d.ts +1 -1
  73. package/dist/types/excalidraw/actions/actionToggleStats.d.ts +48 -63
  74. package/dist/types/excalidraw/actions/actionToggleViewMode.d.ts +49 -64
  75. package/dist/types/excalidraw/actions/actionToggleZenMode.d.ts +49 -64
  76. package/dist/types/excalidraw/actions/actionZindex.d.ts +8 -9
  77. package/dist/types/excalidraw/actions/index.d.ts +2 -2
  78. package/dist/types/excalidraw/actions/register.d.ts +2 -2
  79. package/dist/types/excalidraw/actions/types.d.ts +4 -4
  80. package/dist/types/excalidraw/appState.d.ts +20 -15
  81. package/dist/types/excalidraw/clipboard.d.ts +55 -16
  82. package/dist/types/excalidraw/components/Actions.d.ts +22 -5
  83. package/dist/types/excalidraw/components/App.d.ts +67 -65
  84. package/dist/types/excalidraw/components/Card.d.ts +1 -3
  85. package/dist/types/excalidraw/components/ColorPicker/ColorInput.d.ts +2 -4
  86. package/dist/types/excalidraw/components/ColorPicker/ColorPicker.d.ts +0 -1
  87. package/dist/types/excalidraw/components/ColorPicker/Picker.d.ts +2 -0
  88. package/dist/types/excalidraw/components/ColorPicker/PickerColorList.d.ts +2 -1
  89. package/dist/types/excalidraw/components/ColorPicker/ShadeList.d.ts +2 -1
  90. package/dist/types/excalidraw/components/ColorPicker/colorPickerUtils.d.ts +0 -1
  91. package/dist/types/excalidraw/components/ColorPicker/keyboardNavHandlers.d.ts +0 -1
  92. package/dist/types/excalidraw/components/CommandPalette/CommandPalette.d.ts +1 -0
  93. package/dist/types/excalidraw/components/CommandPalette/types.d.ts +1 -3
  94. package/dist/types/excalidraw/components/ConvertElementTypePopup.d.ts +2 -2
  95. package/dist/types/excalidraw/components/DarkModeToggle.d.ts +1 -1
  96. package/dist/types/excalidraw/components/DefaultSidebar.d.ts +9 -13
  97. package/dist/types/excalidraw/components/ElementLinkDialog.d.ts +1 -1
  98. package/dist/types/excalidraw/components/Ellipsify.d.ts +1 -2
  99. package/dist/types/excalidraw/components/ErrorDialog.d.ts +1 -1
  100. package/dist/types/excalidraw/components/ExcalidrawLogo.d.ts +1 -2
  101. package/dist/types/excalidraw/components/EyeDropper.d.ts +0 -1
  102. package/dist/types/excalidraw/components/FilledButton.d.ts +2 -1
  103. package/dist/types/excalidraw/components/FontPicker/FontPicker.d.ts +2 -1
  104. package/dist/types/excalidraw/components/FontPicker/FontPickerTrigger.d.ts +3 -1
  105. package/dist/types/excalidraw/components/FontPicker/keyboardNavHandlers.d.ts +0 -1
  106. package/dist/types/excalidraw/components/HelpDialog.d.ts +1 -1
  107. package/dist/types/excalidraw/components/HintViewer.d.ts +4 -3
  108. package/dist/types/excalidraw/components/InlineIcon.d.ts +3 -1
  109. package/dist/types/excalidraw/components/LayerUI.d.ts +2 -1
  110. package/dist/types/excalidraw/components/LibraryMenuControlButtons.d.ts +1 -1
  111. package/dist/types/excalidraw/components/LibraryMenuHeaderContent.d.ts +1 -2
  112. package/dist/types/excalidraw/components/LibraryMenuSection.d.ts +1 -1
  113. package/dist/types/excalidraw/components/LibraryUnit.d.ts +2 -3
  114. package/dist/types/excalidraw/components/LoadingMessage.d.ts +0 -1
  115. package/dist/types/excalidraw/components/MobileMenu.d.ts +3 -5
  116. package/dist/types/excalidraw/components/MobileToolBar.d.ts +10 -0
  117. package/dist/types/excalidraw/components/Modal.d.ts +0 -1
  118. package/dist/types/excalidraw/components/OverwriteConfirm/OverwriteConfirm.d.ts +1 -1
  119. package/dist/types/excalidraw/components/Popover.d.ts +2 -1
  120. package/dist/types/excalidraw/components/PropertiesPopover.d.ts +1 -0
  121. package/dist/types/excalidraw/components/RadioGroup.d.ts +0 -1
  122. package/dist/types/excalidraw/components/RadioSelection.d.ts +4 -4
  123. package/dist/types/excalidraw/components/ScrollableList.d.ts +0 -1
  124. package/dist/types/excalidraw/components/Sidebar/Sidebar.d.ts +15 -21
  125. package/dist/types/excalidraw/components/Sidebar/SidebarHeader.d.ts +1 -1
  126. package/dist/types/excalidraw/components/Sidebar/SidebarTab.d.ts +1 -2
  127. package/dist/types/excalidraw/components/Sidebar/SidebarTabTrigger.d.ts +1 -2
  128. package/dist/types/excalidraw/components/Sidebar/SidebarTabTriggers.d.ts +1 -2
  129. package/dist/types/excalidraw/components/Sidebar/SidebarTabs.d.ts +1 -2
  130. package/dist/types/excalidraw/components/Spinner.d.ts +4 -4
  131. package/dist/types/excalidraw/components/Stats/CanvasGrid.d.ts +0 -1
  132. package/dist/types/excalidraw/components/Stats/Collapsible.d.ts +0 -1
  133. package/dist/types/excalidraw/components/Stats/DragInput.d.ts +0 -1
  134. package/dist/types/excalidraw/components/Stats/index.d.ts +7 -8
  135. package/dist/types/excalidraw/components/Stats/utils.d.ts +1 -1
  136. package/dist/types/excalidraw/components/TTDDialog/Chat/ChatHistoryMenu.d.ts +15 -0
  137. package/dist/types/excalidraw/components/TTDDialog/Chat/ChatInterface.d.ts +27 -0
  138. package/dist/types/excalidraw/components/TTDDialog/Chat/ChatMessage.d.ts +14 -0
  139. package/dist/types/excalidraw/components/TTDDialog/Chat/TTDChatPanel.d.ts +26 -0
  140. package/dist/types/excalidraw/components/TTDDialog/Chat/index.d.ts +3 -0
  141. package/dist/types/excalidraw/components/TTDDialog/Chat/useChatAgent.d.ts +8 -0
  142. package/dist/types/excalidraw/components/TTDDialog/MermaidToExcalidraw.d.ts +3 -2
  143. package/dist/types/excalidraw/components/TTDDialog/TTDContext.d.ts +13 -0
  144. package/dist/types/excalidraw/components/TTDDialog/TTDDialog.d.ts +4 -25
  145. package/dist/types/excalidraw/components/TTDDialog/TTDDialogOutput.d.ts +2 -2
  146. package/dist/types/excalidraw/components/TTDDialog/TTDDialogPanel.d.ts +13 -9
  147. package/dist/types/excalidraw/components/TTDDialog/TTDDialogTab.d.ts +1 -2
  148. package/dist/types/excalidraw/components/TTDDialog/TTDDialogTabTrigger.d.ts +1 -2
  149. package/dist/types/excalidraw/components/TTDDialog/TTDDialogTabTriggers.d.ts +1 -2
  150. package/dist/types/excalidraw/components/TTDDialog/TTDDialogTrigger.d.ts +2 -3
  151. package/dist/types/excalidraw/components/TTDDialog/TTDPreviewPanel.d.ts +9 -0
  152. package/dist/types/excalidraw/components/TTDDialog/TextToDiagram.d.ts +8 -0
  153. package/dist/types/excalidraw/components/TTDDialog/common.d.ts +16 -16
  154. package/dist/types/excalidraw/components/TTDDialog/hooks/useChatManagement.d.ts +13 -0
  155. package/dist/types/excalidraw/components/TTDDialog/hooks/useMermaidRenderer.d.ts +14 -0
  156. package/dist/types/excalidraw/components/TTDDialog/hooks/useTextGeneration.d.ts +7 -0
  157. package/dist/types/excalidraw/components/TTDDialog/types.d.ts +91 -0
  158. package/dist/types/excalidraw/components/TTDDialog/useTTDChatStorage.d.ts +22 -0
  159. package/dist/types/excalidraw/components/TTDDialog/utils/TTDStreamFetch.d.ts +24 -0
  160. package/dist/types/excalidraw/components/TTDDialog/utils/chat.d.ts +10 -0
  161. package/dist/types/excalidraw/components/TTDDialog/utils/mermaidValidation.d.ts +1 -0
  162. package/dist/types/excalidraw/components/TextField.d.ts +1 -0
  163. package/dist/types/excalidraw/components/Toast.d.ts +3 -3
  164. package/dist/types/excalidraw/components/ToolPopover.d.ts +25 -0
  165. package/dist/types/excalidraw/components/Trans.d.ts +2 -2
  166. package/dist/types/excalidraw/components/canvases/InteractiveCanvas.d.ts +6 -3
  167. package/dist/types/excalidraw/components/dropdownMenu/DropdownMenu.d.ts +30 -32
  168. package/dist/types/excalidraw/components/dropdownMenu/DropdownMenuContent.d.ts +6 -5
  169. package/dist/types/excalidraw/components/dropdownMenu/DropdownMenuGroup.d.ts +3 -3
  170. package/dist/types/excalidraw/components/dropdownMenu/DropdownMenuItem.d.ts +10 -18
  171. package/dist/types/excalidraw/components/dropdownMenu/DropdownMenuItemContent.d.ts +3 -3
  172. package/dist/types/excalidraw/components/dropdownMenu/DropdownMenuItemContentRadio.d.ts +0 -1
  173. package/dist/types/excalidraw/components/dropdownMenu/DropdownMenuItemCustom.d.ts +2 -2
  174. package/dist/types/excalidraw/components/dropdownMenu/DropdownMenuItemLink.d.ts +6 -6
  175. package/dist/types/excalidraw/components/dropdownMenu/DropdownMenuTrigger.d.ts +3 -4
  176. package/dist/types/excalidraw/components/dropdownMenu/common.d.ts +1 -1
  177. package/dist/types/excalidraw/components/hoc/withInternalFallback.d.ts +1 -1
  178. package/dist/types/excalidraw/components/hyperlink/helpers.d.ts +1 -1
  179. package/dist/types/excalidraw/components/icons.d.ts +26 -12
  180. package/dist/types/excalidraw/components/live-collaboration/LiveCollaborationTrigger.d.ts +4 -3
  181. package/dist/types/excalidraw/components/main-menu/DefaultItems.d.ts +2 -2
  182. package/dist/types/excalidraw/components/main-menu/MainMenu.d.ts +25 -30
  183. package/dist/types/excalidraw/components/shapes.d.ts +129 -1
  184. package/dist/types/excalidraw/components/welcome-screen/WelcomeScreen.Center.d.ts +6 -6
  185. package/dist/types/excalidraw/components/welcome-screen/WelcomeScreen.d.ts +15 -16
  186. package/dist/types/excalidraw/data/blob.d.ts +324 -10
  187. package/dist/types/excalidraw/data/encode.d.ts +4 -4
  188. package/dist/types/excalidraw/data/encryption.d.ts +5 -5
  189. package/dist/types/excalidraw/data/filesystem.d.ts +2 -2
  190. package/dist/types/excalidraw/data/index.d.ts +3 -3
  191. package/dist/types/excalidraw/data/json.d.ts +159 -2
  192. package/dist/types/excalidraw/data/library.d.ts +24 -9
  193. package/dist/types/excalidraw/data/restore.d.ts +27 -7
  194. package/dist/types/excalidraw/data/types.d.ts +4 -1
  195. package/dist/types/excalidraw/editor-jotai.d.ts +11 -11
  196. package/dist/types/excalidraw/errors.d.ts +14 -0
  197. package/dist/types/excalidraw/hooks/useLibraryItemSvg.d.ts +1 -1
  198. package/dist/types/excalidraw/hooks/useOutsideClick.d.ts +4 -3
  199. package/dist/types/excalidraw/hooks/useScrollPosition.d.ts +1 -2
  200. package/dist/types/excalidraw/hooks/useTextEditorFocus.d.ts +14 -0
  201. package/dist/types/excalidraw/i18n.d.ts +2 -2
  202. package/dist/types/excalidraw/index.d.ts +6 -7
  203. package/dist/types/excalidraw/renderer/animation.d.ts +12 -0
  204. package/dist/types/excalidraw/renderer/helpers.d.ts +6 -8
  205. package/dist/types/excalidraw/renderer/interactiveScene.d.ts +8 -14
  206. package/dist/types/excalidraw/scene/Renderer.d.ts +5 -2
  207. package/dist/types/excalidraw/scene/export.d.ts +2 -2
  208. package/dist/types/excalidraw/scene/scroll.d.ts +1 -6
  209. package/dist/types/excalidraw/scene/types.d.ts +22 -5
  210. package/dist/types/excalidraw/shortcut.d.ts +1 -0
  211. package/dist/types/excalidraw/snapping.d.ts +5 -5
  212. package/dist/types/excalidraw/subset/harfbuzz/harfbuzz-bindings.d.ts +1 -1
  213. package/dist/types/excalidraw/subset/harfbuzz/harfbuzz-loader.d.ts +1 -1
  214. package/dist/types/excalidraw/subset/harfbuzz/harfbuzz-wasm.d.ts +1 -1
  215. package/dist/types/excalidraw/subset/woff2/woff2-loader.d.ts +2 -2
  216. package/dist/types/excalidraw/subset/woff2/woff2-wasm.d.ts +1 -1
  217. package/dist/types/excalidraw/types.d.ts +32 -20
  218. package/dist/types/excalidraw/wysiwyg/textWysiwyg.d.ts +2 -2
  219. package/dist/types/math/src/polygon.d.ts +2 -2
  220. package/dist/types/math/src/range.d.ts +1 -3
  221. package/dist/types/math/src/segment.d.ts +4 -3
  222. package/dist/types/utils/src/bbox.d.ts +1 -1
  223. package/dist/types/utils/src/export.d.ts +5 -5
  224. package/dist/types/utils/src/shape.d.ts +6 -6
  225. package/dist/types/utils/src/withinBounds.d.ts +2 -2
  226. package/package.json +2 -2
package/dist/dev/index.js CHANGED
@@ -202,45 +202,152 @@ var LegendreGaussN24CValues = [
202
202
  0.0123412297999872
203
203
  ];
204
204
 
205
+ // src/line.ts
206
+ function line(a, b) {
207
+ return [a, b];
208
+ }
209
+ function linesIntersectAt(a, b) {
210
+ const A1 = a[1][1] - a[0][1];
211
+ const B1 = a[0][0] - a[1][0];
212
+ const A2 = b[1][1] - b[0][1];
213
+ const B2 = b[0][0] - b[1][0];
214
+ const D = A1 * B2 - A2 * B1;
215
+ if (D !== 0) {
216
+ const C1 = A1 * a[0][0] + B1 * a[0][1];
217
+ const C2 = A2 * b[0][0] + B2 * b[0][1];
218
+ return pointFrom((C1 * B2 - C2 * B1) / D, (A1 * C2 - A2 * C1) / D);
219
+ }
220
+ return null;
221
+ }
222
+
223
+ // src/segment.ts
224
+ function lineSegment(a, b) {
225
+ return [a, b];
226
+ }
227
+ var isLineSegment = (segment) => Array.isArray(segment) && segment.length === 2 && isPoint(segment[0]) && isPoint(segment[0]);
228
+ var lineSegmentRotate = (l, angle, origin) => {
229
+ return lineSegment(
230
+ pointRotateRads(l[0], origin || pointCenter(l[0], l[1]), angle),
231
+ pointRotateRads(l[1], origin || pointCenter(l[0], l[1]), angle)
232
+ );
233
+ };
234
+ var segmentsIntersectAt = (a, b) => {
235
+ const a0 = vectorFromPoint(a[0]);
236
+ const a1 = vectorFromPoint(a[1]);
237
+ const b0 = vectorFromPoint(b[0]);
238
+ const b1 = vectorFromPoint(b[1]);
239
+ const r = vectorSubtract(a1, a0);
240
+ const s = vectorSubtract(b1, b0);
241
+ const denominator = vectorCross(r, s);
242
+ if (denominator === 0) {
243
+ return null;
244
+ }
245
+ const i = vectorSubtract(vectorFromPoint(b[0]), vectorFromPoint(a[0]));
246
+ const u = vectorCross(i, r) / denominator;
247
+ const t = vectorCross(i, s) / denominator;
248
+ if (u === 0) {
249
+ return null;
250
+ }
251
+ const p = vectorAdd(a0, vectorScale(r, t));
252
+ if (t >= 0 && t < 1 && u >= 0 && u < 1) {
253
+ return pointFromVector(p);
254
+ }
255
+ return null;
256
+ };
257
+ var pointOnLineSegment = (point, line2, threshold = PRECISION) => {
258
+ const distance = distanceToLineSegment(point, line2);
259
+ if (distance === 0) {
260
+ return true;
261
+ }
262
+ return distance < threshold;
263
+ };
264
+ var distanceToLineSegment = (point, line2) => {
265
+ const [x, y] = point;
266
+ const [[x1, y1], [x2, y2]] = line2;
267
+ const A = x - x1;
268
+ const B = y - y1;
269
+ const C = x2 - x1;
270
+ const D = y2 - y1;
271
+ const dot = A * C + B * D;
272
+ const len_sq = C * C + D * D;
273
+ let param = -1;
274
+ if (len_sq !== 0) {
275
+ param = dot / len_sq;
276
+ }
277
+ let xx;
278
+ let yy;
279
+ if (param < 0) {
280
+ xx = x1;
281
+ yy = y1;
282
+ } else if (param > 1) {
283
+ xx = x2;
284
+ yy = y2;
285
+ } else {
286
+ xx = x1 + param * C;
287
+ yy = y1 + param * D;
288
+ }
289
+ const dx = x - xx;
290
+ const dy = y - yy;
291
+ return Math.sqrt(dx * dx + dy * dy);
292
+ };
293
+ function lineSegmentIntersectionPoints(l, s, threshold) {
294
+ const candidate = linesIntersectAt(line(l[0], l[1]), line(s[0], s[1]));
295
+ if (!candidate || !pointOnLineSegment(candidate, s, threshold) || !pointOnLineSegment(candidate, l, threshold)) {
296
+ return null;
297
+ }
298
+ return candidate;
299
+ }
300
+ function lineSegmentsDistance(s1, s2) {
301
+ if (lineSegmentIntersectionPoints(s1, s2)) {
302
+ return 0;
303
+ }
304
+ return Math.min(
305
+ distanceToLineSegment(s1[0], s2),
306
+ distanceToLineSegment(s1[1], s2),
307
+ distanceToLineSegment(s2[0], s1),
308
+ distanceToLineSegment(s2[1], s1)
309
+ );
310
+ }
311
+
205
312
  // src/curve.ts
206
313
  function curve(a, b, c, d) {
207
314
  return [a, b, c, d];
208
315
  }
209
- function gradient(f, t0, s0, delta = 1e-6) {
210
- return [
211
- (f(t0 + delta, s0) - f(t0 - delta, s0)) / (2 * delta),
212
- (f(t0, s0 + delta) - f(t0, s0 - delta)) / (2 * delta)
213
- ];
214
- }
215
- function solve(f, t0, s0, tolerance = 1e-3, iterLimit = 10) {
316
+ function solveWithAnalyticalJacobian(curve2, lineSegment2, t0, s0, tolerance = 1e-3, iterLimit = 10) {
216
317
  let error = Infinity;
217
318
  let iter = 0;
218
319
  while (error >= tolerance) {
219
320
  if (iter >= iterLimit) {
220
321
  return null;
221
322
  }
222
- const y0 = f(t0, s0);
223
- const jacobian = [
224
- gradient((t, s) => f(t, s)[0], t0, s0),
225
- gradient((t, s) => f(t, s)[1], t0, s0)
226
- ];
227
- const b = [[-y0[0]], [-y0[1]]];
228
- const det = jacobian[0][0] * jacobian[1][1] - jacobian[0][1] * jacobian[1][0];
229
- if (det === 0) {
323
+ const bt = 1 - t0;
324
+ const bt2 = bt * bt;
325
+ const bt3 = bt2 * bt;
326
+ const t0_2 = t0 * t0;
327
+ const t0_3 = t0_2 * t0;
328
+ const bezierX = bt3 * curve2[0][0] + 3 * bt2 * t0 * curve2[1][0] + 3 * bt * t0_2 * curve2[2][0] + t0_3 * curve2[3][0];
329
+ const bezierY = bt3 * curve2[0][1] + 3 * bt2 * t0 * curve2[1][1] + 3 * bt * t0_2 * curve2[2][1] + t0_3 * curve2[3][1];
330
+ const lineX = lineSegment2[0][0] + s0 * (lineSegment2[1][0] - lineSegment2[0][0]);
331
+ const lineY = lineSegment2[0][1] + s0 * (lineSegment2[1][1] - lineSegment2[0][1]);
332
+ const fx = bezierX - lineX;
333
+ const fy = bezierY - lineY;
334
+ error = Math.abs(fx) + Math.abs(fy);
335
+ if (error < tolerance) {
336
+ break;
337
+ }
338
+ const dfx_dt = -3 * bt2 * curve2[0][0] + 3 * bt2 * curve2[1][0] - 6 * bt * t0 * curve2[1][0] - 3 * t0_2 * curve2[2][0] + 6 * bt * t0 * curve2[2][0] + 3 * t0_2 * curve2[3][0];
339
+ const dfy_dt = -3 * bt2 * curve2[0][1] + 3 * bt2 * curve2[1][1] - 6 * bt * t0 * curve2[1][1] - 3 * t0_2 * curve2[2][1] + 6 * bt * t0 * curve2[2][1] + 3 * t0_2 * curve2[3][1];
340
+ const dfx_ds = -(lineSegment2[1][0] - lineSegment2[0][0]);
341
+ const dfy_ds = -(lineSegment2[1][1] - lineSegment2[0][1]);
342
+ const det = dfx_dt * dfy_ds - dfx_ds * dfy_dt;
343
+ if (Math.abs(det) < 1e-12) {
230
344
  return null;
231
345
  }
232
- const iJ = [
233
- [jacobian[1][1] / det, -jacobian[0][1] / det],
234
- [-jacobian[1][0] / det, jacobian[0][0] / det]
235
- ];
236
- const h = [
237
- [iJ[0][0] * b[0][0] + iJ[0][1] * b[1][0]],
238
- [iJ[1][0] * b[0][0] + iJ[1][1] * b[1][0]]
239
- ];
240
- t0 = t0 + h[0][0];
241
- s0 = s0 + h[1][0];
242
- const [tErr, sErr] = f(t0, s0);
243
- error = Math.max(Math.abs(tErr), Math.abs(sErr));
346
+ const invDet = 1 / det;
347
+ const dt = invDet * (dfy_ds * -fx - dfx_ds * -fy);
348
+ const ds = invDet * (-dfy_dt * -fx + dfx_dt * -fy);
349
+ t0 += dt;
350
+ s0 += ds;
244
351
  iter += 1;
245
352
  }
246
353
  return [t0, s0];
@@ -249,50 +356,49 @@ var bezierEquation = (c, t) => pointFrom(
249
356
  (1 - t) ** 3 * c[0][0] + 3 * (1 - t) ** 2 * t * c[1][0] + 3 * (1 - t) * t ** 2 * c[2][0] + t ** 3 * c[3][0],
250
357
  (1 - t) ** 3 * c[0][1] + 3 * (1 - t) ** 2 * t * c[1][1] + 3 * (1 - t) * t ** 2 * c[2][1] + t ** 3 * c[3][1]
251
358
  );
359
+ var initial_guesses = [
360
+ [0.5, 0],
361
+ [0.2, 0],
362
+ [0.8, 0]
363
+ ];
364
+ var calculate = ([t0, s0], l, c) => {
365
+ const solution = solveWithAnalyticalJacobian(c, l, t0, s0, 0.01, 3);
366
+ if (!solution) {
367
+ return null;
368
+ }
369
+ const [t, s] = solution;
370
+ if (t < 0 || t > 1 || s < 0 || s > 1) {
371
+ return null;
372
+ }
373
+ return bezierEquation(c, t);
374
+ };
252
375
  function curveIntersectLineSegment(c, l) {
253
- const line2 = (s) => pointFrom(
254
- l[0][0] + s * (l[1][0] - l[0][0]),
255
- l[0][1] + s * (l[1][1] - l[0][1])
256
- );
257
- const initial_guesses = [
258
- [0.5, 0],
259
- [0.2, 0],
260
- [0.8, 0]
261
- ];
262
- const calculate = ([t0, s0]) => {
263
- const solution2 = solve(
264
- (t2, s2) => {
265
- const bezier_point = bezierEquation(c, t2);
266
- const line_point = line2(s2);
267
- return [
268
- bezier_point[0] - line_point[0],
269
- bezier_point[1] - line_point[1]
270
- ];
271
- },
272
- t0,
273
- s0
274
- );
275
- if (!solution2) {
276
- return null;
277
- }
278
- const [t, s] = solution2;
279
- if (t < 0 || t > 1 || s < 0 || s > 1) {
280
- return null;
281
- }
282
- return bezierEquation(c, t);
283
- };
284
- let solution = calculate(initial_guesses[0]);
376
+ let solution = calculate(initial_guesses[0], l, c);
285
377
  if (solution) {
286
378
  return [solution];
287
379
  }
288
- solution = calculate(initial_guesses[1]);
380
+ solution = calculate(initial_guesses[1], l, c);
289
381
  if (solution) {
290
382
  return [solution];
291
383
  }
292
- solution = calculate(initial_guesses[2]);
384
+ solution = calculate(initial_guesses[2], l, c);
293
385
  if (solution) {
294
386
  return [solution];
295
387
  }
388
+ const startHit = lineSegmentIntersectionPoints(
389
+ lineSegment(bezierEquation(c, 0), bezierEquation(c, 1 / 20)),
390
+ l
391
+ );
392
+ if (startHit) {
393
+ return [startHit];
394
+ }
395
+ const endHit = lineSegmentIntersectionPoints(
396
+ lineSegment(bezierEquation(c, 19 / 20), bezierEquation(c, 1)),
397
+ l
398
+ );
399
+ if (endHit) {
400
+ return [endHit];
401
+ }
296
402
  return [];
297
403
  }
298
404
  function curveClosestPoint(c, p, tolerance = 1e-3) {
@@ -602,102 +708,6 @@ function ellipseLineIntersectionPoints({ center, halfWidth, halfHeight }, [g, h]
602
708
  return candidates;
603
709
  }
604
710
 
605
- // src/line.ts
606
- function line(a, b) {
607
- return [a, b];
608
- }
609
- function linesIntersectAt(a, b) {
610
- const A1 = a[1][1] - a[0][1];
611
- const B1 = a[0][0] - a[1][0];
612
- const A2 = b[1][1] - b[0][1];
613
- const B2 = b[0][0] - b[1][0];
614
- const D = A1 * B2 - A2 * B1;
615
- if (D !== 0) {
616
- const C1 = A1 * a[0][0] + B1 * a[0][1];
617
- const C2 = A2 * b[0][0] + B2 * b[0][1];
618
- return pointFrom((C1 * B2 - C2 * B1) / D, (A1 * C2 - A2 * C1) / D);
619
- }
620
- return null;
621
- }
622
-
623
- // src/segment.ts
624
- function lineSegment(a, b) {
625
- return [a, b];
626
- }
627
- var isLineSegment = (segment) => Array.isArray(segment) && segment.length === 2 && isPoint(segment[0]) && isPoint(segment[0]);
628
- var lineSegmentRotate = (l, angle, origin) => {
629
- return lineSegment(
630
- pointRotateRads(l[0], origin || pointCenter(l[0], l[1]), angle),
631
- pointRotateRads(l[1], origin || pointCenter(l[0], l[1]), angle)
632
- );
633
- };
634
- var segmentsIntersectAt = (a, b) => {
635
- const a0 = vectorFromPoint(a[0]);
636
- const a1 = vectorFromPoint(a[1]);
637
- const b0 = vectorFromPoint(b[0]);
638
- const b1 = vectorFromPoint(b[1]);
639
- const r = vectorSubtract(a1, a0);
640
- const s = vectorSubtract(b1, b0);
641
- const denominator = vectorCross(r, s);
642
- if (denominator === 0) {
643
- return null;
644
- }
645
- const i = vectorSubtract(vectorFromPoint(b[0]), vectorFromPoint(a[0]));
646
- const u = vectorCross(i, r) / denominator;
647
- const t = vectorCross(i, s) / denominator;
648
- if (u === 0) {
649
- return null;
650
- }
651
- const p = vectorAdd(a0, vectorScale(r, t));
652
- if (t >= 0 && t < 1 && u >= 0 && u < 1) {
653
- return pointFromVector(p);
654
- }
655
- return null;
656
- };
657
- var pointOnLineSegment = (point, line2, threshold = PRECISION) => {
658
- const distance = distanceToLineSegment(point, line2);
659
- if (distance === 0) {
660
- return true;
661
- }
662
- return distance < threshold;
663
- };
664
- var distanceToLineSegment = (point, line2) => {
665
- const [x, y] = point;
666
- const [[x1, y1], [x2, y2]] = line2;
667
- const A = x - x1;
668
- const B = y - y1;
669
- const C = x2 - x1;
670
- const D = y2 - y1;
671
- const dot = A * C + B * D;
672
- const len_sq = C * C + D * D;
673
- let param = -1;
674
- if (len_sq !== 0) {
675
- param = dot / len_sq;
676
- }
677
- let xx;
678
- let yy;
679
- if (param < 0) {
680
- xx = x1;
681
- yy = y1;
682
- } else if (param > 1) {
683
- xx = x2;
684
- yy = y2;
685
- } else {
686
- xx = x1 + param * C;
687
- yy = y1 + param * D;
688
- }
689
- const dx = x - xx;
690
- const dy = y - yy;
691
- return Math.sqrt(dx * dx + dy * dy);
692
- };
693
- function lineSegmentIntersectionPoints(l, s, threshold) {
694
- const candidate = linesIntersectAt(line(l[0], l[1]), line(s[0], s[1]));
695
- if (!candidate || !pointOnLineSegment(candidate, s, threshold) || !pointOnLineSegment(candidate, l, threshold)) {
696
- return null;
697
- }
698
- return candidate;
699
- }
700
-
701
711
  // src/polygon.ts
702
712
  function polygon(...points) {
703
713
  return polygonClose(points);
@@ -855,6 +865,7 @@ export {
855
865
  lineSegment,
856
866
  lineSegmentIntersectionPoints,
857
867
  lineSegmentRotate,
868
+ lineSegmentsDistance,
858
869
  linesIntersectAt,
859
870
  normalizeRadians,
860
871
  offsetPointsForQuadraticBezier,