Skip to content

Commit 1274d3e

Browse files
committed
refactor(ElementGroup): 优化平行四边形顶点计算逻辑
将原有的多次 `find` 调用合并为一次 `forEach` 遍历,减少重复计算,提升代码可读性和性能。
1 parent 1d2f1a0 commit 1274d3e

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

src/modules/elements/ElementGroup.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -312,22 +312,26 @@ export default class ElementGroup extends Element implements IElementGroup {
312312
const yParrelSlopeLine = MathUtils.calcParrelLine(ySlopLine, subsCenterCoord);
313313
// 计算过subsCenterCoord与xSlopeLine平行的直线
314314
const xParrelSlopeLine = MathUtils.calcParrelLine(xSlopeLine, subsCenterCoord);
315-
// 计算最小平行四边形的左上角
316-
const topLeft = minParallelogramPoints.find(
317-
point => !MathUtils.isPointClockwiseOfLine(point, yParrelSlopeLine.start, yParrelSlopeLine.end) && !MathUtils.isPointClockwiseOfLine(point, xParrelSlopeLine.start, xParrelSlopeLine.end),
318-
);
319-
// 计算最小平行四边形的右上角
320-
const topRight = minParallelogramPoints.find(
321-
point => MathUtils.isPointClockwiseOfLine(point, yParrelSlopeLine.start, yParrelSlopeLine.end) && !MathUtils.isPointClockwiseOfLine(point, xParrelSlopeLine.start, xParrelSlopeLine.end),
322-
);
323-
// 计算最小平行四边形的右下角
324-
const bottomRight = minParallelogramPoints.find(
325-
point => MathUtils.isPointClockwiseOfLine(point, yParrelSlopeLine.start, yParrelSlopeLine.end) && MathUtils.isPointClockwiseOfLine(point, xParrelSlopeLine.start, xParrelSlopeLine.end),
326-
);
327-
// 计算最小平行四边形的左下角
328-
const bottomLeft = minParallelogramPoints.find(
329-
point => !MathUtils.isPointClockwiseOfLine(point, yParrelSlopeLine.start, yParrelSlopeLine.end) && MathUtils.isPointClockwiseOfLine(point, xParrelSlopeLine.start, xParrelSlopeLine.end),
330-
);
315+
let topLeft: IPoint;
316+
let topRight: IPoint;
317+
let bottomRight: IPoint;
318+
let bottomLeft: IPoint;
319+
minParallelogramPoints.forEach(point => {
320+
const isPointClockwiseOfYSlopLine = MathUtils.isPointClockwiseOfLine(point, yParrelSlopeLine.start, yParrelSlopeLine.end);
321+
const isPointClockwiseOfXSlopLine = MathUtils.isPointClockwiseOfLine(point, xParrelSlopeLine.start, xParrelSlopeLine.end);
322+
if (!isPointClockwiseOfYSlopLine && !isPointClockwiseOfXSlopLine) {
323+
topLeft = point;
324+
}
325+
if (isPointClockwiseOfYSlopLine &&!isPointClockwiseOfXSlopLine) {
326+
topRight = point;
327+
}
328+
if (isPointClockwiseOfYSlopLine && isPointClockwiseOfXSlopLine) {
329+
bottomRight = point;
330+
}
331+
if (!isPointClockwiseOfYSlopLine && isPointClockwiseOfXSlopLine) {
332+
bottomLeft = point;
333+
}
334+
})
331335
// 旋转倾斜后的盒模型坐标
332336
const rotateBoxCoords = [topLeft, topRight, bottomRight, bottomLeft];
333337
// 计算不旋转的盒模型坐标

0 commit comments

Comments
 (0)