Skip to content

Commit

Permalink
fix(draw): use isLineHitRectangle to verify selection is hit geometry…
Browse files Browse the repository at this point in the history
… and image element
  • Loading branch information
pubuzhixing8 committed Dec 27, 2024
1 parent 4036ede commit 8c449f4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
10 changes: 5 additions & 5 deletions packages/core/src/utils/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,36 +179,36 @@ export const isLineHitLine = (a: Point, b: Point, c: Point, d: Point): boolean =
return crossProduct(ab, ac) * crossProduct(ab, ad) <= 0 && crossProduct(cd, ca) * crossProduct(cd, cb) <= 0;
};

export const isPolylineHitRectangle = (points: Point[], rectangle: RectangleClient, isClose: boolean = true) => {
export const isLineHitRectangle = (points: Point[], rectangle: RectangleClient, isClose: boolean = true) => {
const rectanglePoints = RectangleClient.getCornerPoints(rectangle);
const len = points.length;
for (let i = 0; i < len; i++) {
if (i === len - 1 && !isClose) continue;
const p1 = points[i];
const p2 = points[(i + 1) % len];
const isHit = isLineHitRectangleEdge(p1, p2, rectangle);
const isHit = isSingleLineHitRectangleEdge(p1, p2, rectangle);
if (isHit || isPointInPolygon(p1, rectanglePoints) || isPointInPolygon(p2, rectanglePoints)) {
return true;
}
}
return false;
};

export const isPolylineHitRectangleEdge = (points: Point[], rectangle: RectangleClient, isClose: boolean = true) => {
export const isLineHitRectangleEdge = (points: Point[], rectangle: RectangleClient, isClose: boolean = true) => {
const len = points.length;
for (let i = 0; i < len; i++) {
if (i === len - 1 && !isClose) continue;
const p1 = points[i];
const p2 = points[(i + 1) % len];
const isHit = isLineHitRectangleEdge(p1, p2, rectangle);
const isHit = isSingleLineHitRectangleEdge(p1, p2, rectangle);
if (isHit) {
return true;
}
}
return false;
};

export const isLineHitRectangleEdge = (p1: Point, p2: Point, rectangle: RectangleClient) => {
export const isSingleLineHitRectangleEdge = (p1: Point, p2: Point, rectangle: RectangleClient) => {
const rectanglePoints = RectangleClient.getCornerPoints(rectangle);
return (
isLineHitLine(p1, p2, rectanglePoints[0], rectanglePoints[1]) ||
Expand Down
4 changes: 2 additions & 2 deletions packages/draw/src/plugins/with-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
PlaitElement,
RectangleClient,
Selection,
isPolylineHitRectangle,
isLineHitRectangle,
toViewBoxPoint,
toHostPoint,
getHitElementByPoint,
Expand Down Expand Up @@ -59,7 +59,7 @@ export const withTable = (board: PlaitBoard) => {
tableBoard.isRectangleHit = (element: PlaitElement, selection: Selection) => {
if (PlaitDrawElement.isElementByTable(element)) {
const rangeRectangle = RectangleClient.getRectangleByPoints([selection.anchor, selection.focus]);
return isPolylineHitRectangle(element.points, rangeRectangle);
return isLineHitRectangle(element.points, rangeRectangle);
}
return isRectangleHit(element, selection);
};
Expand Down
10 changes: 5 additions & 5 deletions packages/draw/src/utils/hit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
RectangleClient,
Selection,
PlaitBoard,
isPolylineHitRectangle,
isLineHitRectangle,
Point,
distanceBetweenPointAndSegments,
distanceBetweenPointAndPoint,
Expand All @@ -12,7 +12,7 @@ import {
rotateAntiPointsByElement,
isPointInPolygon,
rotatePointsByAngle,
isPolylineHitRectangleEdge
isLineHitRectangleEdge
} from '@plait/core';
import {
PlaitArrowLine,
Expand Down Expand Up @@ -116,12 +116,12 @@ export const isRectangleHitDrawElement = (board: PlaitBoard, element: PlaitEleme

if (PlaitDrawElement.isArrowLine(element)) {
const points = getArrowLinePoints(board, element);
return isPolylineHitRectangle(points, rangeRectangle);
return isLineHitRectangle(points, rangeRectangle);
}

if (PlaitDrawElement.isVectorLine(element)) {
const points = getVectorLinePoints(board, element)!;
return isPolylineHitRectangle(points, rangeRectangle, false);
return isLineHitRectangle(points, rangeRectangle, false);
}

return null;
Expand All @@ -138,7 +138,7 @@ export const isRectangleHitRotatedElement = (

export const isRectangleHitRotatedPoints = (rectangle: RectangleClient, points: Point[], angle: number | undefined) => {
let rotatedPoints = rotatePointsByAngle(points, angle) || points;
return isPolylineHitRectangleEdge(rotatedPoints, rectangle);
return isLineHitRectangle(rotatedPoints, rectangle);
};

export const getHitDrawElement = (board: PlaitBoard, elements: (PlaitDrawElement | PlaitCustomGeometry)[]) => {
Expand Down

0 comments on commit 8c449f4

Please sign in to comment.