gl2d
Version:
2D graphics package for WebGL
435 lines • 14.9 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");
/**
* Ellipse with semi axes (rx,ry) centered at the point (cx,cy).
*/
var Ellipse = (function () {
/**
* Ellipse with semi axes (rx,ry) centered at the point (cx,cy).
*/
function Ellipse(rx, ry, cx, cy) {
if (rx === void 0) { rx = 0; }
if (ry === void 0) { ry = 0; }
if (cx === void 0) { cx = 0; }
if (cy === void 0) { cy = 0; }
this.rx = rx;
this.ry = ry;
this.cx = cx;
this.cy = cy;
}
Ellipse.fromRect = function (bounds) {
var ellipse = new Ellipse();
ellipse.setFromRect(bounds);
return ellipse;
};
Ellipse.create = function (other) {
var ellipse = new Ellipse();
ellipse.set(other);
return ellipse;
};
/**
* Sets this ellipse to an ellipse bounded by the specified rect.
*/
Ellipse.prototype.setFromRect = function (bounds) {
this.rx = bounds.width() / 2;
this.ry = bounds.height() / 2;
this.cx = bounds.centerX();
this.cy = bounds.centerY();
};
/**
* Checks if this ellipse contains the specified point.
* @param pt the point to check.
*/
Ellipse.prototype.contains = function (pt) {
return this.contains$(pt.x, pt.y);
};
/**
* Checks if this ellipse contains the point (x,y).
* @param x the x coordinate of the point.
* @param y the y coordinate of the point.
*/
Ellipse.prototype.contains$ = function (x, y) {
var dx = this.cx - x;
var dy = this.cy - y;
var sx = dx / this.rx;
var sy = dy / this.ry;
return (sx * sx) + (sy * sy) <= 1;
};
/**
* Sets each component of this Ellipse to that of the other Ellipse.
*/
Ellipse.prototype.set = function (other) {
this.rx = other.rx;
this.ry = other.ry;
this.cx = other.cx;
this.cy = other.cy;
};
/**
* Sets each component of this Ellipse.
*/
Ellipse.prototype.set$ = function (rx, ry, cx, cy) {
this.rx = rx;
this.ry = ry;
this.cx = cx;
this.cy = cy;
};
/**
* Sets each component of this Ellipse to the specified scalar.
*/
Ellipse.prototype.setScalar = function (k) {
this.rx = k;
this.ry = k;
this.cx = k;
this.cy = k;
};
/**
* Adds the other Ellipse to this Ellipse componentwise.
*/
Ellipse.prototype.add = function (other) {
this.rx += other.rx;
this.ry += other.ry;
this.cx += other.cx;
this.cy += other.cy;
};
/**
* Adds the specified values to this Ellipse componentwise.
*/
Ellipse.prototype.add$ = function (rx, ry, cx, cy) {
this.rx += rx;
this.ry += ry;
this.cx += cx;
this.cy += cy;
};
/**
* Subtracts the other Ellipse from this Ellipse componentwise.
*/
Ellipse.prototype.subtract = function (other) {
this.rx -= other.rx;
this.ry -= other.ry;
this.cx -= other.cx;
this.cy -= other.cy;
};
/**
* Subtracts the specified values from this Ellipse componentwise.
*/
Ellipse.prototype.subtract$ = function (rx, ry, cx, cy) {
this.rx -= rx;
this.ry -= ry;
this.cx -= cx;
this.cy -= cy;
};
/**
* Multiplies each component of this Ellipse by the specified scalar.
*/
Ellipse.prototype.mulScalar = function (k) {
this.rx *= k;
this.ry *= k;
this.cx *= k;
this.cy *= k;
};
/**
* Divides each component of this Ellipse by the specified scalar.
*/
Ellipse.prototype.divScalar = function (k) {
this.rx /= k;
this.ry /= k;
this.cx /= k;
this.cy /= k;
};
/**
* Checks if each component of this Ellipse is exactly equal to that of the other Ellipse.
*/
Ellipse.prototype.equals = function (other) {
return this.rx === other.rx && this.ry === other.ry && this.cx === other.cx && this.cy === other.cy;
};
/**
* Checks if each component of this Ellipse is exactly equal to the specified scalar.
*/
Ellipse.prototype.equalsScalar = function (k) {
return this.rx === k && this.ry === k && this.cx === k && this.cy === k;
};
/**
* Checks if each component of this Ellipse is approximately equal to that of the other Ellipse.
*/
Ellipse.prototype.epsilonEquals = function (other, e) {
return Math.abs(this.rx - other.rx) <= e && Math.abs(this.ry - other.ry) <= e && Math.abs(this.cx - other.cx) <= e && Math.abs(this.cy - other.cy) <= e;
};
/**
* Checks if each component of this Ellipse is approximately equal to the specified scalar.
*/
Ellipse.prototype.epsilonEqualsScalar = function (k, e) {
return Math.abs(this.rx - k) <= e && Math.abs(this.ry - k) <= e && Math.abs(this.cx - k) <= e && Math.abs(this.cy - k) <= e;
};
/**
* Returns a string representation of this Ellipse.
*/
Ellipse.prototype.toString = function () {
return "{ rx: " + this.rx + ", ry: " + this.ry + ", cx: " + this.cx + ", cy: " + this.cy + " }";
};
return Ellipse;
}());
exports.Ellipse = Ellipse;
/**
* A Ellipse backed by a Float32Array.
*/
var EllipseStruct = (function (_super) {
__extends(EllipseStruct, _super);
/**
* Creates a Ellipse struct backed by the specified data.
*/
function EllipseStruct(data) {
if (data === void 0) { data = new Float32Array(4); }
return _super.call(this, data) || this;
}
EllipseStruct.fromRect = function (bounds) {
var ellipse = new EllipseStruct();
ellipse.setFromRect(bounds);
return ellipse;
};
EllipseStruct.create = function (other) {
var ellipse = new EllipseStruct();
ellipse.set(other);
return ellipse;
};
EllipseStruct.create$ = function (rx, ry, cx, cy) {
var ellipse = new EllipseStruct();
ellipse.set$(rx, ry, cx, cy);
return ellipse;
};
Object.defineProperty(EllipseStruct.prototype, "rx", {
/**
* The semi x axis of this ellipse, that is, the distance from the center of this ellipse to its left and right vertices.
*/
get: function () {
return this.data[0];
},
/**
* The semi x axis of this ellipse, that is, the distance from the center of this ellipse to its left and right vertices.
*/
set: function (value) {
this.data[0] = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(EllipseStruct.prototype, "ry", {
/**
* The semi y axis of this ellipse, that is, the distance from the center of this ellipse to its top and bottom vertices.
*/
get: function () {
return this.data[1];
},
/**
* The semi y axis of this ellipse, that is, the distance from the center of this ellipse to its top and bottom vertices.
*/
set: function (value) {
this.data[1] = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(EllipseStruct.prototype, "cx", {
/**
* The X coordinate of the point at the center of this ellipse.
*/
get: function () {
return this.data[2];
},
/**
* The X coordinate of the point at the center of this ellipse.
*/
set: function (value) {
this.data[2] = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(EllipseStruct.prototype, "cy", {
/**
* The Y coordinate of the point at the center of this ellipse.
*/
get: function () {
return this.data[3];
},
/**
* The Y coordinate of the point at the center of this ellipse.
*/
set: function (value) {
this.data[3] = value;
},
enumerable: true,
configurable: true
});
return EllipseStruct;
}(struct_1.Struct));
exports.EllipseStruct = EllipseStruct;
mixin_1.applyMixins(EllipseStruct, Ellipse);
/**
* A Ellipse buffer backed by a Float32Array.
*/
var EllipseBuffer = (function (_super) {
__extends(EllipseBuffer, _super);
function EllipseBuffer() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* Creates an empty Ellipse buffer with the specified Ellipse capacity.
*/
EllipseBuffer.create = function (capacity) {
return new EllipseBuffer(new Float32Array(capacity * 4));
};
Object.defineProperty(EllipseBuffer.prototype, "rx", {
/**
* The semi x axis of this ellipse, that is, the distance from the center of this ellipse to its left and right vertices.
*/
get: function () {
return this.data[this.dataPosition + 0];
},
/**
* The semi x axis of this ellipse, that is, the distance from the center of this ellipse to its left and right vertices.
*/
set: function (value) {
this.data[this.dataPosition + 0] = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(EllipseBuffer.prototype, "ry", {
/**
* The semi y axis of this ellipse, that is, the distance from the center of this ellipse to its top and bottom vertices.
*/
get: function () {
return this.data[this.dataPosition + 1];
},
/**
* The semi y axis of this ellipse, that is, the distance from the center of this ellipse to its top and bottom vertices.
*/
set: function (value) {
this.data[this.dataPosition + 1] = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(EllipseBuffer.prototype, "cx", {
/**
* The X coordinate of the point at the center of this ellipse.
*/
get: function () {
return this.data[this.dataPosition + 2];
},
/**
* The X coordinate of the point at the center of this ellipse.
*/
set: function (value) {
this.data[this.dataPosition + 2] = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(EllipseBuffer.prototype, "cy", {
/**
* The Y coordinate of the point at the center of this ellipse.
*/
get: function () {
return this.data[this.dataPosition + 3];
},
/**
* The Y coordinate of the point at the center of this ellipse.
*/
set: function (value) {
this.data[this.dataPosition + 3] = value;
},
enumerable: true,
configurable: true
});
/**
* Gets the number of properties in a Ellipse, namely 4.
*/
EllipseBuffer.prototype.structLength = function () {
return 4;
};
/**
* Gets the components of the Ellipse at the specified position of this buffer.
*/
EllipseBuffer.prototype.aget = function (position, dst) {
if (dst === void 0) {
dst = new Ellipse();
}
;
var dataPos = position * this.structLength();
dst.rx = this.data[dataPos++];
dst.ry = this.data[dataPos++];
dst.cx = this.data[dataPos++];
dst.cy = this.data[dataPos++];
return dst;
};
/**
* Gets the components of the current Ellipse, then moves to the next position of this buffer.
*/
EllipseBuffer.prototype.rget = function (dst) {
if (dst === void 0) {
dst = new Ellipse();
}
;
dst.rx = this.data[this.dataPosition++];
dst.ry = this.data[this.dataPosition++];
dst.cx = this.data[this.dataPosition++];
dst.cy = this.data[this.dataPosition++];
return dst;
};
/**
* Sets each component of the Ellipse at the specified position to that of the src Ellipse.
*/
EllipseBuffer.prototype.aset = function (position, src) {
var dataPos = position * this.structLength();
this.data[dataPos++] = src.rx;
this.data[dataPos++] = src.ry;
this.data[dataPos++] = src.cx;
this.data[dataPos++] = src.cy;
};
/**
* Sets each component of the Ellipse at the specified position.
*/
EllipseBuffer.prototype.aset$ = function (position, rx, ry, cx, cy) {
var dataPos = position * this.structLength();
this.data[dataPos++] = rx;
this.data[dataPos++] = ry;
this.data[dataPos++] = cx;
this.data[dataPos++] = cy;
};
/**
* Sets each component of the current Ellipse to that of the src Ellipse, then moves to the next position of this buffer.
*/
EllipseBuffer.prototype.rset = function (src) {
this.data[this.dataPosition++] = src.rx;
this.data[this.dataPosition++] = src.ry;
this.data[this.dataPosition++] = src.cx;
this.data[this.dataPosition++] = src.cy;
};
/**
* Sets each component of the current Ellipse, then moves to the next position of this buffer.
*/
EllipseBuffer.prototype.rset$ = function (rx, ry, cx, cy) {
this.data[this.dataPosition++] = rx;
this.data[this.dataPosition++] = ry;
this.data[this.dataPosition++] = cx;
this.data[this.dataPosition++] = cy;
};
return EllipseBuffer;
}(buffer_1.StructBuffer));
exports.EllipseBuffer = EllipseBuffer;
mixin_1.applyMixins(EllipseBuffer, Ellipse);
//# sourceMappingURL=ellipse.js.map