gl2d
Version:
2D graphics package for WebGL
163 lines • 6.88 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 rect_1 = require("../struct/rect");
var vec2_1 = require("../struct/vec2");
/**
* Helper class for working with vertex data stored in a Float32Array.
*/
var VertexBuffer = (function (_super) {
__extends(VertexBuffer, _super);
function VertexBuffer() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* Creates an empty VertexBuf with the specified vertex capacity.
*/
VertexBuffer.create = function (capacity) {
return new VertexBuffer(new Float32Array(capacity * 2));
};
/**
* Measures the boundaries of a subset of vertices in this buffer.
* @param offset the offset of the first vertex in the subset.
* @param count the number of vertices in the subset.
*/
VertexBuffer.prototype.measureBoundaries = function (offset, count) {
if (offset === void 0) { offset = 0; }
if (count === void 0) { count = this.capacity() - offset; }
return rect_1.Rect.unionOfPoints$(this.data, offset * this.structLength(), count);
};
/**
* Checks if a polygon (specified by indices into this buffer) contains the specified point.
* @param pt the point to check.
* @param polygonIndices the indices for each of the polygons.
*/
VertexBuffer.prototype.indexedContains = function (pt, polygonIndices) {
return this.indexedContains$(pt.x, pt.y, polygonIndices);
};
/**
* Checks if a polygon (specified by indices into this buffer) contains the specified point.
* @param x the x coordinate of the point to check.
* @param y the y coordinate of the point to check.
* @param polygonIndices the indices for each of the polygons.
*/
VertexBuffer.prototype.indexedContains$ = function (x, y, polygonIndices) {
// Assume the point is not inside the polygon
var inside = false;
// Helper vars:
var prevX;
var prevY;
var currX;
var currY;
var vertexCount = polygonIndices.length;
// Get last point in subset
this.moveToPosition(polygonIndices[vertexCount - 1]);
prevX = this.x;
prevY = this.y;
// Check point against each side of polygon
for (var i = 0; i < vertexCount; i++) {
this.moveToPosition(polygonIndices[i]);
currX = this.x;
currY = this.y;
if (isInside(x, y, prevX, prevY, currX, currY)) {
inside = !inside;
}
prevX = currX;
prevY = currY;
}
return inside;
};
/**
* Checks if a polygon (specified by a subset of vertices in this buffer) contains the specified point.
* @param pt the point to check.
* @param offset the offset of the first polygon vertex. Defaults to zero.
* @param count the number of polygon vertices. Defaults to the number of vertices in this buffer.
*/
VertexBuffer.prototype.contains = function (pt, offset, count) {
return this.contains$(pt.x, pt.y, offset, count);
};
/**
* Checks if a polygon (specified by a subset of vertices in this buffer) contains the specified point.
* @param x the x coordinate of the point to check.
* @param y the y coordinate of the point to check.
* @param offset the offset of the first polygon vertex. Defaults to zero.
* @param count the number of polygon vertices. Defaults to the number of vertices in this buffer.
*/
VertexBuffer.prototype.contains$ = function (x, y, offset, count) {
if (offset === void 0) { offset = 0; }
if (count === void 0) { count = this.capacity() - offset; }
//Assume the point is not inside the subset
var inside = false;
// Helper vars:
var prevX;
var prevY;
var currX;
var currY;
// Get last point in subset
this.moveToPosition(offset + count - 1);
prevX = this.x;
prevY = this.y;
// Check point against each side of polygon
while (count-- > 0) {
this.moveToPosition(offset++);
currX = this.x;
currY = this.y;
if (isInside(x, y, prevX, prevY, currX, currY)) {
inside = !inside;
}
prevX = currX;
prevY = currY;
}
return inside;
};
/**
* Offsets the specified vertices in this buffer by the specified vector.
* @param offset the offset of the first vertex.
* @param count the number of vertices to include.
*/
VertexBuffer.prototype.offset = function (vec, offset, count) {
this.offset$(vec.x, vec.y, offset, count);
};
/**
* Offsets the specified vertices in this buffer by the vector (dx,dy).
* @param offset the offset of the first vertex.
* @param count the number of vertices to include.
*/
VertexBuffer.prototype.offset$ = function (dx, dy, offset, count) {
if (offset === void 0) { offset = 0; }
if (count === void 0) { count = this.capacity() - offset; }
//Compute the data index of the first point in the subset
var dataIndex = offset * this.structLength();
//Offset each of the points in the subset
for (var i = 0; i <= count; i++) {
this.data[dataIndex++] += dx;
this.data[dataIndex++] += dy;
}
};
/**
* Transforms the specified vertices in this buffer by the specified matrix.
* @param matrix the transformation matrix.
* @param offset the offset of the first vertex.
* @param count the number of vertices to include.
*/
VertexBuffer.prototype.transform = function (matrix, offset, count) {
if (offset === void 0) { offset = 0; }
if (count === void 0) { count = this.capacity() - offset; }
matrix.mapPoints$(this.data, offset * this.structLength());
};
return VertexBuffer;
}(vec2_1.Vec2Buffer));
exports.VertexBuffer = VertexBuffer;
function isInside(x, y, x1, y1, x2, y2) {
return (y1 > y) !== (y2 > y) && x < (x2 - x1) * (y - y1) / (y2 - y1) + x1;
}
//# sourceMappingURL=vertex.js.map