UNPKG

@maxgraph/core

Version:

maxGraph is a fully client side JavaScript diagramming library that uses SVG and HTML for rendering.

123 lines (119 loc) 4.7 kB
"use strict"; /* Copyright 2021-present The maxGraph project Contributors Copyright (c) 2006-2015, JGraph Ltd Copyright (c) 2006-2015, Gaudenz Alder Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TrianglePerimeter = void 0; const Point_js_1 = __importDefault(require("../../geometry/Point.js")); const mathUtils_js_1 = require("../../../util/mathUtils.js"); /** * This perimeter is registered under `trianglePerimeter` in {@link PerimeterRegistry} when using {@link Graph} or calling {@link registerDefaultPerimeters}. * * @category Perimeter */ const TrianglePerimeter = (bounds, vertex, next, orthogonal = false) => { const direction = vertex != null ? vertex.style.direction : null; const vertical = direction === 'north' || direction === 'south'; const { x } = bounds; const { y } = bounds; const w = bounds.width; const h = bounds.height; let cx = x + w / 2; let cy = y + h / 2; let start = new Point_js_1.default(x, y); let corner = new Point_js_1.default(x + w, cy); let end = new Point_js_1.default(x, y + h); if (direction === 'north') { start = end; corner = new Point_js_1.default(cx, y); end = new Point_js_1.default(x + w, y + h); } else if (direction === 'south') { corner = new Point_js_1.default(cx, y + h); end = new Point_js_1.default(x + w, y); } else if (direction === 'west') { start = new Point_js_1.default(x + w, y); corner = new Point_js_1.default(x, cy); end = new Point_js_1.default(x + w, y + h); } let dx = next.x - cx; let dy = next.y - cy; const alpha = vertical ? Math.atan2(dx, dy) : Math.atan2(dy, dx); const t = vertical ? Math.atan2(w, h) : Math.atan2(h, w); let base = false; if (direction === 'north' || direction === 'west') { base = alpha > -t && alpha < t; } else { base = alpha < -Math.PI + t || alpha > Math.PI - t; } let result = null; if (base) { if (orthogonal && ((vertical && next.x >= start.x && next.x <= end.x) || (!vertical && next.y >= start.y && next.y <= end.y))) { if (vertical) { result = new Point_js_1.default(next.x, start.y); } else { result = new Point_js_1.default(start.x, next.y); } } else if (direction === 'north') { result = new Point_js_1.default(x + w / 2 + (h * Math.tan(alpha)) / 2, y + h); } else if (direction === 'south') { result = new Point_js_1.default(x + w / 2 - (h * Math.tan(alpha)) / 2, y); } else if (direction === 'west') { result = new Point_js_1.default(x + w, y + h / 2 + (w * Math.tan(alpha)) / 2); } else { result = new Point_js_1.default(x, y + h / 2 - (w * Math.tan(alpha)) / 2); } } else { if (orthogonal) { const pt = new Point_js_1.default(cx, cy); if (next.y >= y && next.y <= y + h) { pt.x = vertical ? cx : direction === 'west' ? x + w : x; pt.y = next.y; } else if (next.x >= x && next.x <= x + w) { pt.x = next.x; pt.y = !vertical ? cy : direction === 'north' ? y + h : y; } // Compute angle dx = next.x - pt.x; dy = next.y - pt.y; cx = pt.x; cy = pt.y; } if ((vertical && next.x <= x + w / 2) || (!vertical && next.y <= y + h / 2)) { result = (0, mathUtils_js_1.intersection)(next.x, next.y, cx, cy, start.x, start.y, corner.x, corner.y); } else { result = (0, mathUtils_js_1.intersection)(next.x, next.y, cx, cy, corner.x, corner.y, end.x, end.y); } } if (result == null) { result = new Point_js_1.default(cx, cy); } return result; }; exports.TrianglePerimeter = TrianglePerimeter;