forked from alexbol99/flatten-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
482 lines (404 loc) · 13.9 KB
/
index.d.ts
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// Type definitions for flatten-js library
// Project: https://github.com/alexbol99/flatten-js
// Definitions by: Alex Bol
import IntervalTree from "@flatten-js/interval-tree";
/// <reference types="@flatten-js/interval-tree" />
declare namespace Flatten {
interface SVGAttributes {
r?: number,
stroke?: string | number,
strokeWidth?: number,
fill?: string | number,
fillRule?: "nonzero" | "evenodd",
fillOpacity?: number,
id? : string,
className?: string
}
type Comparable = any; // any object that implements operators '<' and '==' and 'max'
type Value = any;
interface Interval {
low: Comparable;
high: Comparable;
readonly max: Comparable;
clone(): Interval;
less_than(other_interval: Interval) : boolean;
equal_to(other_interval: Interval) : boolean;
intersect(other_interval: Interval) : boolean;
not_intersect(other_interval: Interval) : boolean;
output() : any;
comparable_max(arg1: Comparable, arg2: Comparable) : Comparable;
comparable_less_than(arg1: Comparable, arg2: Comparable ) : boolean;
}
class LinkedListElement {
next: LinkedListElement | null;
prev: LinkedListElement | null;
}
class LinkedList {
// members
first: LinkedListElement;
last: LinkedListElement;
// getters
readonly size: number;
// public methods
append(element: LinkedListElement): LinkedList;
insert(newElement: LinkedListElement, prevElement: LinkedListElement): LinkedList;
remove(element: LinkedListElement): LinkedList;
toArray(): LinkedListElement[];
isEmpty(): boolean;
}
class CircularLinkedList extends LinkedList {}
class Arc {
constructor(
pc?: Point,
r?: number,
startAngle?: number,
endAngle?: number,
counterClockwise?: boolean
);
// members
ps: Point;
r: Number;
startAngle: number;
endAngle: number;
counterClockwise: boolean;
// getters
readonly start: Point;
readonly end: Point;
readonly center: Point;
readonly length: number;
readonly sweep: number;
readonly vertices: [Point, Point];
readonly box: Box;
// public methods
breakToFunctional(): Array<Arc>;
chordHeight(): number;
clone(): Arc;
contains(pt: Point): boolean;
chordHeight(): number;
distanceTo(geom: Shape | PlanarSet): [number, Segment];
intersect(shape: Shape): Array<Point>;
middle(): Point;
reverse(): Arc;
rotate(angle: number, center: Point): Arc;
split(pt: Point): Arc[];
svg(attrs?: SVGAttributes): string;
tangentInEnd(): Vector;
tangentInStart(): Vector;
transform(matrix?: Matrix): Arc;
translate(vec: Vector): Arc;
translate(x:number, y:number): Arc;
toJSON() : Object;
}
class Box implements Interval {
constructor(xmin?: number, ymin?: number, xmax?: number, ymax?: number);
//members
xmin: number;
ymin: number;
xmax: number;
ymax: number;
// getters
readonly center: Point;
readonly high: any;
readonly low: any;
readonly max: Box;
// public methods
clone(): Interval;
equal_to(box: Interval): boolean;
intersect(box: Interval): boolean;
less_than(box: Interval): boolean;
merge(box: Box): Box;
not_intersect(box: Interval): boolean;
output() : void;
set(xmin: number, ymin: number, xmax: number, ymax: number): void;
svg(attrs?: SVGAttributes): string;
comparable_max(arg1: Comparable, arg2: Comparable) : Comparable;
comparable_less_than(arg1: Comparable, arg2: Comparable ) : boolean;
}
class Circle {
constructor(pc: Point, r: number);
// members
pc: Point;
r: number;
// getters
readonly box: Box;
readonly center: Point;
// public methods
clone(): Circle;
contains(shape: Shape): boolean;
distanceTo(geom: Shape | PlanarSet): [number, Segment];
intersect(shape: Shape): Array<Point>;
svg(attrs?: SVGAttributes): string;
toArc(counterclockwise?: boolean): Arc;
toJSON() : Object;
}
class Line {
constructor(pt?: Point, norm?: Vector);
constructor(norm: Vector, pt: Point);
constructor(pt1: Point, pt2: Point);
// members
pt: Point;
norm: Vector;
// getters
readonly slope: number;
readonly standard: [number, number, number];
// public methods
clone(): Line;
contains(pt: Point): boolean;
distanceTo(shape: Shape): [number, Segment];
incidentTo(line: Line): boolean;
intersect(shape: Shape): Point[];
parallelTo(line: Line): boolean;
svg(box: Box, attrs?: SVGAttributes): string;
toJSON() : Object;
}
class Point {
constructor(x?: number, y?: number);
constructor(arg?: [number, number]);
// members
x: number;
y: number;
//getters
readonly box: Box;
readonly vertices: [Point];
// public methods
clone(): Point;
distanceTo(geom: Shape | PlanarSet): [number, Segment];
equalTo(pt: Point): boolean;
leftTo(line: Line): boolean;
lessThan(pt: Point): boolean;
on(shape: Point | Shape): boolean;
projectionOn(line: Line): Point;
rotate(angle: number, center?: Point): Point;
svg(attrs?: SVGAttributes): string;
transform(matrix: Matrix): Point;
translate(vec: Vector): Point;
translate(x: number, y: number): Point;
toJSON() : Object;
}
class Ray {
constructor(pt: Point);
// members
pt: Point;
// getters
readonly start: Point;
readonly box: Box;
readonly norm: Vector;
// public methods
clone(): Ray;
intersect(shape: Segment | Arc): Point[];
}
class Segment {
constructor(ps?: Point, pe?: Point);
// members
ps: Point;
pe: Point;
// getters
readonly start: Point;
readonly end: Point;
readonly box: Box;
readonly length: number;
readonly slope: number;
readonly vertices: [Point, Point];
// public methods
clone(): Segment;
contains(pt: Point): boolean;
distanceTo(shape: Shape): [number, Segment];
equalTo(seg: Segment): boolean;
intersect(shape: Shape): Point[];
isZeroLength(): boolean;
middle(): Point;
reverse(): Segment;
rotate(angle: number, center?: Point): Segment;
split(pt: Point): Segment[];
tangentInStart(): Vector;
tangentInEnd(): Vector;
transform(matrix: Matrix): Segment;
translate(vec: Vector): Segment;
translate(x: number, y: number): Segment;
svg(attrs?: SVGAttributes): string;
toJSON() : Object;
}
class Vector {
constructor(x?: number, y?: number);
constructor(ps: Point, pe: Point);
// members
x: number;
y: number;
// getters
readonly length: number;
readonly slope: number;
// public methods
add(v: Vector): Vector;
angleTo(v: Vector): number;
clone(): Vector;
cross(v: Vector): number;
dot(v: Vector): number;
equalTo(v: Vector): boolean;
invert(): Vector;
multiply(scalar: number): Vector;
normalize(): Vector;
projectionOn(v: Vector): Vector;
rotate(angle: number): Vector;
rotate90CCW(): Vector;
rotate90CW(): Vector;
subtract(v: Vector): Vector;
toJSON() : Object;
}
class Matrix {
constructor(a?: number, b?: number, c?: number, d?: number, tx?: number, ty?: number);
// members
a: number;
b: number;
c: number;
d: number;
tx: number;
ty: number;
// public methods
clone(): Matrix;
equalTo(matrix: Matrix): boolean;
multiply(matrix: Matrix): Matrix;
rotate(angle: number): Matrix;
scale(sx: number, sy: number): Matrix;
transform(vector: [number,number]): [number, number];
translate(tx: number, ty: number): Matrix;
translate(vector: Vector) : Matrix;
}
// any object that has "box" property that implements "Interval" interface may be indexable
// all shapes has box property that fits Interval interface
interface IndexableElement {
box: Interval;
}
// @ts-ignore (Set)
class PlanarSet extends Set {
constructor();
// members
index: IntervalTree;
// public methods
add(element: IndexableElement): this;
delete(element: IndexableElement): boolean;
clear() : void;
hit(pt: Point): IndexableElement[];
search(box: Box): IndexableElement[];
svg(): string;
}
const INSIDE = 1;
const OUTSIDE = 0;
const BOUNDARY = 2;
const CONTAINS = 3;
const INTERLACE = 4;
enum EdgeRelationType {INSIDE, OUTSIDE, BOUNDARY}
enum RelationType {INSIDE, OUTSIDE, BOUNDARY, CONTAINS, INTERLACE}
const OVERLAP_SAME = 1;
const OVERLAP_OPPOSITE = 2;
enum EdgeOverlappingType {OVERLAP_SAME, OVERLAP_OPPOSITE}
class Edge {
constructor(shape: Segment | Arc);
// members
shape: Segment | Arc;
face: Face;
next: Edge;
prev: Edge;
bvStart: EdgeRelationType;
bvEnd: EdgeRelationType;
bv: EdgeRelationType;
overlap: EdgeOverlappingType;
arc_length: number;
// getters
readonly start: Point;
readonly end: Point;
readonly length: number;
readonly box: Box;
// public methods
isSegment() : boolean;
isArc() : boolean;
contains(pt: Point): boolean;
middle(): Point;
setInclusion(polygon: Polygon): EdgeRelationType;
setOverlap(edge: Edge) : EdgeOverlappingType;
}
class Face extends CircularLinkedList {
// constructor is not documented and should not be called implicitly
// members
first: Edge;
last: Edge;
// getters
readonly box: Box;
readonly size: number;
readonly edges: Edge[];
// public methods
// Overriding methods Face.append(PlanarSet, Edge) has different number of parameters
// than refactored method LinkedList.append(LinkedListElement).
// also Face.insert(), Face.remove()
append(element: LinkedListElement):LinkedList;
append(edges: PlanarSet, edge: Edge): void;
insert(element: LinkedListElement): LinkedList;
insert(edges: PlanarSet, newEdge: Edge, edgeBefore: Edge): void;
remove(element: LinkedListElement): LinkedList;
remove(edges: PlanarSet, edge: Edge): void;
area(): number;
getRelation(polygon: Polygon): RelationType;
isSimple(edges: Edge[]): boolean;
orientation(): Flatten.ORIENTATION.PolygonOrientationType;
reverse(): void;
setArcLength(): void;
signedArea(): number;
svg(attrs?: SVGAttributes, pathDefined? : boolean): string;
}
class Polygon {
constructor();
// members
edges: PlanarSet;
faces: PlanarSet;
// getters
readonly box: Box;
readonly vertices: Point[];
// public methods
addFace(args: Array<Point> | Array<Segment | Arc> | Circle | Box): Face;
addVertex(edge: Edge, pt: Point): Edge;
area(): number;
clone(): Polygon;
contains(shape: Shape): boolean;
deleteFace(face: Face): boolean;
distanceTo(shape: Shape): [number, Segment];
intersect(shape: Shape): Point[];
isValid(): boolean;
removeChain(face: Face, edgeFrom: Edge, edgeTo: Edge): void;
rotate(angle?: number, center?: Point): Polygon;
svg(attrs?: SVGAttributes): string;
transform(matrix?: Matrix): Polygon;
translate(vec: Vector): Polygon;
toJSON() : Object;
}
type Shape = Point | Line | Circle | Segment | Arc | Polygon;
function point(x?: number, y?: number): Point;
function point(arr?: [number, number]);
function circle(pc: Point, r: number) : Circle;
function line(pt?: Point, norm?: Vector) : Line;
function line(norm?: Vector, pt?: Point) : Line;
function line(pt1?: Point, pt2?: Point) : Line;
function segment(ps?: Point, pe?: Point) : Segment;
function segment(arr: [number, number, number, number]) : Segment;
function segment(psx: number, psy: number, pex: number, pey: number) : Segment;
function arc(pc?: Point, r?: number, startAngle?: number, endAngle?: number, counterClockwise?: boolean) : Arc;
function vector(x?: number, y?: number) : Vector;
function vector(arr: [number, number]) : Vector;
function vector(p1: Point, p2: Point) : Vector;
function ray(pt?: Point) : Ray;
function ray(x: number, y: number): Ray;
function matrix(a: number, b: number, c: number, d: number, tx: number, ty: number) : Matrix;
}
declare namespace Flatten.ORIENTATION {
const CCW: -1;
const CW: 1;
const NOT_ORIENTABLE: 0;
enum PolygonOrientationType {CCW, CW, NOT_ORIENTABLE}
}
declare namespace Flatten.Utils {
var DP_TOL: number;
function EQ_0(x: number) : boolean;
function GT(x: number, y: number) : boolean;
function GE(x: number, y: number) : boolean;
function LT(x: number, y: number) : boolean;
function LE(x: number, y: number) : boolean;
}
export default Flatten;