UNPKG

gl2d

Version:

2D graphics package for WebGL

444 lines 14.4 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var struct_1 = require("gulp-structify/struct"); var buffer_1 = require("gulp-structify/buffer"); var mixin_1 = require("gulp-structify/mixin"); /** * The line from (x1,y1) to (x2,y2). */ var Line = (function () { /** * The line from (x1,y1) to (x2,y2). */ function Line(x1, y1, x2, y2) { if (x1 === void 0) { x1 = 0; } if (y1 === void 0) { y1 = 0; } if (x2 === void 0) { x2 = 0; } if (y2 === void 0) { y2 = 0; } this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; } Line.fromPointToPoint = function (p1, p2) { var line = new Line(); line.setFromPointToPoint(p1, p2); return line; }; Line.create = function (other) { var line = new Line(); line.set(other); return line; }; /** * Sets this line to the line from one point to another. */ Line.prototype.setFromPointToPoint = function (p1, p2) { this.x1 = p1.x; this.y1 = p1.y; this.x2 = p2.x; this.y2 = p2.y; }; /** * Checks if the distance from this line to the specified point is less than epsilon. */ Line.prototype.contains = function (pt, epsilon) { return this.contains$(pt.x, pt.y, epsilon); }; /** * Checks if the distance from this line to (x,y) is less than epsilon. */ Line.prototype.contains$ = function (x, y, epsilon) { var ax = this.x1, ay = this.y1, bx = this.x2 - ax, by = this.y2 - ay, t = -1; var vx = x - ax, vy = y - ay; var dot = bx * vx + by * vy; var len2 = bx * bx + by * by; if (len2 != 0) { t = dot / len2; } if (t < 0 || t > 1) { // Line does not contain point return false; } var projX = ax + t * bx; var projY = ay + t * by; var dx = x - projX; var dy = y - projY; var dist2 = dx * dx + dy * dy; return dist2 < (epsilon * epsilon); }; /** * Sets each component of this Line to that of the other Line. */ Line.prototype.set = function (other) { this.x1 = other.x1; this.y1 = other.y1; this.x2 = other.x2; this.y2 = other.y2; }; /** * Sets each component of this Line. */ Line.prototype.set$ = function (x1, y1, x2, y2) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; }; /** * Sets each component of this Line to the specified scalar. */ Line.prototype.setScalar = function (k) { this.x1 = k; this.y1 = k; this.x2 = k; this.y2 = k; }; /** * Adds the other Line to this Line componentwise. */ Line.prototype.add = function (other) { this.x1 += other.x1; this.y1 += other.y1; this.x2 += other.x2; this.y2 += other.y2; }; /** * Adds the specified values to this Line componentwise. */ Line.prototype.add$ = function (x1, y1, x2, y2) { this.x1 += x1; this.y1 += y1; this.x2 += x2; this.y2 += y2; }; /** * Subtracts the other Line from this Line componentwise. */ Line.prototype.subtract = function (other) { this.x1 -= other.x1; this.y1 -= other.y1; this.x2 -= other.x2; this.y2 -= other.y2; }; /** * Subtracts the specified values from this Line componentwise. */ Line.prototype.subtract$ = function (x1, y1, x2, y2) { this.x1 -= x1; this.y1 -= y1; this.x2 -= x2; this.y2 -= y2; }; /** * Multiplies each component of this Line by the specified scalar. */ Line.prototype.mulScalar = function (k) { this.x1 *= k; this.y1 *= k; this.x2 *= k; this.y2 *= k; }; /** * Divides each component of this Line by the specified scalar. */ Line.prototype.divScalar = function (k) { this.x1 /= k; this.y1 /= k; this.x2 /= k; this.y2 /= k; }; /** * Checks if each component of this Line is exactly equal to that of the other Line. */ Line.prototype.equals = function (other) { return this.x1 === other.x1 && this.y1 === other.y1 && this.x2 === other.x2 && this.y2 === other.y2; }; /** * Checks if each component of this Line is exactly equal to the specified scalar. */ Line.prototype.equalsScalar = function (k) { return this.x1 === k && this.y1 === k && this.x2 === k && this.y2 === k; }; /** * Checks if each component of this Line is approximately equal to that of the other Line. */ Line.prototype.epsilonEquals = function (other, e) { return Math.abs(this.x1 - other.x1) <= e && Math.abs(this.y1 - other.y1) <= e && Math.abs(this.x2 - other.x2) <= e && Math.abs(this.y2 - other.y2) <= e; }; /** * Checks if each component of this Line is approximately equal to the specified scalar. */ Line.prototype.epsilonEqualsScalar = function (k, e) { return Math.abs(this.x1 - k) <= e && Math.abs(this.y1 - k) <= e && Math.abs(this.x2 - k) <= e && Math.abs(this.y2 - k) <= e; }; /** * Returns a string representation of this Line. */ Line.prototype.toString = function () { return "{ x1: " + this.x1 + ", y1: " + this.y1 + ", x2: " + this.x2 + ", y2: " + this.y2 + " }"; }; return Line; }()); exports.Line = Line; /** * A Line backed by a Float32Array. */ var LineStruct = (function (_super) { __extends(LineStruct, _super); /** * Creates a Line struct backed by the specified data. */ function LineStruct(data) { if (data === void 0) { data = new Float32Array(4); } return _super.call(this, data) || this; } LineStruct.fromPointToPoint = function (p1, p2) { var line = new LineStruct(); line.setFromPointToPoint(p1, p2); return line; }; LineStruct.create = function (other) { var line = new LineStruct(); line.set(other); return line; }; LineStruct.create$ = function (x1, y1, x2, y2) { var line = new LineStruct(); line.set$(x1, y1, x2, y2); return line; }; Object.defineProperty(LineStruct.prototype, "x1", { /** * The X coordinate of the point at the start of this Line. */ get: function () { return this.data[0]; }, /** * The X coordinate of the point at the start of this Line. */ set: function (value) { this.data[0] = value; }, enumerable: true, configurable: true }); Object.defineProperty(LineStruct.prototype, "y1", { /** * The Y coordinate of the point at the start of this Line. */ get: function () { return this.data[1]; }, /** * The Y coordinate of the point at the start of this Line. */ set: function (value) { this.data[1] = value; }, enumerable: true, configurable: true }); Object.defineProperty(LineStruct.prototype, "x2", { /** * The X coordinate of the point at the end of this Line. */ get: function () { return this.data[2]; }, /** * The X coordinate of the point at the end of this Line. */ set: function (value) { this.data[2] = value; }, enumerable: true, configurable: true }); Object.defineProperty(LineStruct.prototype, "y2", { /** * The Y coordinate of the point at the end of this Line. */ get: function () { return this.data[3]; }, /** * The Y coordinate of the point at the end of this Line. */ set: function (value) { this.data[3] = value; }, enumerable: true, configurable: true }); return LineStruct; }(struct_1.Struct)); exports.LineStruct = LineStruct; mixin_1.applyMixins(LineStruct, Line); /** * A Line buffer backed by a Float32Array. */ var LineBuffer = (function (_super) { __extends(LineBuffer, _super); function LineBuffer() { return _super !== null && _super.apply(this, arguments) || this; } /** * Creates an empty Line buffer with the specified Line capacity. */ LineBuffer.create = function (capacity) { return new LineBuffer(new Float32Array(capacity * 4)); }; Object.defineProperty(LineBuffer.prototype, "x1", { /** * The X coordinate of the point at the start of the current Line. */ get: function () { return this.data[this.dataPosition + 0]; }, /** * The X coordinate of the point at the start of the current Line. */ set: function (value) { this.data[this.dataPosition + 0] = value; }, enumerable: true, configurable: true }); Object.defineProperty(LineBuffer.prototype, "y1", { /** * The Y coordinate of the point at the start of the current Line. */ get: function () { return this.data[this.dataPosition + 1]; }, /** * The Y coordinate of the point at the start of the current Line. */ set: function (value) { this.data[this.dataPosition + 1] = value; }, enumerable: true, configurable: true }); Object.defineProperty(LineBuffer.prototype, "x2", { /** * The X coordinate of the point at the end of the current Line. */ get: function () { return this.data[this.dataPosition + 2]; }, /** * The X coordinate of the point at the end of the current Line. */ set: function (value) { this.data[this.dataPosition + 2] = value; }, enumerable: true, configurable: true }); Object.defineProperty(LineBuffer.prototype, "y2", { /** * The Y coordinate of the point at the end of the current Line. */ get: function () { return this.data[this.dataPosition + 3]; }, /** * The Y coordinate of the point at the end of the current Line. */ set: function (value) { this.data[this.dataPosition + 3] = value; }, enumerable: true, configurable: true }); /** * Gets the number of properties in a Line, namely 4. */ LineBuffer.prototype.structLength = function () { return 4; }; /** * Gets the components of the Line at the specified position of this buffer. */ LineBuffer.prototype.aget = function (position, dst) { if (dst === void 0) { dst = new Line(); } ; var dataPos = position * this.structLength(); dst.x1 = this.data[dataPos++]; dst.y1 = this.data[dataPos++]; dst.x2 = this.data[dataPos++]; dst.y2 = this.data[dataPos++]; return dst; }; /** * Gets the components of the current Line, then moves to the next position of this buffer. */ LineBuffer.prototype.rget = function (dst) { if (dst === void 0) { dst = new Line(); } ; dst.x1 = this.data[this.dataPosition++]; dst.y1 = this.data[this.dataPosition++]; dst.x2 = this.data[this.dataPosition++]; dst.y2 = this.data[this.dataPosition++]; return dst; }; /** * Sets each component of the Line at the specified position to that of the src Line. */ LineBuffer.prototype.aset = function (position, src) { var dataPos = position * this.structLength(); this.data[dataPos++] = src.x1; this.data[dataPos++] = src.y1; this.data[dataPos++] = src.x2; this.data[dataPos++] = src.y2; }; /** * Sets each component of the Line at the specified position. */ LineBuffer.prototype.aset$ = function (position, x1, y1, x2, y2) { var dataPos = position * this.structLength(); this.data[dataPos++] = x1; this.data[dataPos++] = y1; this.data[dataPos++] = x2; this.data[dataPos++] = y2; }; /** * Sets each component of the current Line to that of the src Line, then moves to the next position of this buffer. */ LineBuffer.prototype.rset = function (src) { this.data[this.dataPosition++] = src.x1; this.data[this.dataPosition++] = src.y1; this.data[this.dataPosition++] = src.x2; this.data[this.dataPosition++] = src.y2; }; /** * Sets each component of the current Line, then moves to the next position of this buffer. */ LineBuffer.prototype.rset$ = function (x1, y1, x2, y2) { this.data[this.dataPosition++] = x1; this.data[this.dataPosition++] = y1; this.data[this.dataPosition++] = x2; this.data[this.dataPosition++] = y2; }; return LineBuffer; }(buffer_1.StructBuffer)); exports.LineBuffer = LineBuffer; mixin_1.applyMixins(LineBuffer, Line); //# sourceMappingURL=line.js.map