gl2d
Version:
2D graphics package for WebGL
369 lines • 11.3 kB
JavaScript
"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");
/**
* A two-dimensional vector with (x,y) components.
*/
var Vec2 = (function () {
/**
* A two-dimensional vector with (x,y) components.
*/
function Vec2(x, y) {
if (x === void 0) { x = 0; }
if (y === void 0) { y = 0; }
this.x = x;
this.y = y;
}
Vec2.fromPointToPoint = function (initial, terminal) {
var vec2 = new Vec2();
vec2.setFromPointToPoint(initial, terminal);
return vec2;
};
Vec2.create = function (other) {
var vec2 = new Vec2();
vec2.set(other);
return vec2;
};
/**
* Computes the length of this Vec2.
*/
Vec2.prototype.length = function () {
return Math.sqrt(this.length2());
};
/**
* Computes the length squared of this Vec2.
*/
Vec2.prototype.length2 = function () {
return this.x * this.x + this.y * this.y;
};
/**
* Sets this Vec2 to a vector from the initial point to the terminal point.
*/
Vec2.prototype.setFromPointToPoint = function (initial, terminal) {
this.x = terminal.x - initial.x;
this.y = terminal.y - initial.y;
};
/**
* Computes the dot product of this Vec2 with the other Vec2.
*/
Vec2.prototype.dot = function (other) {
return this.x * other.x + this.y * other.y;
};
/**
* Computes the cross product of this Vec2 with the other Vec2.
*/
Vec2.prototype.cross = function (other) {
return (this.x * other.y) - (other.x * this.y);
};
/**
* Inverts this Vec2.
*/
Vec2.prototype.invert = function () {
this.x = -this.x;
this.y = -this.y;
};
/**
* Normalizes this Vec2 so that it has a length of one.
*/
Vec2.prototype.normalize = function () {
this.divScalar(this.length());
};
/**
* Rotates this Vec2 90 degrees to the left (CCW).
*/
Vec2.prototype.rotateLeft = function () {
var x = this.x;
this.x = -this.y;
this.y = x;
};
/**
* Rotates this Vec2 90 degrees to the right (CW).
*/
Vec2.prototype.rotateRight = function () {
var x = this.x;
this.x = this.y;
this.y = -x;
};
/**
* Sets each component of this Vec2 to that of the other Vec2.
*/
Vec2.prototype.set = function (other) {
this.x = other.x;
this.y = other.y;
};
/**
* Sets each component of this Vec2.
*/
Vec2.prototype.set$ = function (x, y) {
this.x = x;
this.y = y;
};
/**
* Sets each component of this Vec2 to the specified scalar.
*/
Vec2.prototype.setScalar = function (k) {
this.x = k;
this.y = k;
};
/**
* Adds the other Vec2 to this Vec2 componentwise.
*/
Vec2.prototype.add = function (other) {
this.x += other.x;
this.y += other.y;
};
/**
* Adds the specified values to this Vec2 componentwise.
*/
Vec2.prototype.add$ = function (x, y) {
this.x += x;
this.y += y;
};
/**
* Subtracts the other Vec2 from this Vec2 componentwise.
*/
Vec2.prototype.subtract = function (other) {
this.x -= other.x;
this.y -= other.y;
};
/**
* Subtracts the specified values from this Vec2 componentwise.
*/
Vec2.prototype.subtract$ = function (x, y) {
this.x -= x;
this.y -= y;
};
/**
* Multiplies each component of this Vec2 by the specified scalar.
*/
Vec2.prototype.mulScalar = function (k) {
this.x *= k;
this.y *= k;
};
/**
* Divides each component of this Vec2 by the specified scalar.
*/
Vec2.prototype.divScalar = function (k) {
this.x /= k;
this.y /= k;
};
/**
* Checks if each component of this Vec2 is exactly equal to that of the other Vec2.
*/
Vec2.prototype.equals = function (other) {
return this.x === other.x && this.y === other.y;
};
/**
* Checks if each component of this Vec2 is exactly equal to the specified scalar.
*/
Vec2.prototype.equalsScalar = function (k) {
return this.x === k && this.y === k;
};
/**
* Checks if each component of this Vec2 is approximately equal to that of the other Vec2.
*/
Vec2.prototype.epsilonEquals = function (other, e) {
return Math.abs(this.x - other.x) <= e && Math.abs(this.y - other.y) <= e;
};
/**
* Checks if each component of this Vec2 is approximately equal to the specified scalar.
*/
Vec2.prototype.epsilonEqualsScalar = function (k, e) {
return Math.abs(this.x - k) <= e && Math.abs(this.y - k) <= e;
};
/**
* Returns a string representation of this Vec2.
*/
Vec2.prototype.toString = function () {
return "{ x: " + this.x + ", y: " + this.y + " }";
};
return Vec2;
}());
exports.Vec2 = Vec2;
/**
* A Vec2 backed by a Float32Array.
*/
var Vec2Struct = (function (_super) {
__extends(Vec2Struct, _super);
/**
* Creates a Vec2 struct backed by the specified data.
*/
function Vec2Struct(data) {
if (data === void 0) { data = new Float32Array(2); }
return _super.call(this, data) || this;
}
Vec2Struct.fromPointToPoint = function (initial, terminal) {
var vec2 = new Vec2Struct();
vec2.setFromPointToPoint(initial, terminal);
return vec2;
};
Vec2Struct.create = function (other) {
var vec2 = new Vec2Struct();
vec2.set(other);
return vec2;
};
Vec2Struct.create$ = function (x, y) {
var vec2 = new Vec2Struct();
vec2.set$(x, y);
return vec2;
};
Object.defineProperty(Vec2Struct.prototype, "x", {
/**
* The X component of this Vec2.
*/
get: function () {
return this.data[0];
},
/**
* The X component of this Vec2.
*/
set: function (value) {
this.data[0] = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Vec2Struct.prototype, "y", {
/**
* The Y component of this Vec2.
*/
get: function () {
return this.data[1];
},
/**
* The Y component of this Vec2.
*/
set: function (value) {
this.data[1] = value;
},
enumerable: true,
configurable: true
});
return Vec2Struct;
}(struct_1.Struct));
exports.Vec2Struct = Vec2Struct;
mixin_1.applyMixins(Vec2Struct, Vec2);
/**
* A Vec2 buffer backed by a Float32Array.
*/
var Vec2Buffer = (function (_super) {
__extends(Vec2Buffer, _super);
function Vec2Buffer() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* Creates an empty Vec2 buffer with the specified Vec2 capacity.
*/
Vec2Buffer.create = function (capacity) {
return new Vec2Buffer(new Float32Array(capacity * 2));
};
Object.defineProperty(Vec2Buffer.prototype, "x", {
/**
* The X component of the current Vec2.
*/
get: function () {
return this.data[this.dataPosition + 0];
},
/**
* The X component of the current Vec2.
*/
set: function (value) {
this.data[this.dataPosition + 0] = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Vec2Buffer.prototype, "y", {
/**
* The Y component of the current Vec2.
*/
get: function () {
return this.data[this.dataPosition + 1];
},
/**
* The Y component of the current Vec2.
*/
set: function (value) {
this.data[this.dataPosition + 1] = value;
},
enumerable: true,
configurable: true
});
/**
* Gets the number of properties in a Vec2, namely 2.
*/
Vec2Buffer.prototype.structLength = function () {
return 2;
};
/**
* Gets the components of the Vec2 at the specified position of this buffer.
*/
Vec2Buffer.prototype.aget = function (position, dst) {
if (dst === void 0) {
dst = new Vec2();
}
;
var dataPos = position * this.structLength();
dst.x = this.data[dataPos++];
dst.y = this.data[dataPos++];
return dst;
};
/**
* Gets the components of the current Vec2, then moves to the next position of this buffer.
*/
Vec2Buffer.prototype.rget = function (dst) {
if (dst === void 0) {
dst = new Vec2();
}
;
dst.x = this.data[this.dataPosition++];
dst.y = this.data[this.dataPosition++];
return dst;
};
/**
* Sets each component of the Vec2 at the specified position to that of the src Vec2.
*/
Vec2Buffer.prototype.aset = function (position, src) {
var dataPos = position * this.structLength();
this.data[dataPos++] = src.x;
this.data[dataPos++] = src.y;
};
/**
* Sets each component of the Vec2 at the specified position.
*/
Vec2Buffer.prototype.aset$ = function (position, x, y) {
var dataPos = position * this.structLength();
this.data[dataPos++] = x;
this.data[dataPos++] = y;
};
/**
* Sets each component of the current Vec2 to that of the src Vec2, then moves to the next position of this buffer.
*/
Vec2Buffer.prototype.rset = function (src) {
this.data[this.dataPosition++] = src.x;
this.data[this.dataPosition++] = src.y;
};
/**
* Sets each component of the current Vec2, then moves to the next position of this buffer.
*/
Vec2Buffer.prototype.rset$ = function (x, y) {
this.data[this.dataPosition++] = x;
this.data[this.dataPosition++] = y;
};
return Vec2Buffer;
}(buffer_1.StructBuffer));
exports.Vec2Buffer = Vec2Buffer;
mixin_1.applyMixins(Vec2Buffer, Vec2);
//# sourceMappingURL=vec2.js.map