diff options
Diffstat (limited to 'src/Map/Shapes.js')
-rw-r--r-- | src/Map/Shapes.js | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/src/Map/Shapes.js b/src/Map/Shapes.js new file mode 100644 index 0000000..3443f73 --- /dev/null +++ b/src/Map/Shapes.js @@ -0,0 +1,113 @@ +import { Enum } from '../Util/Util.js'; +import { dist, AABB } from '../Geometry/Geometry.js'; + + +class Node { + constructor(x, y) { + this.moveTo(x, y); + } + + moveTo(x, y) { + this.x = x; + this.y = y; + } +} + + +class ShapeType extends Enum { + constructor(name) { super('Shape', name); } +} + + +class Shape { + static Point = new ShapeType('Point'); + static Path = new ShapeType('Path'); + static Polygon = new ShapeType('Polygon'); + + constructor(type, nodes) { + this.type = type; + this.nodes = nodes; + + this.boundingBox = this.computeBoundingBox(); + } + + computeBoundingBox() { + let xMin = Infinity; + let xMax = -Infinity; + let yMin = Infinity; + let yMax = -Infinity; + + for (let node of this.nodes) { + if (node.x < xMin) xMin = node.x; + if (node.x > xMax) xMax = node.x; + + if (node.y < yMin) yMin = node.y; + if (node.y > yMax) yMax = node.y; + } + + return new AABB(xMin, yMin, xMax-xMin, yMax-yMin); + } +} + +class Point extends Shape { + constructor(node) { + super(Shape.Point, [node]); + } +} + +class Path extends Shape { + constructor(nodes) { + super(Shape.Path, nodes); + } +} + +class Polygon extends Shape { + constructor(nodes) { + super(Shape.Polygon, nodes); + } +} + + +class ZoomLevel extends Enum { + static Globe = new ZoomLevel('Globe'); + static Nation = new ZoomLevel('Nation'); + static Province = new ZoomLevel('Province'); + static City = new ZoomLevel('City'); + static Neighborhoot = new ZoomLevel('Neighborhood'); + static Building = new ZoomLevel('Building'); + + constructor(name) { super('ZoomLevel', name); } +} + +class MapObject { + constructor(core, layerId, minZoom, maxZoom=minZoom) { + this.core = core; + this.layerId = layerId; + this.minZoom = minZoom; + this.maxZoom = maxZoom; + + this.shapes = []; + this.boundingBox = this.computeBoundingBox(); + } + + computeBoundingBox() { + let xMin = Infinity; + let xMax = -Infinity; + let yMin = Infinity; + let yMax = -Infinity; + + for (let shape of this.shapes) { + const box = shape.boundingBox; + if (box.x < xMin) xMin = box.x; + if (box.x + box.width > xMax) xMax = box.x + box.width; + + if (box.y < yMin) yMin = box.y; + if (box.y + box.height > yMax) yMax = box.y + box.height; + } + + return new AABB(xMin, yMin, xMax-xMin, yMax-yMin); + } +} + + +export { Node, Shape, Point, Path, Polygon, ZoomLevel, MapObject }; |