From d201ae04f7545013f581da033384ec7e967b14b2 Mon Sep 17 00:00:00 2001 From: Dominic Alie Date: Thu, 15 Oct 2015 14:05:21 -0400 Subject: [PATCH 1/3] Sylvester type definitions --- sylvester/sylvester-test.ts | 207 +++++++++ sylvester/sylvester.d.ts | 825 ++++++++++++++++++++++++++++++++++++ 2 files changed, 1032 insertions(+) create mode 100644 sylvester/sylvester-test.ts create mode 100644 sylvester/sylvester.d.ts diff --git a/sylvester/sylvester-test.ts b/sylvester/sylvester-test.ts new file mode 100644 index 0000000000..1eb5e076ec --- /dev/null +++ b/sylvester/sylvester-test.ts @@ -0,0 +1,207 @@ +/// +interface IAny { + a: any; +} + +var obj: IAny; +var n: number = 0; +var bool: boolean = false; +var str: string = ''; + +var l: Line = Line.create(v, v); +var m: Matrix = Matrix.create(v); +var p: Plane = Plane.create(v, v); +var v: Vector = Vector.create([n, n]); + +l = Line.create([n], [n]); +l = Line.X; +l = Line.Y; +l = Line.Z; +v = l.anchor; +bool = l.contains(v); +v = l.direction; +n = l.distanceFrom(l); +n = l.distanceFrom(v); +n = l.distanceFrom(p); +l = l.dup(); +bool = l.eql(l); +v = l.intersectionWith(l); +v = l.intersectionWith(p); +bool = l.intersects(l); +bool = l.intersects(p); +bool = l.isParallelTo(l); +bool = l.isParallelTo(p); +bool = l.liesIn(p); +v = l.pointClosestTo(l); +v = l.pointClosestTo(v); +l = l.reflectionIn(l); +l = l.reflectionIn(p); +l = l.reflectionIn(v); +l = l.rotate(n, l); +l = l.rotate(n, v); +l = l.setVectors([n, n], v); +l = l.setVectors(v, [n, n]); +l = l.translate(v); +l = l.translate([n, n]); + +m = Matrix.create(m); +m = Matrix.create([n]); +m = Matrix.create([[n]]); +m = Matrix.create(v); +m = Matrix.Diagonal(m); +m = Matrix.Diagonal([n]); +m = Matrix.Diagonal([[n]]); +m = Matrix.Diagonal(v); +m = Matrix.I(n); +m = Matrix.Random(n, n); +m = Matrix.Rotation(n); +m = Matrix.Rotation(n, v); +m = Matrix.RotationX(n); +m = Matrix.RotationY(n); +m = Matrix.RotationZ(n); +m = Matrix.Zero(n, n); +m = m.add(m); +m = m.augment(m); +m = m.augment(v); +bool = m.canMultiplyFromLeft(m); +v = m.col(n); +n = m.cols(); +n = m.det(); +n = m.determinant(); +v = m.diagonal(); +obj = m.dimensions(); +m = m.dup(); +v = m.e(n, n); +var element: number[][] = m.elements; +bool = m.eql(m); +bool = m.eql(v); +obj = m.indexOf(n); +str = m.inspect(); +m = m.inv(); +m = m.inverse(); +bool = m.isSameSizeAs(m); +bool = m.isSingular(); +bool = m.isSquare(); + +m = m.map((x, i, j) => { + n = x; + n = i; + n = j; +}); + +n = m.max(); +m = m.minor(n, n, n, n); +m = m.multiply(n); +m = m.multiply(m); +v = m.multiply(v); +n = m.rank(); +n = m.rk(); +m = m.round(); +v = m.row(n); +n = m.rows(); +m = m.setElements(m); +m = m.setElements([n, n]); +m = m.setElements([[n, n]]); +m = m.setElements(v); +m = m.snapTo(n); +m = m.subtract(m); +m = m.toRightTriangular(); +m = m.toUpperTriangular(); +n = m.tr(); +n = m.trace(); +m = m.transpose(); +m = m.x(m); +m = m.x(n); +v = m.x(v); + +p = Plane.create([0], [0]); +p = Plane.XY; +p = Plane.YX; +p = Plane.YZ; +p = Plane.ZX; +v = p.anchor; +bool = p.contains(v); +bool = p.contains(l); +n = p.distanceFrom(p); +n = p.distanceFrom(v); +n = p.distanceFrom(l); +p = p.dup(); +bool = p.eql(p); +v = p.intersectionWith(l); +l = p.intersectionWith(p); +bool = p.intersects(p); +bool = p.intersects(l); +bool = p.isParallelTo(p); +bool = p.isParallelTo(l); +bool = p.isPerpendicularTo(p); +v = p.normal; +v = p.pointClosestTo(v); +p = p.reflectionIn(p); +p = p.reflectionIn(v); +p = p.reflectionIn(l); +p = p.rotate(n, l); +p = p.setVectors([n], [n]); +p = p.setVectors(v, v); +p = p.setVectors([n], [n], [n]); +p = p.setVectors(v, v, v); +p = p.translate([n]); +p = p.translate(v); + +v = Vector.create(v); +v = Vector.i; +v = Vector.j; +v = Vector.k; +v = Vector.Random(n); +v = Vector.Zero(n); +v = v.add(v); +v = v.add([n, n]); +n = v.angleFrom(v); +v = v.cross(v); +v = v.cross([n, n]); +n = v.dimensions(); +n = v.distanceFrom(v); +n = v.distanceFrom(l); +n = v.distanceFrom(p); +n = v.dot(v); +n = v.dot([n, n]); +v = v.dup(); +n = v.e(n); + +v.each((x, i) => { + n = x; + n = i; +}); + +var elements: number[] = v.elements; +bool = v.eql(v); +bool = v.eql([n, n]); +n = v.indexOf(n); +str = v.inspect(); +bool = v.isAntiparallelTo(v); +bool = v.isParallelTo(v); +bool = v.isPerpendicularTo(v); +bool = v.liesIn(p); +bool = v.liesOn(l); + +v = v.map((x, i) => { + n = x; + n = i; +}); + +n = v.max(); +n = v.modulus(); +v = v.multiply(n); +v = v.reflectionIn(v); +v = v.reflectionIn(l); +v = v.reflectionIn(p); +v = v.rotate(n, v); +v = v.rotate(n, l); +v = v.round(); +v = v.setElements(v); +v = v.setElements([n, n]); +v = v.snapTo(n); +v = v.subtract(v); +v = v.to3D(); +m = v.toDiagonalMatrix(); +v = v.toUnitVector(); +v = v.x(n); \ No newline at end of file diff --git a/sylvester/sylvester.d.ts b/sylvester/sylvester.d.ts new file mode 100644 index 0000000000..0aa869d38c --- /dev/null +++ b/sylvester/sylvester.d.ts @@ -0,0 +1,825 @@ +// Type definitions for sylvester 0.1.3 +// Project: https://github.com/jcoglan/sylvester +// Definitions by: Stephane Alie +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +// === Sylvester === +// Vector and Matrix mathematics modules for JavaScript +// Copyright (c) 2007 James Coglan + +interface VectorStatic { + /** + * Constructor function. + */ + create(elements: Vector|Array): Vector; + + i: Vector; + j: Vector; + k: Vector; + + /** + * Random vector of size n. + * + * @param {number} n The vector size. + */ + Random(n: number): Vector; + + /** + * Vector filled with zeros. + * + * @param {number} n The vector size. + */ + Zero(n: number): Vector; +} + +interface Vector { + /** + * Gets an array containing the vector's elements. + */ + elements: Array; + + /** + * Returns element i of the vector. + */ + e(i: number): number; + + /** + * Returns the number of elements the vector has. + */ + dimensions(): number; + + /** + * Returns the modulus ('length') of the vector. + */ + modulus(): number; + + /** + * Returns true if the vector is equal to the argument. + * + * @param {Vector|Array} vector The vector to compare equality. + */ + eql(vector: Vector|Array): boolean; + + /** + * Returns a copy of the vector. + */ + dup(): Vector; + + /** + * Maps the vector to another vector according to the given function. + * + * @param {Function} fn The function to apply to each element (x, i) => {}. + */ + map(fn: (x: number, i: number) => any): Vector; + + /** + * Calls the iterator for each element of the vector in turn. + * + * @param {Function} fn The function to apply to each element (x, i) => {}. + */ + each(fn: (x: number, i: number) => any): void; + + /** + * Returns a new vector created by normalizing the receiver. + */ + toUnitVector(): Vector; + + /** + * Returns the angle between the vector and the argument (also a vector). + * + * @param {Vector} vector The other vector to calculate the angle. + */ + angleFrom(vector: Vector): number; + + /** + * Returns true if the vector is parallel to the argument. + * + * @param {Vector} vector The other vector. + */ + isParallelTo(vector: Vector): boolean; + + /** + * Returns true if the vector is antiparallel to the argument. + * + * @param {Vector} vector The other vector. + */ + isAntiparallelTo(vector: Vector): boolean; + + /** + * Returns true iff the vector is perpendicular to the argument. + * + * @param {Vector} vector The other vector. + */ + isPerpendicularTo(vector: Vector): boolean; + + /** + * Returns the result of adding the argument to the vector. + * + * @param {Vector|Array} vector The vector. + */ + add(vector: Vector|Array): Vector; + + /** + * Returns the result of subtracting the argument from the vector. + * + * @param {Vector|Array} vector The vector. + */ + subtract(vector: Vector|Array): Vector; + + /** + * Returns the result of multiplying the elements of the vector by the argument. + * + * @param {number} k The value by which to multiply the vector. + */ + multiply(k: number): Vector; + + /** + * Returns the result of multiplying the elements of the vector by the argument (Alias for multiply(k)). + * + * @param {number} k The value by which to multiply the vector. + */ + x(k: number): Vector; + + /** + * Returns the scalar product of the vector with the argument. Both vectors must have equal dimensionality. + * + * @param: {Vector|Array} vector The other vector. + */ + dot(vector: Vector|Array): number; + + /** + * Returns the vector product of the vector with the argument. Both vectors must have dimensionality 3. + * + * @param {Vector|Array} vector The other vector. + */ + cross(vector: Vector|Array): Vector; + + /** + * Returns the (absolute) largest element of the vector. + */ + max(): number; + + /** + * Returns the index of the first match found. + * + * @param {number} x The value. + */ + indexOf(x: number): number; + + /** + * Returns a diagonal matrix with the vector's elements as its diagonal elements. + */ + toDiagonalMatrix(): Matrix; + + /** + * Returns the result of rounding the elements of the vector. + */ + round(): Vector; + + /** + * Returns a copy of the vector with elements set to the given value if they differ from + * it by less than Sylvester.precision. + * + * @param {number} x The value to snap to. + */ + snapTo(x: number): Vector; + + /** + * Returns the vector's distance from the argument, when considered as a point in space. + * + * @param {Vector|Line|Plane} obj The object to calculate the distance. + */ + distanceFrom(obj: Vector|Line|Plane): number; + + /** + * Returns true if the vector is point on the given line. + * + * @param {Line} line The line. + */ + liesOn(line: Line): boolean; + + /** + * Return true if the vector is a point in the given plane. + * + * @param {Plane} plane The plane. + */ + liesIn(plane: Plane): boolean; + + /** + * Rotates the vector about the given object. The object should be a point if the vector is 2D, + * and a line if it is 3D. Be careful with line directions! + * + * @param {number} t The angle in radians. + * @param {Vector|Line} obj The rotation axis. + */ + rotate(t: number, obj: Vector|Line): Vector; + + /** + * Returns the result of reflecting the point in the given point, line or plane. + * + * @param {Vector|Line|Plane} obj The object. + */ + reflectionIn(obj: Vector|Line|Plane): Vector; + + /** + * Utility to make sure vectors are 3D. If they are 2D, a zero z-component is added. + */ + to3D(): Vector; + + /** + * Returns a string representation of the vector. + */ + inspect(): string; + + /** + * Set vector's elements from an array. + * + * @param {Vector|Array} els The elements. + */ + setElements(els: Vector|Array): Vector; +} + +interface MatrixStatic { + /** + * Constructor function. + * + * @param {Array|Array>|Vector|Matrix} elements The elements. + */ + create(elements: Array|Array>|Vector | Matrix): Matrix; + + /** + * Identity matrix of size n. + * + * @param {number} n The size. + */ + I(n: number): Matrix; + + /** + * Diagonal matrix - all off-diagonal elements are zero + * + * @param {any} elements The elements. + */ + Diagonal(elements: Array|Array>|Vector | Matrix): Matrix; + + /** + * Rotation matrix about some axis. If no axis is supplied, assume we're after a 2D transform. + * + * @param {number} theta The angle in radians. + * @param {Vector} a [Optional] The axis. + */ + Rotation(theta: number, a?: Vector): Matrix; + + RotationX(t: number): Matrix; + RotationY(t: number): Matrix; + RotationZ(t: number): Matrix; + + /** + * Random matrix of n rows, m columns. + * + * @param {number} n The number of rows. + * @param {number} m The number of columns. + */ + Random(n: number, m: number): Matrix; + + /** + * Matrix filled with zeros. + * + * @param {number} n The number of rows. + * @param {number} m The number of columns. + */ + Zero(n: number, m: number): Matrix; +} + +interface Matrix { + /** + * Gets a nested array containing the matrix's elements. + */ + elements: Array>; + /** + * Returns element (i,j) of the matrix. + * + * @param {number} i The row index. + * @param {number} j The column index. + */ + e(i: number, j: number): any; + + /** + * Returns row k of the matrix as a vector. + * + * @param {number} i The row index. + */ + row(i: number): Vector; + + /** + * Returns column k of the matrix as a vector. + * + * @param {number} j The column index. + */ + col(j: number): Vector; + + /** + * Returns the number of rows/columns the matrix has. + * + * @return {any} An object { rows: , cols: }. + */ + dimensions(): any; + + /** + * Returns the number of rows in the matrix. + */ + rows(): number; + + /** + * Returns the number of columns in the matrix. + */ + cols(): number; + + /** + * Returns true if the matrix is equal to the argument. You can supply a vector as the argument, + * in which case the receiver must be a one-column matrix equal to the vector. + * + * @param {Vector|Matrix} matrix The argument to compare. + */ + eql(matrix: Vector|Matrix): boolean; + + /** + * Returns a copy of the matrix. + */ + dup(): Matrix; + + /** + * Maps the matrix to another matrix (of the same dimensions) according to the given function. + * + * @param {Function} fn The function. + */ + map(fn: (x: number, i: number, j: number) => any): Matrix; + + /** + * Returns true iff the argument has the same dimensions as the matrix. + * + * @param {Matrix} matrix The other matrix. + */ + isSameSizeAs(matrix: Matrix): boolean; + + /** + * Returns the result of adding the argument to the matrix. + * + * @param {Matrix} matrix The matrix to add. + */ + add(matrix: Matrix): Matrix; + + /** + * Returns the result of subtracting the argument from the matrix. + * + * @param {Matrix} matrix The matrix to substract. + */ + subtract(matrix: Matrix): Matrix; + + /** + * Returns true iff the matrix can multiply the argument from the left. + * + * @param {Matrix} matrix The matrix. + */ + canMultiplyFromLeft(matrix: Matrix): boolean; + + /** + * Returns the result of multiplying the matrix from the right by the argument. If the argument is a scalar + * then just multiply all the elements. If the argument is a vector, a vector is returned, which saves you + * having to remember calling col(1) on the result. + * + * @param {number|Matrix} matrix The multiplier. + */ + multiply(matrix: number|Matrix): Matrix; + + /** + * Returns the result of multiplying the matrix from the right by the argument. If the argument is a scalar + * then just multiply all the elements. If the argument is a vector, a vector is returned, which saves you + * having to remember calling col(1) on the result. + * + * @param {Vector} vector The multiplier. + */ + multiply(vector: Vector): Vector; + + x(matrix: number|Matrix): Matrix; + + x(vector: Vector): Vector; + + /** + * Returns a submatrix taken from the matrix. Argument order is: start row, start col, nrows, ncols. + * Element selection wraps if the required index is outside the matrix's bounds, so you could use + * this to perform row/column cycling or copy-augmenting. + * + * @param {number} a Starting row index. + * @param {number} b Starting column index. + * @param {number} c Number of rows. + * @param {number} d Number of columns. + */ + minor(a: number, b: number, c: number, d: number): Matrix; + + /** + * Returns the transpose of the matrix. + */ + transpose(): Matrix; + + /** + * Returns true if the matrix is square. + */ + isSquare(): boolean; + + /** + * Returns the (absolute) largest element of the matrix. + */ + max(): number; + + /** + * Returns the indeces of the first match found by reading row-by-row from left to right. + * + * @param {number} x The value. + * + * @return {any} The element indeces i.e: { row:1, col:1 } + */ + indexOf(x: number): any; + + /** + * If the matrix is square, returns the diagonal elements as a vector; otherwise, returns null. + */ + diagonal(): Vector; + + /** + * Make the matrix upper (right) triangular by Gaussian elimination. This method only adds multiples + * of rows to other rows. No rows are scaled up or switched, and the determinant is preserved. + */ + toRightTriangular(): Matrix; + toUpperTriangular(): Matrix; + + /** + * Returns the determinant for square matrices. + */ + determinant(): number; + det(): number; + + /** + * Returns true if the matrix is singular. + */ + isSingular(): boolean; + + /** + * Returns the trace for square matrices. + */ + trace(): number; + tr(): number; + + /** + * Returns the rank of the matrix. + */ + rank(): number; + rk(): number; + + /** + * Returns the result of attaching the given argument to the right-hand side of the matrix. + * + * @param {Matrix|Vector} matrix The matrix or vector. + */ + augment(matrix: Matrix|Vector): Matrix; + + /** + * Returns the inverse (if one exists) using Gauss-Jordan. + */ + inverse(): Matrix; + inv(): Matrix; + + /** + * Returns the result of rounding all the elements. + */ + round(): Matrix; + + /** + * Returns a copy of the matrix with elements set to the given value if they differ from it + * by less than Sylvester.precision. + * + * @param {number} x The value. + */ + snapTo(x: number): Matrix; + + /** + * Returns a string representation of the matrix. + */ + inspect(): string; + + /** + * Set the matrix's elements from an array. If the argument passed is a vector, the resulting matrix + * will be a single column. + * + * @param {Array|Array>|Vector|Matrix} matrix The elements. + */ + setElements(matrix: Array|Array>|Vector|Matrix): Matrix; +} + +interface LineStatic { + /** + * Constructor function. + * + * @param Array|Vector anchor The anchor vector. + * @param Array|Vector direction The direction vector. + */ + create(anchor: Array|Vector, direction: Array|Vector): Line; + + X: Line; + Y: Line; + Z: Line; +} + +interface Line { + /** + * Gets the 3D vector corresponding to a point on the line. + */ + anchor: Vector; + + /** + * Gets a normalized 3D vector representing the line's direction. + */ + direction: Vector; + + /** + * Returns true if the argument occupies the same space as the line. + * + * @param {Line} line The other line. + */ + eql(line: Line): boolean; + + /** + * Returns a copy of the line. + */ + dup(): Line; + + /** + * Returns the result of translating the line by the given vector/array. + * + * @param {Vector|Array} vector The translation vector. + */ + translate(vector: Vector|Array): Line; + + /** + * Returns true if the line is parallel to the argument. Here, 'parallel to' means that the argument's + * direction is either parallel or antiparallel to the line's own direction. A line is parallel to a + * plane if the two do not have a unique intersection. + * + * @param {Line|Plane} obj The object. + */ + isParallelTo(obj: Line|Plane): boolean; + + /** + * Returns the line's perpendicular distance from the argument, which can be a point, a line or a plane. + * + * @param {Vector|Line|Plane} obj The object. + */ + distanceFrom(obj: Vector|Line|Plane): number; + + /** + * Returns true if the argument is a point on the line. + * + * @param {Vector} point The point. + */ + contains(point: Vector): boolean; + + /** + * Returns true if the line lies in the given plane. + * + * @param {Plane} plane The plane. + */ + liesIn(plane: Plane): boolean; + + /** + * Returns true if the line has a unique point of intersection with the argument. + * + * @param {Line|Plane} obj The object. + */ + intersects(obj: Line|Plane): boolean; + + /** + * Returns the unique intersection point with the argument, if one exists. + * + * @param {Line|Plane} obj The object. + */ + intersectionWith(obj: Line|Plane): Vector; + + /** + * Returns the point on the line that is closest to the given point or line. + * + * @param {Vector|Line} obj The object. + */ + pointClosestTo(obj: Vector|Line): Vector; + + /** + * Returns a copy of the line rotated by t radians about the given line. Works by finding the argument's + * closest point to this line's anchor point (call this C) and rotating the anchor about C. Also rotates + * the line's direction about the argument's. Be careful with this - the rotation axis' direction + * affects the outcome! + * + * @param {number} t The angle in radians. + * @param {Vector|Line} axis The axis. + */ + rotate(t: number, axis: Vector|Line): Line; + + /** + * Returns the line's reflection in the given point or line. + * + * @param {Vector|Line|Plane} obj The object. + */ + reflectionIn(obj: Vector|Line|Plane): Line; + + /** + * Set the line's anchor point and direction. + * + * @param {Array|Vector} anchor The anchor vector. + * @param {Array|Vector} direction The direction vector. + */ + setVectors(anchor: Array|Vector, direction: Array|Vector): Line; +} + +interface PlaneStatic { + /** + * Constructor function. + */ + create(anchor: Array|Vector, normal: Array|Vector): Plane; + + /** + * Constructor function. + */ + create(anchor: Array|Vector, v1: Array|Vector, v2: Array|Vector): Plane; + + XY: Plane; + YZ: Plane; + ZX: Plane; + YX: Plane; +} + +interface Plane { + /** + * Gets the 3D vector corresponding to a point in the plane. + */ + anchor: Vector; + + /** + * Gets a normalized 3D vector perpendicular to the plane. + */ + normal: Vector; + + /** + * Returns true if the plane occupies the same space as the argument. + * + * @param {Plane} plane The other plane. + */ + eql(plane: Plane): boolean; + + /** + * Returns a copy of the plane. + */ + dup(): Plane; + + /** + * Returns the result of translating the plane by the given vector. + * + * @param {Array|Vector} vector The translation vector. + */ + translate(vector: Array|Vector): Plane; + + /** + * Returns true if the plane is parallel to the argument. Will return true if the planes are equal, + * or if you give a line and it lies in the plane. + * + * @param {Line|Plane} obj The object. + */ + isParallelTo(obj: Line|Plane): boolean; + + /** + * Returns true if the receiver is perpendicular to the argument. + * + * @param {Plane} plane The other plane. + */ + isPerpendicularTo(plane: Plane): boolean; + + /** + * Returns the plane's distance from the given object (point, line or plane). + * + * @parm {Vector|Line|Plane} obj The object. + */ + distanceFrom(obj: Vector|Line|Plane): number; + + /** + * Returns true if the plane contains the given point or line. + * + * @param {Vector|Line} obj The object. + */ + contains(obj: Vector|Line): boolean; + + /** + * Returns true if the plane has a unique point/line of intersection with the argument. + * + * @param {Line|Plane} obj The object. + */ + intersects(obj: Line|Plane): boolean; + + /** + * Returns the unique intersection with the argument, if one exists. + * + * @param {Line} line The line. + */ + intersectionWith(line: Line): Vector; + + /** + * Returns the unique intersection with the argument, if one exists. + * + * @param {Plane} plane The plane. + */ + intersectionWith(plane: Plane): Line; + + /** + * Returns the point in the plane closest to the given point. + * + * @param {Vector} point The point. + */ + pointClosestTo(point: Vector): Vector; + + /** + * Returns a copy of the plane, rotated by t radians about the given line. See notes on Line#rotate. + * + * @param {number} t The angle in radians. + * @param {Line} axis The line axis. + */ + rotate(t: number, axis: Line): Plane; + + /** + * Returns the reflection of the plane in the given point, line or plane. + * + * @param {Vector|Line|Plane} obj The object. + */ + reflectionIn(obj: Vector|Line|Plane): Plane; + + /** + * Sets the anchor point and normal to the plane. Normal vector is normalised before storage. + * + * @param {Array|Vector} anchor The anchor vector. + * @param {Array|Vector} normal The normal vector. + */ + setVectors(anchor: Array|Vector, normal: Array|Vector): Plane; + + /** + * Sets the anchor point and normal to the plane. The normal is calculated by assuming the three points + * should lie in the same plane. Normal vector is normalised before storage. + * + * @param {Array|Vector} anchor The anchor vector. + * @param {Array|Vector} v1 The first direction vector. + * @param {Array|Vector} v2 The second direction vector. + */ + setVectors(anchor: Array|Vector, v1: Array|Vector, v2: Array|Vector): Plane; +} + +declare module Sylvester { + export var version: string; + export var precision: number; +} + +declare var Vector: VectorStatic; +declare var Matrix: MatrixStatic; +declare var Line: LineStatic; +declare var Plane: PlaneStatic; + +/** +* Constructor function. +* +* @param {Vector|Array): Vector; + +/** +* Constructor function. +* +* @param {Array|Array>|Vector|Matrix} elements The elements. +*/ +declare function $M(elements: Array|Array>|Vector | Matrix): Matrix; + +/** +* Constructor function. +* +* @param Array|Vector anchor The anchor vector. +* @param Array|Vector direction The direction vector. +*/ +declare function $L(anchor: Array|Vector, direction: Array|Vector): Line; + +/** +* Constructor function. +* +* @param {Array|Vector} anchor The anchor vector. +* @param {Array|Vector} normal The normal vector. +*/ +declare function $P(anchor: Array|Vector, normal: Array|Vector): Plane; + +/** + * Constructor function. + * + * @param {Array|Vector} anchor The anchor vector. + * @param {Array|Vector} v1 The first direction vector. + * @param {Array|Vecotr} v2 The second direction vector. + */ +declare function $P(anchor: Array|Vector, v1: Array|Vector, v2: Array|Vector): Plane; \ No newline at end of file From 6d2b4cc7a04d45df15cafe6bdb16c697e7413d2e Mon Sep 17 00:00:00 2001 From: Dominic Alie Date: Fri, 30 Oct 2015 10:12:18 -0400 Subject: [PATCH 2/3] Fix namespace issue --- sylvester/sylvester-test.ts | 17 +++ sylvester/sylvester.d.ts | 212 ++++++++++++++++++------------------ 2 files changed, 123 insertions(+), 106 deletions(-) diff --git a/sylvester/sylvester-test.ts b/sylvester/sylvester-test.ts index 1eb5e076ec..e42bb374e0 100644 --- a/sylvester/sylvester-test.ts +++ b/sylvester/sylvester-test.ts @@ -13,10 +13,14 @@ var m: Matrix = Matrix.create(v); var p: Plane = Plane.create(v, v); var v: Vector = Vector.create([n, n]); +l = $L(v, v); +l = $L([n], [n]); l = Line.create([n], [n]); + l = Line.X; l = Line.Y; l = Line.Z; + v = l.anchor; bool = l.contains(v); v = l.direction; @@ -44,10 +48,15 @@ l = l.setVectors(v, [n, n]); l = l.translate(v); l = l.translate([n, n]); +m = $M(m); +m = $M([n]); +m = $M([[n]]); +m = $M(v); m = Matrix.create(m); m = Matrix.create([n]); m = Matrix.create([[n]]); m = Matrix.create(v); + m = Matrix.Diagonal(m); m = Matrix.Diagonal([n]); m = Matrix.Diagonal([[n]]); @@ -114,11 +123,15 @@ m = m.x(m); m = m.x(n); v = m.x(v); +p = $P([n], [n]); +p = $P(v, v); p = Plane.create([0], [0]); + p = Plane.XY; p = Plane.YX; p = Plane.YZ; p = Plane.ZX; + v = p.anchor; bool = p.contains(v); bool = p.contains(l); @@ -147,10 +160,14 @@ p = p.setVectors(v, v, v); p = p.translate([n]); p = p.translate(v); +v = $V(v); +v = $V([n]); v = Vector.create(v); + v = Vector.i; v = Vector.j; v = Vector.k; + v = Vector.Random(n); v = Vector.Zero(n); v = v.add(v); diff --git a/sylvester/sylvester.d.ts b/sylvester/sylvester.d.ts index 0aa869d38c..2b899728f7 100644 --- a/sylvester/sylvester.d.ts +++ b/sylvester/sylvester.d.ts @@ -7,29 +7,111 @@ // Vector and Matrix mathematics modules for JavaScript // Copyright (c) 2007 James Coglan -interface VectorStatic { - /** - * Constructor function. - */ - create(elements: Vector|Array): Vector; +declare module Sylvester { + interface VectorStatic { + /** + * Constructor function. + */ + create(elements: Vector|Array): Vector; - i: Vector; - j: Vector; - k: Vector; + i: Vector; + j: Vector; + k: Vector; - /** - * Random vector of size n. - * - * @param {number} n The vector size. - */ - Random(n: number): Vector; + /** + * Random vector of size n. + * + * @param {number} n The vector size. + */ + Random(n: number): Vector; - /** - * Vector filled with zeros. - * - * @param {number} n The vector size. - */ - Zero(n: number): Vector; + /** + * Vector filled with zeros. + * + * @param {number} n The vector size. + */ + Zero(n: number): Vector; + } + interface MatrixStatic { + /** + * Constructor function. + * + * @param {Array|Array>|Vector|Matrix} elements The elements. + */ + create(elements: Array|Array>|Vector | Matrix): Matrix; + + /** + * Identity matrix of size n. + * + * @param {number} n The size. + */ + I(n: number): Matrix; + + /** + * Diagonal matrix - all off-diagonal elements are zero + * + * @param {any} elements The elements. + */ + Diagonal(elements: Array|Array>|Vector | Matrix): Matrix; + + /** + * Rotation matrix about some axis. If no axis is supplied, assume we're after a 2D transform. + * + * @param {number} theta The angle in radians. + * @param {Vector} a [Optional] The axis. + */ + Rotation(theta: number, a?: Vector): Matrix; + + RotationX(t: number): Matrix; + RotationY(t: number): Matrix; + RotationZ(t: number): Matrix; + + /** + * Random matrix of n rows, m columns. + * + * @param {number} n The number of rows. + * @param {number} m The number of columns. + */ + Random(n: number, m: number): Matrix; + + /** + * Matrix filled with zeros. + * + * @param {number} n The number of rows. + * @param {number} m The number of columns. + */ + Zero(n: number, m: number): Matrix; + } + + interface LineStatic { + /** + * Constructor function. + * + * @param Array|Vector anchor The anchor vector. + * @param Array|Vector direction The direction vector. + */ + create(anchor: Array|Vector, direction: Array|Vector): Line; + + X: Line; + Y: Line; + Z: Line; + } + interface PlaneStatic { + /** + * Constructor function. + */ + create(anchor: Array|Vector, normal: Array|Vector): Plane; + + /** + * Constructor function. + */ + create(anchor: Array|Vector, v1: Array|Vector, v2: Array|Vector): Plane; + + XY: Plane; + YZ: Plane; + ZX: Plane; + YX: Plane; + } } interface Vector { @@ -239,57 +321,6 @@ interface Vector { setElements(els: Vector|Array): Vector; } -interface MatrixStatic { - /** - * Constructor function. - * - * @param {Array|Array>|Vector|Matrix} elements The elements. - */ - create(elements: Array|Array>|Vector | Matrix): Matrix; - - /** - * Identity matrix of size n. - * - * @param {number} n The size. - */ - I(n: number): Matrix; - - /** - * Diagonal matrix - all off-diagonal elements are zero - * - * @param {any} elements The elements. - */ - Diagonal(elements: Array|Array>|Vector | Matrix): Matrix; - - /** - * Rotation matrix about some axis. If no axis is supplied, assume we're after a 2D transform. - * - * @param {number} theta The angle in radians. - * @param {Vector} a [Optional] The axis. - */ - Rotation(theta: number, a?: Vector): Matrix; - - RotationX(t: number): Matrix; - RotationY(t: number): Matrix; - RotationZ(t: number): Matrix; - - /** - * Random matrix of n rows, m columns. - * - * @param {number} n The number of rows. - * @param {number} m The number of columns. - */ - Random(n: number, m: number): Matrix; - - /** - * Matrix filled with zeros. - * - * @param {number} n The number of rows. - * @param {number} m The number of columns. - */ - Zero(n: number, m: number): Matrix; -} - interface Matrix { /** * Gets a nested array containing the matrix's elements. @@ -515,20 +546,6 @@ interface Matrix { setElements(matrix: Array|Array>|Vector|Matrix): Matrix; } -interface LineStatic { - /** - * Constructor function. - * - * @param Array|Vector anchor The anchor vector. - * @param Array|Vector direction The direction vector. - */ - create(anchor: Array|Vector, direction: Array|Vector): Line; - - X: Line; - Y: Line; - Z: Line; -} - interface Line { /** * Gets the 3D vector corresponding to a point on the line. @@ -637,23 +654,6 @@ interface Line { setVectors(anchor: Array|Vector, direction: Array|Vector): Line; } -interface PlaneStatic { - /** - * Constructor function. - */ - create(anchor: Array|Vector, normal: Array|Vector): Plane; - - /** - * Constructor function. - */ - create(anchor: Array|Vector, v1: Array|Vector, v2: Array|Vector): Plane; - - XY: Plane; - YZ: Plane; - ZX: Plane; - YX: Plane; -} - interface Plane { /** * Gets the 3D vector corresponding to a point in the plane. @@ -780,10 +780,10 @@ declare module Sylvester { export var precision: number; } -declare var Vector: VectorStatic; -declare var Matrix: MatrixStatic; -declare var Line: LineStatic; -declare var Plane: PlaneStatic; +declare var Vector: Sylvester.VectorStatic; +declare var Matrix: Sylvester.MatrixStatic; +declare var Line: Sylvester.LineStatic; +declare var Plane: Sylvester.PlaneStatic; /** * Constructor function. From 9c433c79496fb2b79a6e87f7e4be5d3bc3dfdf5a Mon Sep 17 00:00:00 2001 From: Dominic Alie Date: Mon, 2 Nov 2015 09:52:28 -0500 Subject: [PATCH 3/3] Renamed test file --- sylvester/{sylvester-test.ts => sylvester-tests.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename sylvester/{sylvester-test.ts => sylvester-tests.ts} (100%) diff --git a/sylvester/sylvester-test.ts b/sylvester/sylvester-tests.ts similarity index 100% rename from sylvester/sylvester-test.ts rename to sylvester/sylvester-tests.ts