Skip to content

Commit

Permalink
fix: optimize vertical/horizontal case
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoluoHe committed Dec 17, 2024
1 parent 87d6b99 commit b43792c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
8 changes: 4 additions & 4 deletions packages/vrender-components/src/axis/overlap/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ export function itemIntersect(item1: IText, item2: IText) {
}

const DELTA_ANGLE = Math.sin(Math.PI / 10);
export function isAngleVertical(angle: number) {
export function isAngleVertical(angle: number, delta = DELTA_ANGLE) {
const hasAngle = !isNil(angle) && angle !== 0;
const cos = hasAngle ? Math.cos(angle) : 1;
return hasAngle && Math.abs(cos) <= DELTA_ANGLE;
return hasAngle && Math.abs(cos) <= delta;
}

export function isAngleHorizontal(angle: number) {
export function isAngleHorizontal(angle: number, delta = DELTA_ANGLE) {
const hasAngle = !isNil(angle) && angle !== 0;
const sin = hasAngle ? Math.sin(angle) : 0;
return !hasAngle || Math.abs(sin) <= DELTA_ANGLE;
return !hasAngle || Math.abs(sin) <= delta;
}
15 changes: 10 additions & 5 deletions packages/vrender-components/src/axis/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,22 @@ export function textIntersect(textA: IText, textB: IText, sep: number) {
}
// 注意:默认旋转角度一样
const angle = (a as IOBBBounds).angle;
const hasAngle = isNil(angle);
if (!hasAngle || isAngleHorizontal(angle) || isAngleVertical(angle)) {
if (isAngleHorizontal(angle, Math.PI / 18)) {
return sep > Math.max(b.x1 - a.x2, a.x1 - b.x2, b.y1 - a.y2, a.y1 - b.y2);
}
// 旋转后的两个中心点未必在一条水平线上
const centerA = { x: (a.x1 + a.x2) / 2, y: (a.y1 + a.y2) / 2 };
const centerB = { x: (b.x1 + b.x2) / 2, y: (b.y1 + b.y2) / 2 };
const centerDistance = Math.sqrt((centerA.x - centerB.x) ** 2 + (centerA.y - centerB.y) ** 2);
const height = a.height();
const centerAngle = Math.PI - Math.atan2(centerB.y - centerA.y, centerB.x - centerA.x);
return sep > Math.abs(Math.sin((angle as number) + centerAngle)) * centerDistance - height;

if (isAngleVertical(angle, Math.PI / 18)) {
return sep > Math.abs(centerB.x - centerA.x) - height;
}

const direction = { x: Math.cos(angle), y: Math.sin(angle) };
const vectorAB = { x: centerB.x - centerA.x, y: centerB.y - centerA.y };
const projectionLength = Math.abs(vectorAB.x * direction.x + vectorAB.y * direction.y);
return sep > projectionLength - height;
}
a = textA.AABBBounds;
b = textB.AABBBounds;
Expand Down

0 comments on commit b43792c

Please sign in to comment.