UNPKG

ts-scikit

Version:

A scientific toolkit written in Typescript

79 lines 2.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Point2 = void 0; const tuple2_1 = require("./tuple2"); const vector2_1 = require("./vector2"); /** * A point with 2 coordinates: x and y. */ class Point2 extends tuple2_1.Tuple2 { constructor(x, y) { super(x, y); } /** * Returns the point q = p + v for this point p and the specified vector v. * @param v the vector v. * @returns the point q = p + v. */ plus(v) { return new Point2(this.x + v.x, this.y + v.y); } /** * Moves this point p by adding the specified vector v. * @param v the vector v. * @returns a reference to this point, moved along vector v. */ plusEquals(v) { this.x += v.x; this.y += v.y; return this; } /** * Returns the point or vector q = p - v for this point p. * If v is a vector, q is a point translated along vector v. * If v is a point, q is the vector difference. * @param v the point or vector v. * @returns the vector or point q = p - v. */ minus(v) { if (v instanceof vector2_1.Vector2) { return new Point2(this.x - v.x, this.y - v.y); } else { return new vector2_1.Vector2(this.x - v.x, this.y - v.y); } } /** * Moves this point by subtracting the specified vector v * @param v the vector v. * @returns a reference to this point, moved along vector v. */ minusEquals(v) { this.x -= v.x; this.y -= v.y; return this; } /** * Returns an affine combination of this point p and the specified point q. * @param a the weight of the point q. * @param q the point q. * @returns the affine combination (1 - a) * p + a * q. */ affine(a, q) { const b = 1.0 - a; const p = this.clone(); return new Point2(b * p.x + a * q.x, b * p.y + a * q.y); } /** * Computes the distance between this point p and the specified point q. * @param q the point q. * @returns the distance |q - p|. */ distanceTo(q) { const dx = this.x - q.x; const dy = this.y - q.y; return Math.sqrt(dx * dx + dy * dy); } } exports.Point2 = Point2; //# sourceMappingURL=point2.js.map