cleaned up computation for hex bounding box

This commit is contained in:
Gavin McDonald
2018-07-06 12:00:58 -04:00
parent eb04c3f0eb
commit d3540ed117
3 changed files with 29 additions and 59 deletions

View File

@@ -1,6 +1,6 @@
import Cartographer from './cartographer.js'; import Cartographer from './cartographer.js';
import {sqrt3, extend} from './utils.js'; import {extend, rangeInclusive, sqrt3} from './utils.js';
import Hex from './hex.js'; import Hex from './hex.js';
import Point from './point.js'; import Point from './point.js';
@@ -26,29 +26,6 @@ export default class CartographerXYZ extends Cartographer {
'pixelToTile', 'pixelToTile',
'boundingBox', 'boundingBox',
].map(method => this[method] = this[method].bind(this)); ].map(method => this[method] = this[method].bind(this));
this.pointyTop = settings.pointyTop;
// in pixels
this.originX = 0;
this.originY = 0;
// in pixels
this.scale = 25;
this.scaleMin = 10;
this.scaleMax = 250;
// in cells
this.width = 0;
this.height = 0;
extend(this, settings);
this.originX = parseInt(this.originX);
this.originY = parseInt(this.originY);
this.width = parseInt(this.width);
this.height = parseInt(this.height);
} }
maxWidth() { maxWidth() {
@@ -113,43 +90,36 @@ export default class CartographerXYZ extends Cartographer {
return new Hex(q, r); return new Hex(q, r);
} }
boundingBox(upperLeftPoint, lowerRightPoint) { boundingBox(upperLeftPoint, upperRightPoint, lowerLeftPoint, lowerRightPoint) {
let tiles = []; const upperLeftTile = this.pixelToTile(upperLeftPoint);
let upperRightPoint = new Point(lowerRightPoint.getX(), upperLeftPoint.getY()); const lowerLeftTile = this.pixelToTile(lowerLeftPoint);
const lowerRightTile = this.pixelToTile(lowerRightPoint);
// push out by a 1 axially to account for interlocking hexagons const upperRightTile = this.pixelToTile(upperRightPoint);
// possibly return hexagons not within bounds
let upperLeftTile = this.pixelToTile(upperLeftPoint).moveAxial({q: 0, r: -1});
let lowerRightTile = this.pixelToTile(lowerRightPoint).moveAxial({q: 0, r: 1});
let upperRightTile = this.pixelToTile(upperRightPoint).moveAxial({q: 1, r: -1});
let height = lowerRightTile.getR() - upperRightTile.getR();
let width = upperRightTile.getQ() - upperLeftTile.getQ();
if (this.pointyTop) { if (this.pointyTop) {
for (let row = 0; row <= height; row++) { const rows = rangeInclusive(upperLeftTile.getR() -1 , lowerLeftTile.getR() + 1);
tiles[row] = [];
let r = upperLeftTile.getR() + row;
let qOffset = upperLeftTile.getQ() - Math.floor(row / 2);
for (let q = qOffset; q <= qOffset + width; q++) { const width = upperRightTile.getQ() - upperLeftTile.getQ();
tiles[row].push(new Hex(q, r)); return rows.map((r, index) => {
} const left = upperLeftTile.getQ() - Math.floor(index / 2);
} const right = left + width;
const columns = rangeInclusive(left, right + 1);
return columns.map(q => new Hex(q, r));
});
} }
else { else {
for (let col = 0; col <= width; col++) { const columns = rangeInclusive(upperLeftTile.getQ() - 1, upperRightTile.getQ() + 1);
tiles[col] = [];
let q = upperLeftTile.getQ() + col;
let rOffset = upperLeftTile.getR() - Math.floor(col / 2);
for (let r = rOffset; r <= rOffset + height; r++) { const height = lowerRightTile.getR() - upperRightTile.getR();
tiles[col].push(new Hex(q, r)); return columns.map((q, index) => {
} const top = upperLeftTile.getR() - Math.floor(index / 2);
} const bottom = top + height;
} const rows = rangeInclusive(top, bottom + 1);
return tiles; return rows.map(r => new Hex(q, r));
});
}
} }
} }

View File

@@ -13,11 +13,11 @@ class Demo {
let tessellate = new Tessellate({ let tessellate = new Tessellate({
element: '#container', element: '#container',
board: Tessellate.BOARD_STYLES.SQUARE, board: Tessellate.BOARD_STYLES.HEX,
tile: Tessellate.TILE_STYLES.SQUARE, tile: Tessellate.TILE_STYLES.HEX,
tap: this.onTap, tap: this.onTap,
draw: this.draw, draw: this.draw,
pointyTop: true, //utils.random(1) ? true : false, pointyTop: false, //utils.random(1) ? true : false,
}); });
this.circle = new DrawCircle(); this.circle = new DrawCircle();