多边形线段细分备用--分割多段

This commit is contained in:
joylink_zhaoerwei 2023-06-02 14:16:02 +08:00
parent 9be2d014bc
commit f4b9ba2b37
2 changed files with 44 additions and 0 deletions

View File

@ -19,6 +19,7 @@ import {
distance2, distance2,
linePoint, linePoint,
pointPolygon, pointPolygon,
calculateLineSegmentingPoint,
} from '../utils'; } from '../utils';
import { GraphicTransformEvent, ShiftData } from './GraphicTransformPlugin'; import { GraphicTransformEvent, ShiftData } from './GraphicTransformPlugin';
@ -57,6 +58,9 @@ export abstract class GraphicEditPlugin<
export const addWaypointConfig: MenuItemOptions = { export const addWaypointConfig: MenuItemOptions = {
name: '添加路径点', name: '添加路径点',
}; };
export const addWaySegmentingConfig: MenuItemOptions = {
name: '细分',
};
export const removeWaypointConfig: MenuItemOptions = { export const removeWaypointConfig: MenuItemOptions = {
name: '移除路径点', name: '移除路径点',
}; };
@ -251,6 +255,21 @@ export function addLineWayPoint(
graphic.linePoints = points; graphic.linePoints = points;
} }
export function addPolygonSegmentingPoint(
graphic: ILineGraphic,
start: number,
end: number,
knife = 2
) {
const linePoints = graphic.linePoints;
const points = linePoints.slice(0, start + 1);
points.push(
...calculateLineSegmentingPoint(linePoints[start], linePoints[end], knife)
);
points.push(...linePoints.slice(end));
graphic.linePoints = points;
}
function assertBezierWayPoint(i: number) { function assertBezierWayPoint(i: number) {
const c = i % 3; const c = i % 3;
if (c !== 0) { if (c !== 0) {

View File

@ -287,6 +287,31 @@ export function calculateLineMidpoint(p1: IPointData, p2: IPointData): Point {
return new Point(x, y); return new Point(x, y);
} }
/**
* 线--线
* @param p1
* @param p2
* @param knife
* @returns
*/
export function calculateLineSegmentingPoint(
p1: IPointData,
p2: IPointData,
knife: number
): IPointData[] {
const segmentingPoints: IPointData[] = [];
const x = p1.x < p2.x ? p1.x : p2.x;
const y = p1.y < p2.y ? p1.y : p2.y;
const w = Math.abs(p1.x - p2.x);
const h = Math.abs(p1.y - p2.y);
for (let i = 0; i < knife - 1; i++) {
const pointX = x + (w * (i + 1)) / knife;
const pointy = y + (h * (i + 1)) / knife;
segmentingPoints.push(new Point(pointX, pointy));
}
return segmentingPoints;
}
/** /**
* 线 * 线
* @param p1 * @param p1