import * as Utils from '../utils'; // @ts-ignore import { Cartesian3 } from 'cesium'; import { CesiumTypes, LngLat, PolygonStyle, Viewer } from '../type'; import Basic from '../bastic'; export class Circle extends Basic { points: Cartesian3[] = []; freehand: boolean; constructor(cesium: CesiumTypes, viewer: Viewer, style?: PolygonStyle) { super(cesium, viewer, style); this.cesium = cesium; this.freehand = true; this.setState('drawing'); } addPoint(cartesian: Cartesian3) { this.points.push(cartesian); if (this.points.length === 1) { this.onMouseMove(); } else if (this.points.length > 1) { this.finishDrawing(); } } updateMovingPoint(cartesian: Cartesian3) { const tempPoints = [...this.points, cartesian]; const geometryPoints = this.createGraphic(tempPoints); this.setGeometryPoints(geometryPoints); 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 lnglatPoints = positions.map((pnt) => { return this.cartesianToLnglat(pnt); }); const center = lnglatPoints[0]; const pnt2 = lnglatPoints[1]; const radius = Utils.MathDistance(center, pnt2); const res: any = this.generatePoints(center, radius); const temp = [].concat(...res); const cartesianPoints = this.cesium.Cartesian3.fromDegreesArray(temp); return cartesianPoints; } generatePoints(center: LngLat, radius: number) { let x, y, angle; const points: LngLat[] = []; for (let i = 0; i <= 100; i++) { angle = (Math.PI * 2 * i) / 100; x = center[0] + radius * Math.cos(angle); y = center[1] + radius * Math.sin(angle); points.push([x, y]); } return points; } getPoints() { return this.points; } }