Air-Quality-Web/client_src/js/Overlay/VoronoiOverlay.mjs

70 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-05-29 19:21:03 +00:00
"use strict";
import Voronoi from 'voronoi';
import Vector2 from '../Helpers/Vector2.mjs';
import Rectangle from '../Helpers/Rectangle.mjs';
import SvgWriter from '../Helpers/SVGWriter.mjs';
/**
* Generates and manages a single voronoi SVGOverlay layer.
*/
2019-05-29 19:21:03 +00:00
class VoronoiOverlay {
constructor() {
this.border = new Vector2(0.1, 0.1); // lat / long
this.cells = [];
}
addCells(...cells) {
this.cells.push(...cells);
}
/**
* Computes the bounding box of all the currently registered points.
* Includes a border, which is defined by this.border.
* @return {Rectangle} The bounding box of the currently registered points.
*/
computeBoundingBox() {
let result = new Rectangle(Infinity, Infinity, -Infinity, -Infinity);
for(let cell of this.cells) {
if(cell.point.x < result.x) result.x = cell.point.x;
if(cell.point.y < result.y) result.y = cell.point.y;
if(cell.point.x > result.Right) result.Right = cell.point.x;
if(cell.point.y > result.Bottom) result.Bottom = cell.point.y;
}
2019-05-29 19:21:03 +00:00
result.Left -= this.border.x;
result.Right += this.border.x;
result.Top -= this.border.y;
result.Bottom += this.border.y;
return result;
}
render() {
let bounding_box = this.computeBoundingBox();
// Recycle the diagram object if possible
if(typeof VoronoiOverlay.diagram !== "undefined")
VoronoiOverlay.voronoi.recycle(VoronoiOverlay.diagram);
VoronoiOverlay.diagram = VoronoiOverlay.voronoi.compute(
this.cells.map((cell) => cell.point),
bounding_box
);
console.log(VoronoiOverlay.diagram)
let svg = new SvgWriter();
2019-05-29 19:21:03 +00:00
}
}
VoronoiOverlay.voronoi = new Voronoi();
2019-05-29 19:21:03 +00:00
export default VoronoiOverlay;