refactor: efficiency
This commit is contained in:
parent
742228ec3f
commit
1fff0cdb16
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user