From afe0044eb65a88b41d0a92f626da7d008911ddee Mon Sep 17 00:00:00 2001 From: lindapaiste Date: Wed, 12 Aug 2020 23:33:55 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#46644=20created=20?= =?UTF-8?q?type=20definitions=20for=20"geometric"=20package=20by=20@lindap?= =?UTF-8?q?aiste?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * created type definitions for "geometric" package * Update types/geometric/index.d.ts Co-authored-by: Piotr Błażejewicz (Peter Blazejewicz) * Update types/geometric/index.d.ts Co-authored-by: Piotr Błażejewicz (Peter Blazejewicz) Co-authored-by: Piotr Błażejewicz (Peter Blazejewicz) --- types/geometric/geometric-tests.ts | 45 +++++++ types/geometric/index.d.ts | 200 +++++++++++++++++++++++++++++ types/geometric/tsconfig.json | 23 ++++ types/geometric/tslint.json | 1 + 4 files changed, 269 insertions(+) create mode 100644 types/geometric/geometric-tests.ts create mode 100644 types/geometric/index.d.ts create mode 100644 types/geometric/tsconfig.json create mode 100644 types/geometric/tslint.json diff --git a/types/geometric/geometric-tests.ts b/types/geometric/geometric-tests.ts new file mode 100644 index 0000000000..647f386df9 --- /dev/null +++ b/types/geometric/geometric-tests.ts @@ -0,0 +1,45 @@ +import geometric = require("geometric"); +import { Line, LineInterpolator, Point, Polygon } from "geometric"; + +const polygon: Polygon = [[0, 0], [10, 0], [0, 20], [10, 20]]; +const polygonB: Polygon = [[1, 1], [2, 8], [10, 0]]; +const point: Point = [1, 2]; +const line: Line = [[1, 2], [3, 4]]; +const interpolator: LineInterpolator = geometric.lineInterpolate(line); +const t0: Point = interpolator(.5); +const t1: number = geometric.angleReflect(0, 90); +const t2: number = geometric.angleToDegrees(75); +const t3: number = geometric.angleToRadians(1); +const t4: number = geometric.lineAngle(line); +const t5: LineInterpolator = geometric.lineInterpolate(line); +const t6: boolean = geometric.lineIntersectsLine(line, line); +const t7: boolean = geometric.lineIntersectsPolygon(line, polygon); +const t8: number = geometric.lineLength(line); +const t9: Point = geometric.lineMidpoint(line); +const t10: boolean = geometric.pointInPolygon(point, polygon); +const t11: boolean = geometric.pointLeftofLine(point, line); +const t12: boolean = geometric.pointRightofLine(point, line); +const t13: boolean = geometric.pointOnLine(point, line); +const t14: boolean = geometric.pointOnPolygon(point, polygon); +const t15: Point = geometric.pointRotate(point, 90); +const t16: Point = geometric.pointRotate(point, 90, [1, 1]); +const t17: Point = geometric.pointTranslate(point, 75, 100); +const t18: boolean = geometric.pointWithLine(point, line); +const t19: number = geometric.polygonArea(polygon); +const t20: number = geometric.polygonArea(polygon, true); +const t21: [Point, Point] | null = geometric.polygonBounds(polygon); +const t22: Point = geometric.polygonCentroid(polygon); +const t23: Polygon = geometric.polygonHull(polygon); +const t24: boolean = geometric.polygonInPolygon(polygon, polygonB); +const t25: boolean = geometric.polygonIntersectsPolygon(polygon, polygonB); +const t26: number = geometric.polygonLength(polygon); +const t27: Point = geometric.polygonMean(polygon); +const t28: Polygon = geometric.polygonRegular(); +const t29: Polygon = geometric.polygonRegular(5); +const t30: Polygon = geometric.polygonRegular(5, 1); +const t31: Polygon = geometric.polygonRegular(5, 1, [0, 0]); +const t32: Polygon = geometric.polygonRotate(polygon, 45); +const t33: Polygon = geometric.polygonRotate(polygon, 45, point); +const t34: Polygon = geometric.polygonScale(polygon, .5); +const t35: Polygon = geometric.polygonScale(polygon, .5, point); +const t36: Polygon = geometric.polygonTranslate(polygon, 45, 100); diff --git a/types/geometric/index.d.ts b/types/geometric/index.d.ts new file mode 100644 index 0000000000..eb652f0dce --- /dev/null +++ b/types/geometric/index.d.ts @@ -0,0 +1,200 @@ +// Type definitions for geometric 2.2 +// Project: https://github.com/HarryStevens/geometric#readme +// Definitions by: Linda Paiste +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export as namespace geometric; + +// -------------------------------PRIMITIVES-------------------------------------------// + + /** + * Points are represented as arrays of two numbers, such as [0, 0]. + */ + export type Point = [number, number]; + + /** + * Lines are represented as arrays of two points, such as [[0, 0], [1, 0]]. Because they have endpoints, these are + * technically line segments, but Geometric.js refers to them as lines for simplicity's sake. + */ + export type Line = [Point, Point]; + + /** + * Polygons are represented as arrays of vertices, each of which is a point, such as [[0, 0], [1, 0], [1, 1], [0, + * 1]]. Polygons can be closed – the first and last vertex are the same – or open. + */ + export type Polygon = Point[]; + +// -------------------------------POINTS-------------------------------------------// + + /** + * Returns the coordinates resulting from rotating a point about an origin by an angle in degrees. If origin is + * not specified, the origin defaults to [0, 0]. + */ + export function pointRotate(point: Point, angle: number, origin?: Point): Point; + + /** + * Returns the coordinates resulting from translating a point by an angle in degrees and a distance. + */ + export function pointTranslate(point: Point, angle: number, distance: number): Point; + +// -------------------------------LINES-------------------------------------------// + + /** + * Returns the angle of a line, in degrees, with respect to the horizontal axis. + */ + export function lineAngle(line: Line): number; + + /** + * Returns an interpolator function given a line [a, b]. The returned interpolator function takes a single + * argument t, where t is a number ranging from 0 to 1; a value of 0 returns a, while a value of 1 returns b. + * Intermediate values interpolate from a to b along the line segment. + */ + export function lineInterpolate(line: Line): LineInterpolator; + + export type LineInterpolator = (t: number) => Point; + + /** + * Returns the length of a line. + */ + export function lineLength(line: Line): number; + + /** + * Returns the midpoint of a line. + */ + export function lineMidpoint(line: Line): Point; + +// -------------------------------POLYGONS-------------------------------------------// + + /** + * Returns the area of a polygon. You can pass a boolean indicating whether the returned area is signed, which + * defaults to false. + */ + export function polygonArea(polygon: Polygon, signed?: boolean): number; + + /** + * Returns the bounds of a polygon, ignoring points with invalid values (null, undefined, NaN, Infinity). The + * returned bounds are represented as an array of two points, where the first point is the top-left corner and the + * second point is the bottom-right corner. For example: + * const rectangle = [[0, 0], [0, 1], [1, 1], [1, 0]]; + * const bounds = geometric.polygonBounds(rectangle); // [[0, 0], [1, 1]] + * Returns null if the polygon has fewer than three points. + */ + export function polygonBounds(polygon: Polygon): null | [Point, Point]; + + /** + * Returns the weighted centroid of a polygon. Not to be confused with a mean center. + */ + export function polygonCentroid(polygon: Polygon): Point; + + /** + * Returns the convex hull, represented as a polygon, for an array of points. Returns null if the input array has + * fewer than three points. Uses Andrew’s monotone chain algorithm. + */ + export function polygonHull(points: Point[]): Polygon; + + /** + * Returns the length of a polygon's perimeter. + */ + export function polygonLength(polygon: Polygon): number; + + /** + * Returns the arithmetic mean of the vertices of a polygon. Keeps duplicate vertices, resulting in different + * values for open and closed polygons. Not to be confused with a centroid. + */ + + export function polygonMean(polygon: Polygon): Point; + + /** + * Returns the vertices of a regular polygon of the specified number of sides, area, and center coordinates. If + * sides is not specified, defaults to 3. If area is not specified, defaults to 100. If center is not specified, + * defaults to [0, 0]. + */ + export function polygonRegular(sides?: number, area?: number, center?: Point): Polygon; + + /** + * Returns the vertices resulting from rotating a polygon about an origin by an angle in degrees. If origin is not + * specified, the origin defaults to [0, 0]. + */ + export function polygonRotate(polygon: Polygon, angle: number, origin?: Point): Polygon; + + /** + * Returns the vertices resulting from scaling a polygon by a scaleFactor (where 1 is the polygon's current size) + * from an origin point. If origin is not specified, the origin defaults to the polygon's centroid. + */ + export function polygonScale(polygon: Polygon, scaleFactor: number, origin?: Point): Polygon; + + /** + * Returns the vertices resulting from translating a polygon by an angle in degrees and a distance. + */ + export function polygonTranslate(polygon: Polygon, angle: number, distance: number): Polygon; + +// -------------------------------RELATIONSHIPS-------------------------------------------// + + /** + * Returns a boolean representing whether lineA intersects lineB. + */ + export function lineIntersectsLine(lineA: Line, lineB: Line): boolean; + + /** + * Returns a boolean representing whether a line intersects a polygon. + */ + export function lineIntersectsPolygon(line: Line, polygon: Polygon): boolean; + + /** + * Returns a boolean representing whether a point is inside of a polygon. Uses ray casting. + */ + export function pointInPolygon(point: Point, polygon: Polygon): boolean; + + /** + * Returns a boolean representing whether a point is located on one of the edges of a polygon. + */ + export function pointOnPolygon(point: Point, polygon: Polygon): boolean; + + /** + * Returns a boolean representing whether a point is collinear with a line and is also located on the line segment. + * See also pointWithLine. + */ + export function pointOnLine(point: Point, line: Line): boolean; + + /** + * Returns a boolean representing whether a point is collinear with a line. See also pointOnLine. + */ + export function pointWithLine(point: Point, line: Line): boolean; + + /** + * Returns a boolean representing whether a point is to the left of a line. + */ + export function pointLeftofLine(point: Point, line: Line): boolean; + + /** + * Returns a boolean representing whether a point is to the right of a line. + */ + export function pointRightofLine(point: Point, line: Line): boolean; + + /** + * Returns a boolean representing whether polygonA is contained by polygonB. + */ + export function polygonInPolygon(polygonA: Polygon, polygonB: Polygon): boolean; + + /** + * Returns a boolean representing whether polygonA intersects but is not contained by polygonB. + */ + export function polygonIntersectsPolygon(polygonA: Polygon, polygonB: Polygon): boolean; + +// -------------------------------ANGLES-------------------------------------------// + + /** + * Returns the angle of reflection given a starting angle, also known as the angle of incidence, and the angle of + * the surface off of which it is reflected. + */ + export function angleReflect(incidenceAngle: number, surfaceAngle: number): number; + + /** + * Returns the result of a converting an angle in radians to the same angle in degrees. + */ + export function angleToDegrees(angle: number): number; + + /** + * Returns the result of a converting an angle in degrees to the same angle in radians. + */ + export function angleToRadians(angle: number): number; diff --git a/types/geometric/tsconfig.json b/types/geometric/tsconfig.json new file mode 100644 index 0000000000..91732029bf --- /dev/null +++ b/types/geometric/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "geometric-tests.ts" + ] +} diff --git a/types/geometric/tslint.json b/types/geometric/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/geometric/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }