diff --git a/poly2tri/poly2tri-tests.ts b/poly2tri/poly2tri-tests.ts new file mode 100644 index 0000000000..2f062b3bee --- /dev/null +++ b/poly2tri/poly2tri-tests.ts @@ -0,0 +1,92 @@ +/// + +function initializeCDT(): poly2tri.SweepContext { + var contour = [ + new poly2tri.Point(100, 100), + new poly2tri.Point(100, 300), + new poly2tri.Point(300, 300), + new poly2tri.Point(300, 100) + ]; + + var swctx = new poly2tri.SweepContext(contour); + return swctx; +} + +function addHole() { + var swctx = initializeCDT(); + var hole = [ + new poly2tri.Point(200, 200), + new poly2tri.Point(200, 250), + new poly2tri.Point(250, 250) + ]; + swctx.addHole(hole); +} + +function addHoles() { + var swctx = initializeCDT(); + var hole1 = [ + new poly2tri.Point(200, 200), + new poly2tri.Point(200, 250), + new poly2tri.Point(250, 250) + ]; + + var hole2 = [ + new poly2tri.Point(110, 110), + new poly2tri.Point(110, 120), + new poly2tri.Point(120, 120) + ]; + + swctx.addHoles([hole1, hole2]); +} + +function addPoint() { + var swctx = initializeCDT(); + var point = new poly2tri.Point(150, 150); + swctx.addPoint(point); +} + +function addPoints() { + var swctx = initializeCDT(); + var point1 = new poly2tri.Point(150, 150); + var point2 = new poly2tri.Point(155, 155); + swctx.addPoints([point1, point2]); + swctx.addPoints([{x: 110, y: 120}]); +} + +function triangulate() { + var swctx = initializeCDT(); + swctx.triangulate(); + var triangles = swctx.getTriangles(); + + triangles.forEach(t => { + t.getPoints().forEach(p => { + console.log(p.x, p.y); + }); + var p1 = t.getPoint(0); + var p2 = t.getPoint(1); + var p3 = t.getPoint(2); + }); +} + +function chaining() { + var swctx = initializeCDT(); + + var hole1 = [ + new poly2tri.Point(200, 200), + new poly2tri.Point(200, 250), + new poly2tri.Point(250, 250) + ]; + + var hole2 = [ + new poly2tri.Point(110, 110), + new poly2tri.Point(110, 120), + new poly2tri.Point(120, 120) + ]; + + var holes = [hole1, hole2]; + var point1 = new poly2tri.Point(150, 150); + var point2 = new poly2tri.Point(155, 155); + var points = [point1, point2, { x: 153, y: 153 }]; + + var triangles = swctx.addHoles(holes).addPoints(points).triangulate().getTriangles(); +} diff --git a/poly2tri/poly2tri.d.ts b/poly2tri/poly2tri.d.ts new file mode 100644 index 0000000000..9b9edebd20 --- /dev/null +++ b/poly2tri/poly2tri.d.ts @@ -0,0 +1,113 @@ +// Type definitions for poly2tri v0.9.10 +// Project: http://github.com/r3mi/poly2tri.js/ +// Definitions by: Elemar Junior +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module poly2tri { + + interface IPointLike { + x: number; + y: number; + } + + class Point implements IPointLike { + + x: number; + y: number; + + constructor(x: number, y: number); + + toString(): string; + + toJSON(): JSON; + + clone(): Point; + + set_zero(): Point; + + set(x: number, y: number): Point; + + negate(): Point; + + add(n: IPointLike): Point; + + sub(n: IPointLike): Point; + + mul(s: number): Point; + + length(): number; + + normalize(): number; + + equals(p: IPointLike): boolean; + + static negate(p: IPointLike): Point; + + static add(a: IPointLike, b: IPointLike): Point; + + static sub(a: IPointLike, b: IPointLike): Point; + + static mul(s: number, p: IPointLike): Point; + + static cross(a: number, b: number): number; + + static cross(a: IPointLike, b: number): number; + + static cross(a: IPointLike, b: IPointLike): number; + + static cross(a: number, b: IPointLike): number; + + static toStringBase(p: IPointLike): string; + + static toString(p: IPointLike): string; + + static compare(a: IPointLike, b: IPointLike): number; + + static equals(a: IPointLike, b: IPointLike): boolean; + + static dot(a: IPointLike, b: IPointLike): number; + } + + + class SweepContext { + constructor(contour: Array); + + constructor(contour: Array, options: JSON); + + addHole(polyline: Array): SweepContext; + + addHoles(holes: Array>): SweepContext; + + addPoint(point: IPointLike): SweepContext; + + addPoints(point: Array): SweepContext; + + triangulate(): SweepContext; + + getBoundingBox(): { min: IPointLike; max: IPointLike; }; + + getTriangles(): Array; + } + + class Triangle { + constructor(a: IPointLike, b: IPointLike, c: IPointLike); + + toString(): string; + + getPoint(index: number): IPointLike; + + getPoints(): Array; + + containsPoint(point: IPointLike): boolean; + + containsPoints(p1: IPointLike, p2: IPointLike): boolean; + + isInterior(): boolean; + } +} + + + + + +