Finite boards (#1)
This commit is contained in:
79
src/cartographerFlatXY.js
Normal file
79
src/cartographerFlatXY.js
Normal file
@@ -0,0 +1,79 @@
|
||||
import Cartographer from './cartographer.js';
|
||||
|
||||
import {rangeInclusive, sqrt2} from './utils.js';
|
||||
|
||||
import Square from './square.js';
|
||||
import Point from './point.js';
|
||||
|
||||
export default class CartographerFlatXY 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.minWidth() * sqrt2;
|
||||
}
|
||||
|
||||
minWidth() {
|
||||
return this.scale * 2;
|
||||
}
|
||||
|
||||
horizontalDistance() {
|
||||
return this.minWidth();
|
||||
}
|
||||
|
||||
verticalDistance() {
|
||||
return this.minWidth();
|
||||
}
|
||||
|
||||
calculateHorizontalScale(pixels, tiles) {
|
||||
return pixels / tiles / 2;
|
||||
}
|
||||
|
||||
calculateVerticalScale(pixels, tiles) {
|
||||
return pixels / tiles / 2;
|
||||
}
|
||||
|
||||
tileToPixel(square) {
|
||||
const x = square.getX() * this.minWidth();
|
||||
const y = square.getY() * this.minWidth();
|
||||
|
||||
return new Point(x + this.originX, this.originY - y);
|
||||
}
|
||||
|
||||
pixelToTile(point) {
|
||||
const pixelX = point.getX() - this.originX;
|
||||
const pixelY = this.originY - point.getY();
|
||||
|
||||
const x = pixelX / this.minWidth();
|
||||
const y = pixelY / this.minWidth();
|
||||
|
||||
return new Square(x, y);
|
||||
}
|
||||
|
||||
boundingBox(upperLeftPoint, upperRightPoint, lowerLeftPoint, lowerRightPoint) {
|
||||
const upperLeftTile = this.pixelToTile(upperLeftPoint);
|
||||
const lowerRightTile = this.pixelToTile(lowerRightPoint);
|
||||
const upperRightTile = this.pixelToTile(upperRightPoint);
|
||||
|
||||
const columns = rangeInclusive(upperLeftTile.getX(), upperRightTile.getX());
|
||||
const rows = rangeInclusive(lowerRightTile.getY(), upperLeftTile.getY());
|
||||
|
||||
return columns.map(x => rows.map(y => new Square(x, y)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user