From fbd4805970d9040f86638c8e166baca79013d68a Mon Sep 17 00:00:00 2001 From: Gavin McDonald Date: Sun, 1 Jul 2018 09:57:35 -0400 Subject: [PATCH] fixes and boundingBox simplification for flat-topped-square boards --- src/cartographerXY.js | 34 +++++++++++++--------------------- src/utils.js | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/cartographerXY.js b/src/cartographerXY.js index 7fd4691..5ce18e7 100644 --- a/src/cartographerXY.js +++ b/src/cartographerXY.js @@ -1,6 +1,6 @@ import Cartographer from './cartographer.js'; -import {sqrt2} from './utils.js'; +import {rangeInclusive, sqrt2} from './utils.js'; import Square from './square.js'; import Point from './point.js'; @@ -61,7 +61,7 @@ export default class CartographerXY extends Cartographer { let pixelY = this.pointyTop ? (this.maxWidth() / 2) * square.getY() : minWidth(square.getY()); pixelX += this.originX; - pixelY += this.originY; + pixelY = this.originY - pixelY;; return new Point(pixelX, pixelY); } @@ -73,7 +73,7 @@ export default class CartographerXY extends Cartographer { const radiusShort = a => a / this.minWidth(); let pixelX = point.getX() - this.originX; - let pixelY = point.getY() - this.originY; + let pixelY = this.originY - point.getY(); let x = this.pointyTop ? radiusLong(pixelX, pixelY) : radiusShort(pixelX); let y = this.pointyTop ? radiusLong(pixelY, pixelX) : radiusShort(pixelY); @@ -82,39 +82,31 @@ export default class CartographerXY extends Cartographer { } boundingBox(upperLeftPoint, lowerRightPoint) { - let tiles = []; let upperRightPoint = new Point(lowerRightPoint.getX(), upperLeftPoint.getY()); let upperLeftTile = this.pixelToTile(upperLeftPoint); let lowerRightTile = this.pixelToTile(lowerRightPoint); let upperRightTile = this.pixelToTile(upperRightPoint); - let height = lowerRightTile.getY() - upperRightTile.getY(); - let width = upperRightTile.getX() - upperLeftTile.getX(); - if (this.pointyTop) { - for (let row = 0; row <= height; row++) { - tiles[row] = []; - let y = upperLeftTile.getY() + row; - let xOffset = upperLeftTile.getX() - (row % 2); - - for (let x = xOffset; x <= xOffset + width; x++) { - tiles[row].push(new Square(x, y)); - } - } - } - else { for (let col = 0; col <= width; col++) { tiles[col] = []; let x = upperLeftTile.getX() + col; - let yOffset = upperLeftTile.getY(); + const bottomRow = upperLeftTile.getY() - height; - for (let y = yOffset; y <= yOffset + height; y++) { + 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)); } } } + else { + const columns = rangeInclusive(upperLeftTile.getX(), upperRightTile.getX()); + const rows = rangeInclusive(lowerRightTile.getY(), upperLeftTile.getY()); - return tiles; + return columns.map(x => rows.map(y => new Square(x, y))); + } } } diff --git a/src/utils.js b/src/utils.js index 219919a..84dafd8 100644 --- a/src/utils.js +++ b/src/utils.js @@ -57,3 +57,21 @@ export function random(min, max) { return min + Math.floor(Math.random() * (max - min + 1)); } +export function range (start, end) { + if (end == null) { + end = start; + start = 0; + } + + return Array.from(Array(end - start), (_value, index) => index + start); +} + +export function rangeInclusive(start, end) { + if (end == null) { + end = start; + start = 0; + } + + return range(start, end+1); +} +