fixes and boundingBox simplification for flat-topped-square boards
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import Cartographer from './cartographer.js';
|
import Cartographer from './cartographer.js';
|
||||||
|
|
||||||
import {sqrt2} from './utils.js';
|
import {rangeInclusive, sqrt2} from './utils.js';
|
||||||
|
|
||||||
import Square from './square.js';
|
import Square from './square.js';
|
||||||
import Point from './point.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());
|
let pixelY = this.pointyTop ? (this.maxWidth() / 2) * square.getY() : minWidth(square.getY());
|
||||||
|
|
||||||
pixelX += this.originX;
|
pixelX += this.originX;
|
||||||
pixelY += this.originY;
|
pixelY = this.originY - pixelY;;
|
||||||
|
|
||||||
return new Point(pixelX, pixelY);
|
return new Point(pixelX, pixelY);
|
||||||
}
|
}
|
||||||
@@ -73,7 +73,7 @@ export default class CartographerXY extends Cartographer {
|
|||||||
const radiusShort = a => a / this.minWidth();
|
const radiusShort = a => a / this.minWidth();
|
||||||
|
|
||||||
let pixelX = point.getX() - this.originX;
|
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 x = this.pointyTop ? radiusLong(pixelX, pixelY) : radiusShort(pixelX);
|
||||||
let y = this.pointyTop ? radiusLong(pixelY, pixelX) : radiusShort(pixelY);
|
let y = this.pointyTop ? radiusLong(pixelY, pixelX) : radiusShort(pixelY);
|
||||||
@@ -82,39 +82,31 @@ export default class CartographerXY extends Cartographer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
boundingBox(upperLeftPoint, lowerRightPoint) {
|
boundingBox(upperLeftPoint, lowerRightPoint) {
|
||||||
let tiles = [];
|
|
||||||
let upperRightPoint = new Point(lowerRightPoint.getX(), upperLeftPoint.getY());
|
let upperRightPoint = new Point(lowerRightPoint.getX(), upperLeftPoint.getY());
|
||||||
|
|
||||||
let upperLeftTile = this.pixelToTile(upperLeftPoint);
|
let upperLeftTile = this.pixelToTile(upperLeftPoint);
|
||||||
let lowerRightTile = this.pixelToTile(lowerRightPoint);
|
let lowerRightTile = this.pixelToTile(lowerRightPoint);
|
||||||
let upperRightTile = this.pixelToTile(upperRightPoint);
|
let upperRightTile = this.pixelToTile(upperRightPoint);
|
||||||
|
|
||||||
let height = lowerRightTile.getY() - upperRightTile.getY();
|
|
||||||
let width = upperRightTile.getX() - upperLeftTile.getX();
|
|
||||||
|
|
||||||
if (this.pointyTop) {
|
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++) {
|
for (let col = 0; col <= width; col++) {
|
||||||
tiles[col] = [];
|
tiles[col] = [];
|
||||||
let x = upperLeftTile.getX() + 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));
|
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)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
18
src/utils.js
18
src/utils.js
@@ -57,3 +57,21 @@ export function random(min, max) {
|
|||||||
return min + Math.floor(Math.random() * (max - min + 1));
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user