// @ts-ignore import { Cartesian3, Viewer } from 'cesium'; import { CesiumTypes, PolygonStyle } from '../type'; import Basic from '../bastic'; import *as Utils from '../utils'; export class Rectangle extends Basic { points: Cartesian3[] = []; constructor(cesium: CesiumTypes, viewer: Viewer, style?: PolygonStyle) { super(cesium, viewer, style); this.cesium = cesium; this.setState('drawing'); } addPoint(cartesian: Cartesian3) { this.points.push(cartesian); if (this.points.length <= 2) { this.onMouseMove(); } else if (this.points.length > 2) { this.finishDrawing(); } } updateMovingPoint(cartesian: Cartesian3) { const tempPoints = [...this.points, cartesian]; // const geometryPoints = this.createGraphic(tempPoints); this.setGeometryPoints(tempPoints); if (tempPoints.length === 2) { this.addTempLine(); } else { const geometryPoints = this.createGraphic(tempPoints); this.setGeometryPoints(geometryPoints) this.removeTempLine(); this.drawPolygon(); } } updateDraggingPoint(cartesian: Cartesian3, index: number) { this.points[index] = cartesian; const geometryPoints = this.createGraphic(this.points); this.setGeometryPoints(geometryPoints); this.drawPolygon(); } createGraphic(positions: Cartesian3[]) { const [p1, p2, p3] = positions.map((cartesian) => { return this.cartesianToLnglat(cartesian); }); const clockWise = Utils.isClockWise(p1, p2, p3) const angle1 = clockWise ? Utils.getAngleOfThreePoints(p3, p2, p1) : Utils.getAngleOfThreePoints(p1, p2, p3); const d23 = Math.abs(Utils.MathDistance(p2, p3)); const d24 = Math.abs(Math.cos(Math.PI / 2 - angle1)) * d23; const angle = clockWise ? Math.PI / 2 : Math.PI * 3 / 2 const p4 = Utils.getThirdPoint(p1, p2, angle, d24, angle == Math.PI / 2 ? !clockWise : clockWise) const p5 = Utils.getThirdPoint(p2, p1, angle, d24, angle == Math.PI / 2 ? clockWise : !clockWise) const cartesianPoints = this.cesium.Cartesian3.fromDegreesArray([...p1, ...p2, ...p4, ...p5]); return cartesianPoints; } getPoints() { return this.points; } }