-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathintersect.d.ts
More file actions
139 lines (126 loc) · 3.52 KB
/
intersect.d.ts
File metadata and controls
139 lines (126 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Find or counts the intersections between two SVG paths.
*
* Returns a number in counting mode and a list of intersections otherwise.
*
* A single intersection entry contains the intersection coordinates (x, y)
* as well as additional information regarding the intersecting segments
* on each path (segment1, segment2) and the relative location of the
* intersection on these segments (t1, t2).
*
* The path may be an SVG path string or an array of path components
* such as `[ [ 'M', 0, 10 ], [ 'L', 20, 0 ] ]`.
*
* For performance optimization, pre-parsed paths can be passed directly,
* {@link parsePath | the parsePath utility} can be used to pre-parse any path.
*
* @example
*
* import findPathIntersections from 'path-intersection';
*
* var intersections = findPathIntersections(
* 'M0,0L100,100',
* [ [ 'M', 0, 100 ], [ 'L', 100, 0 ] ]
* );
*
* // intersections = [
* // { x: 50, y: 50, segment1: 1, segment2: 1, t1: 0.5, t2: 0.5 }
* // ];
*
* @param {Path} path1
* @param {Path} path2
* @param {boolean} [justCount=false]
*
* @return {Intersection[]|number}
*/
declare function findPathIntersections(path1: Path, path2: Path, justCount: true): number;
declare function findPathIntersections(path1: Path, path2: Path, justCount: false): Intersection[];
declare function findPathIntersections(path1: Path, path2: Path): Intersection[];
declare function findPathIntersections(path1: Path, path2: Path, justCount?: boolean): Intersection[] | number;
export default findPathIntersections;
/**
* Parses a path to an optimized format. The result can be cached
* and reused to maximize performance in subsequent intersection calculations.
*
* This is the recommended way to pre-parse paths for repeated use.
* Paths parsed this way will not be re-parsed when passed to
* {@link findPathIntersections | the intersect function}.
*
* @example
*
* import intersect, { parsePath } from 'path-intersection';
*
* // parse once
* const path1 = parsePath('M0,0L100,100');
* const path2 = parsePath('M0,100L100,0');
*
* // cache and reuse
* const result1 = intersect(path1, parsedPath2);
* const result2 = intersect(path2, parsedPath2);
*
* @param {Path} path - the path to parse
*
* @return {PathComponent[]} pre-parsed and optimized path
*/
export function parsePath(path: Path): PathComponent[];
/**
* A SVG path string, or it's array encoded version.
*
* @example
*
* "M150,150m0,-18a18,18,0,1,1,0,36a18,18,0,1,1,0,-36z"
*
* @example
*
* [
* ['M', 1, 2],
* ['m', 0, -2],
* ['a', 1, 1, 0, 1, 1, 0, 2 * 1],
* ['a', 1, 1, 0, 1, 1, 0, -2 * 1],
* ['z']
* ]
*/
declare type Path = string | PathComponent[];
/**
* A SVG path component, stored as an array with the operation, and parameters.
*
* @example
*
* ['M', 1, 2]
*/
declare type PathComponent = [ string, ...number[] ];
declare interface Intersection {
/**
* Segment of first path.
*/
segment1: number;
/**
* Segment of first path.
*/
segment2: number;
/**
* The x coordinate.
*/
x: number;
/**
* The y coordinate.
*/
y: number;
/**
* Bezier curve for matching path segment 1.
*/
bez1: number[];
/**
* Bezier curve for matching path segment 2.
*/
bez2: number[];
/**
* Relative position of intersection on path segment1 (0.5 => in middle, 0.0 => at start, 1.0 => at end).
*/
t1: number;
/**
* Relative position of intersection on path segment2 (0.5 => in middle, 0.0 => at start, 1.0 => at end).
*/
t2: number;
}
export type { Intersection, Path, PathComponent };