import Point from "../Geometry/Point.js"; import Line from "../Geometry/Line.js"; import { dataService } from "./DataService.js"; import VectorCategory from "../enum/VectorCategory.js"; import { mathUtil } from "../Util/MathUtil.js"; import { uiService } from "./UIService.js"; import { addLine } from "../Controls/AddLine.js"; export default class LineService { constructor() {} create(startPoint, endPoint, category, vectorId) { if (!startPoint || !endPoint || mathUtil.equalPoint(startPoint, endPoint)) { return null; } let start = new Point(startPoint); let end = new Point(endPoint); let line = new Line(start.vectorId, end.vectorId, vectorId); if (category) { line.setCategory(category); } start.setPointParent(line.vectorId, "start"); end.setPointParent(line.vectorId, "end"); dataService.addPoint(start); dataService.addPoint(end); dataService.addLine(line); return line; } createByPointId(startId, endId, category, vectorId) { let line = new Line(startId, endId, vectorId); if (category) { line.setCategory(category); } let start = dataService.getPoint(startId); let end = dataService.getPoint(endId); start.setPointParent(line.vectorId, "start"); end.setPointParent(line.vectorId, "end"); dataService.addLine(line); return line; } deleteLine(lineId) { let line = dataService.getLine(lineId); //如果是基准线 if (line.getCategory() == VectorCategory.Line.BaseLine) { let points = dataService.getPoints(); for (let key in points) { let point = dataService.getPoint(key); let category = point.getCategory(); if ( category == VectorCategory.Point.BasePoint || category == VectorCategory.Point.TestBasePoint || category == VectorCategory.Point.TestPoint ) { dataService.deletePoint(key); } } Settings.baseLineId = null; } dataService.deleteLine(); } copy(vectorId) { let line = dataService.getLine(vectorId); let startPoint = dataService.getPoint(line.startId); let endPoint = dataService.getPoint(line.endId); startPoint = uiService.getNewPositionForPop(startPoint); endPoint = uiService.getNewPositionForPop(endPoint); let newLine = this.create(startPoint, endPoint, line.category); newLine.setArrowColor(line.arrowColor); return newLine; } //将pointId1合并到pointId2 mergePoint(pointId1, pointId2) { if (pointId1 == pointId2) { return; } else if (pointId1 != null && pointId2 != null) { let point1 = dataService.getPoint(pointId1); let parent1 = point1.getParent(); let point2 = dataService.getPoint(pointId2); for (let key in parent1) { let line = dataService.getLine(key); let dir = line.getDir(pointId1); if (dir == "start") { line.startId = pointId2; } else if (dir == "end") { line.endId = pointId2; } point2.setPointParent(key, dir); } dataService.deletePoint(pointId1); } } } const lineService = new LineService(); export { lineService };