convex-pixel
Version:
The library for creating pseudo 3d interactive scenes based on webgl
152 lines • 3.82 kB
JavaScript
/**
* Vector2D
* Uses pooling for speed performance
*/
export class Vector2D {
constructor(x = 0, y = 0) {
this.x = x;
this.y = y;
}
static get poolCount() {
return this._pool.length;
}
static new(x = 0, y = 0) {
if (Vector2D._pool.length > 0) {
const vect = Vector2D._pool.pop();
if (vect) {
return vect.set(x, y);
}
}
return new Vector2D(x, y);
}
set(x, y) {
this.x = x;
this.y = y;
return this;
}
/**
* @deprecated
*/
move(x, y) {
this.x = x;
this.y = y;
return this;
}
from(vector) {
this.x = vector.x;
this.y = vector.y;
return this;
}
free() {
Vector2D._pool.push(this);
}
valueOf() {
return { x: this.x, y: this.y };
}
clone() {
return Vector2D.new(this.x, this.y);
}
add(vector, isClone = true) {
if (!isClone) {
this.x += vector.x;
this.y += vector.y;
return this;
}
return Vector2D.new(this.x + vector.x, this.y + vector.y);
}
deduct(vector, isClone = true) {
if (!isClone) {
this.x -= vector.x;
this.y -= vector.y;
return this;
}
return Vector2D.new(this.x - vector.x, this.y - vector.y);
}
measureDistance(vector, xAxis = true, yAxis = true) {
const a = Math.max(this.x, vector.x) - Math.min(this.x, vector.x);
const b = Math.max(this.y, vector.y) - Math.min(this.y, vector.y);
if (xAxis && yAxis)
return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
else if (xAxis)
return a;
else if (yAxis)
return b;
return 0;
}
comparePoint(vector) {
return this.x === vector.x && this.y === vector.y;
}
empty() {
this.x = this.y = 0;
}
}
Vector2D._pool = new Array();
// tslint:disable-next-line: max-classes-per-file
export class Vector3D {
constructor(x = 0, y = 0, z = 0) {
this.x = x;
this.y = y;
this.z = z;
}
static get poolCount() {
return this._pool.length;
}
static new(x = 0, y = 0, z = 0) {
if (Vector3D._pool.length > 0) {
const vect = Vector3D._pool.pop();
if (vect) {
return vect.set(x, y, z);
}
}
return new Vector3D(x, y, z);
}
set(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
return this;
}
free() {
Vector3D._pool.push(this);
}
valueOf() {
return { x: this.x, y: this.y, z: this.z };
}
}
Vector3D._pool = new Array();
// tslint:disable-next-line: max-classes-per-file
export class Vector4D {
constructor(x = 0, y = 0, z = 0, t = 0) {
this.x = x;
this.y = y;
this.z = z;
this.t = t;
}
static get poolCount() {
return this._pool.length;
}
static new(x = 0, y = 0, z = 0, t = 0) {
if (Vector4D._pool.length > 0) {
const vect = Vector4D._pool.pop();
if (vect) {
return vect.set(x, y, z, t);
}
}
return new Vector4D(x, y, z, t);
}
set(x, y, z, t) {
this.x = x;
this.y = y;
this.z = z;
this.t = t;
return this;
}
free() {
Vector4D._pool.push(this);
}
valueOf() {
return { x: this.x, y: this.y, z: this.z, t: this.t };
}
}
Vector4D._pool = new Array();
//# sourceMappingURL=vector.js.map