rendering a board of squares

This commit is contained in:
Gavin McDonald
2018-06-24 13:59:12 -04:00
parent 26c6813d95
commit bf37910877
9 changed files with 374 additions and 47 deletions

120
src/cartographerXY.js Normal file
View File

@@ -0,0 +1,120 @@
import Cartographer from './cartographer.js';
import {sqrt2} from './utils.js';
import Square from './square.js';
import Point from './point.js';
export default class CartographerXY extends Cartographer {
constructor(settings) {
super(settings);
[
'maxWidth',
'minWidth',
'maxDistance',
'minDistance',
'getCellWidth',
'getCellHeight',
'getHorzDistance',
'getVertDistance',
'tileToPixel',
'pixelToTile',
'boundingBox',
].map(method => this[method] = this[method].bind(this));
}
maxWidth() {
return this.minWidth() * sqrt2;
}
minWidth() {
return this.scale * 2;
}
maxDistance() {
return this.maxWidth();
}
minDistance() {
return this.minWidth();
}
getCellWidth() {return this.pointyTop ? this.maxWidth() : this.minWidth();}
getCellHeight() {return this.pointyTop ? this.maxWidth() : this.minWidth();}
getHorzDistance() {return this.pointyTop ? this.maxDistance() : this.minDistance();}
getVertDistance() {return this.pointyTop ? this.maxDistance() : this.minDistance();}
tileToPixel(square) {
let scale = this.scale;
const minWidth = (a) => this.minWidth() * a;
const maxWidth = (a) => this.maxWidth() * a;
let pixelX = this.pointyTop ? maxWidth(square.getX()) : minWidth(square.getX());
let pixelY = this.pointyTop ? maxWidth(square.getY()) : minWidth(square.getY());
pixelX += this.originX;
pixelY += this.originY;
return new Point(pixelX, pixelY);
}
pixelToTile(point) {
let scale = this.scale;
const radiusLong = a => a / this.maxWidth();
const radiusShort = a => a / this.minWidth();
let pixelX = point.getX() - this.originX;
let pixelY = point.getY() - this.originY;
let x = this.pointyTop ? radiusLong(pixelX, pixelY) : radiusShort(pixelX);
let y = this.pointyTop ? radiusLong(pixelY, pixelX) : radiusShort(pixelY);
return new Square(x, y);
}
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();
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();
for (let y = yOffset; y <= yOffset + height; y++) {
tiles[col].push(new Square(x, y));
}
}
}
return tiles;
}
}