/
Cybran65
/
Project_0
Обзор
Документация
Войти
/
Cybran65
/
Project_0
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/lib/geometry.ts
78 строк
2 KB
Cybran65
feat: интерактивный прототип интерфейса МИСИ-САПР
08 авг 2026, 22:59
08 авг 2026, 22:59
6df5b61
Код
Авторство
О чём код?
import type { DrawSpec } from '../types'; export type Contour = Array<[number, number]>; /** Округление координат до шага привязки, м. */ export function snap(value: number, step: number): number { if (step <= 0) return value; return Math.round(value / step) * step; } /** * Контур нового элемента по указанным точкам. * * Точечные элементы — квадрат вокруг точки, линейные — прямоугольник * вдоль отрезка заданной толщины, площадные — прямоугольник по двум углам. */ export function buildFootprint(spec: DrawSpec, points: Contour): Contour { if (spec.points === 1 || points.length < 2) { const [x, y] = points[0]; const half = (spec.size ?? 0.4) / 2; return [ [x - half, y - half], [x + half, y - half], [x + half, y + half], [x - half, y + half], ]; } const [[x1, y1], [x2, y2]] = points; if (spec.thickness) { const dx = x2 - x1; const dy = y2 - y1; const length = Math.hypot(dx, dy) || 1; const nx = (-dy / length) * (spec.thickness / 2); const ny = (dx / length) * (spec.thickness / 2); return [ [x1 + nx, y1 + ny], [x2 + nx, y2 + ny], [x2 - nx, y2 - ny], [x1 - nx, y1 - ny], ]; } return [ [x1, y1], [x2, y1], [x2, y2], [x1, y2], ]; } /** Площадь контура в плане, м². */ export function contourArea(contour: Contour): number { let sum = 0; for (let i = 0; i < contour.length; i += 1) { const [x1, y1] = contour[i]; const [x2, y2] = contour[(i + 1) % contour.length]; sum += x1 * y2 - x2 * y1; } return Math.abs(sum) / 2; } /** Длина осевой линии между двумя точками, м. */ export function segmentLength(points: Contour): number { if (points.length < 2) return 0; const [[x1, y1], [x2, y2]] = points; return Math.hypot(x2 - x1, y2 - y1); } /** Центр контура. */ export function contourCenter(contour: Contour): [number, number] { const sum = contour.reduce( (acc, [x, y]) => [acc[0] + x, acc[1] + y] as [number, number], [0, 0] as [number, number], ); return [sum[0] / contour.length, sum[1] / contour.length]; }