fixes for pointy-top square boards

This commit is contained in:
Gavin McDonald
2018-07-05 21:37:56 -04:00
parent fbd4805970
commit eb04c3f0eb
4 changed files with 120 additions and 40 deletions

View File

@@ -51,14 +51,19 @@ export default class CartographerXY extends Cartographer {
getVertDistance() {return this.pointyTop ? this.maxDistance() : this.minDistance();}
tileToPixel(square) {
let scale = this.scale;
const scale = this.scale;
const minWidth = (a) => this.minWidth() * a;
// (above/below axis) * (distance from axis) / (size)
const pointyDistanceX = (x, y) => (x < y ? -1 : 1) * (Math.abs(y - x) / sqrt2) * this.minWidth();
const pointyDistanceY = (x, y) => (-x < y ? 1 : -1) * (Math.abs(x + y) / sqrt2) * this.minWidth();
const maxWidth = (a, b) => (this.maxWidth() * a) + ((b % 2) * (this.maxWidth() / 2));
const flatDistance = a => this.minWidth() * a;
let pixelX = this.pointyTop ? maxWidth(square.getX(), square.getY()) : minWidth(square.getX());
let pixelY = this.pointyTop ? (this.maxWidth() / 2) * square.getY() : minWidth(square.getY());
const x = square.getX();
const y = square.getY();
let pixelX = this.pointyTop ? pointyDistanceX(x, y) : flatDistance(x);
let pixelY = this.pointyTop ? pointyDistanceY(x, y) : flatDistance(y);
pixelX += this.originX;
pixelY = this.originY - pixelY;;
@@ -69,38 +74,53 @@ export default class CartographerXY extends Cartographer {
pixelToTile(point) {
let scale = this.scale;
const radiusLong = a => (a / (this.maxWidth() / 2));
const radiusShort = a => a / this.minWidth();
// (above/below axis) * (distance from axis) / (size)
const pointyDistanceX = (x, y) => (-x < y ? 1 : -1) * (Math.abs(x + y) / sqrt2) / this.minWidth();
const pointyDistanceY = (x, y) => (x < y ? 1 : -1) * (Math.abs(y - x) / sqrt2) / this.minWidth();
const flatDistance = a => a / this.minWidth();
let pixelX = point.getX() - this.originX;
let pixelY = this.originY - point.getY();
let x = this.pointyTop ? radiusLong(pixelX, pixelY) : radiusShort(pixelX);
let y = this.pointyTop ? radiusLong(pixelY, pixelX) : radiusShort(pixelY);
let x = this.pointyTop ? pointyDistanceX(pixelX, pixelY) : flatDistance(pixelX);
let y = this.pointyTop ? pointyDistanceY(pixelX, pixelY) : flatDistance(pixelY);
return new Square(x, y);
}
boundingBox(upperLeftPoint, lowerRightPoint) {
let upperRightPoint = new Point(lowerRightPoint.getX(), upperLeftPoint.getY());
let upperLeftTile = this.pixelToTile(upperLeftPoint);
let lowerRightTile = this.pixelToTile(lowerRightPoint);
let upperRightTile = this.pixelToTile(upperRightPoint);
boundingBox(upperLeftPoint, upperRightPoint, lowerLeftPoint, lowerRightPoint) {
const upperLeftTile = this.pixelToTile(upperLeftPoint);
const lowerRightTile = this.pixelToTile(lowerRightPoint);
const upperRightTile = this.pixelToTile(upperRightPoint);
if (this.pointyTop) {
for (let col = 0; col <= width; col++) {
tiles[col] = [];
let x = upperLeftTile.getX() + col;
const bottomRow = upperLeftTile.getY() - height;
const lowerLeftTile = this.pixelToTile(lowerLeftPoint);
for (let y = upperLeftTile.getY(); y >= bottomRow; y--) {
// TODO: this results in missing tiles (e.g. (1,0))
// fix by tilting coordinates 45 degrees?
x = this.pointyTop ? x - (y % 2) : x;
tiles[col].push(new Square(x, y));
}
}
const columns = rangeInclusive(lowerLeftTile.getX(), upperRightTile.getX());
const upperLeftIntercept = upperLeftTile.getY() - upperLeftTile.getX();
const upperRightIntercept = upperLeftTile.getY() + upperLeftTile.getX();
const lowerLeftIntercept = lowerRightTile.getY() - lowerRightTile.getX();
const lowerRightIntercept = lowerRightTile.getY() + lowerRightTile.getX();
const aboutHalf = Math.floor(columns.length / 2);
const midway = columns.length % 2 ? columns[aboutHalf] :
(columns[aboutHalf - 1] + columns[aboutHalf]) / 2;
return columns.map(x => {
let top = x < midway ? upperLeftIntercept + x : upperRightIntercept - x;
let bottom = x < midway ? lowerRightIntercept - x : lowerLeftIntercept + x;
bottom = Math.min(bottom, top);
top = Math.max(bottom, top);
// push out by 1 on either end to account for interlocking tiles
const rows = rangeInclusive(bottom - 1, top + 1);
return rows.map(y => new Square(x, y));
});
}
else {
const columns = rangeInclusive(upperLeftTile.getX(), upperRightTile.getX());