refactor: efficiency

This commit is contained in:
cha0s 2019-03-25 20:49:42 -05:00
parent 742228ec3f
commit 1fff0cdb16

View File

@ -33,19 +33,28 @@ class TilesBase {
slice(rectangle) {
const tilesRectangle = this.rectangle;
// Get intersection.
if (!Rectangle.intersects(rectangle, tilesRectangle)) {
return [];
}
let [x, y, width, height] = Rectangle.intersection(rectangle, tilesRectangle);
const rowWidth = tilesRectangle[2];
const slice = new Array(width * height);
for (let j = 0; j < height; ++j) {
for (let i = 0; i < width; ++i) {
slice[y * width + x] = this.data[y * rowWidth + x];
let [x, y, sliceWidth, sliceHeight] = Rectangle.intersection(
rectangle,
tilesRectangle,
);
// No muls in the loop.
let sliceRow = y * sliceWidth;
const dataWidth = this.width;
let dataRow = y * dataWidth;
// Copy slice.
const slice = new Array(sliceWidth * sliceHeight);
for (let j = 0; j < sliceHeight; ++j) {
for (let i = 0; i < sliceWidth; ++i) {
slice[sliceRow + x] = this.data[dataRow + x];
x++;
}
y++;
x -= width;
sliceRow += sliceWidth;
dataRow += dataWidth;
x -= sliceWidth;
}
return slice;
}