Finite boards (#1)

This commit is contained in:
gavin
2018-07-15 02:54:26 +00:00
committed by Gitea
parent 68f83bfc4b
commit 0545c0a5d9
14 changed files with 497 additions and 356 deletions

View File

@@ -0,0 +1,86 @@
import Cartographer from './cartographer.js';
import {rangeInclusive, sqrt3} from './utils.js';
import Hex from './hex.js';
import Point from './point.js';
export default class CartographerFlatXYZ extends Cartographer {
constructor(settings) {
super(settings);
[
'maxWidth',
'minWidth',
'horizontalDistance',
'verticalDistance',
'calculateHorizontalScale',
'calculateVerticalScale',
'tileToPixel',
'pixelToTile',
'boundingBox',
].map(method => this[method] = this[method].bind(this));
}
maxWidth() {
return this.scale * 2;
}
minWidth() {
return this.scale * sqrt3;
}
horizontalDistance() {
return this.maxWidth() * (3/4);
}
verticalDistance() {
return this.minWidth();
}
calculateHorizontalScale(pixels, tiles) {
return pixels / (tiles * (3/4)) / 2;
}
calculateVerticalScale(pixels, tiles) {
return pixels / tiles / sqrt3;
}
tileToPixel(hex) {
const pixelX = this.scale * 3/2 * hex.getQ();
const pixelY = this.scale * sqrt3 * (hex.getR() + (hex.getQ() / 2));
return new Point(pixelX + this.originX, pixelY + this.originY);
}
pixelToTile(point) {
const pixelX = point.getX() - this.originX;
const pixelY = point.getY() - this.originY;
const q = (pixelX * (2 / 3)) / this.scale;
const r = ((pixelY * (sqrt3 / 3)) - (pixelX / 3)) / this.scale;
return new Hex(q, r);
}
boundingBox(upperLeftPoint, upperRightPoint, lowerLeftPoint, lowerRightPoint) {
const upperLeftTile = this.pixelToTile(upperLeftPoint);
const lowerLeftTile = this.pixelToTile(lowerLeftPoint);
const lowerRightTile = this.pixelToTile(lowerRightPoint);
const upperRightTile = this.pixelToTile(upperRightPoint);
const columns = rangeInclusive(upperLeftTile.getQ() - 1, upperRightTile.getQ() + 1);
const height = lowerRightTile.getR() - upperRightTile.getR();
return columns.map((q, index) => {
const top = upperLeftTile.getR() - Math.floor(index / 2);
const bottom = top + height;
const rows = rangeInclusive(top, bottom + 1);
return rows.map(r => new Hex(q, r));
});
}
}