summaryrefslogtreecommitdiff
path: root/src/Map/Shapes.js
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-06-05 20:38:18 -0500
committersanine <sanine.not@pm.me>2022-06-05 20:38:18 -0500
commitb3d9ffe8107bd7e0989c1fed2e5bdf31037134bb (patch)
tree6baaa464c8dd1e2b0e7cb4c31e897f6745edeb73 /src/Map/Shapes.js
parentb9d4c658d3561106a452d8014c4d021fe175b759 (diff)
begin adding logical map shapes
Diffstat (limited to 'src/Map/Shapes.js')
-rw-r--r--src/Map/Shapes.js113
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 };