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 CartographerPointyXYZ 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.minWidth();
}
verticalDistance() {
return this.maxWidth() * (3/4);
}
calculateHorizontalScale(pixels, tiles) {
return pixels / tiles / sqrt3;
}
calculateVerticalScale(pixels, tiles) {
return pixels / (tiles * (3/4)) / 2;
}
tileToPixel(hex) {
const pixelX = this.scale * sqrt3 * (hex.getQ() + (hex.getR() / 2));
const pixelY = this.scale * 3/2 * hex.getR();
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 * (sqrt3 / 3)) - (pixelY / 3)) / this.scale;
const r = (pixelY * (2 / 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 rows = rangeInclusive(upperLeftTile.getR() -1 , lowerLeftTile.getR() + 1);
const width = upperRightTile.getQ() - upperLeftTile.getQ();
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));
});
}
}