is-rect-intersects.ts 607 B

123456789101112131415
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import { Rectangle } from '@flowgram.ai/utils';
  6. export const isRectIntersects = (rectA: Rectangle, rectB: Rectangle): boolean => {
  7. // 检查水平方向是否有重叠
  8. const hasHorizontalOverlap = rectA.right > rectB.left && rectA.left < rectB.right;
  9. // 检查垂直方向是否有重叠
  10. const hasVerticalOverlap = rectA.bottom > rectB.top && rectA.top < rectB.bottom;
  11. // 只有当水平和垂直方向都有重叠时,两个矩形才相交
  12. return hasHorizontalOverlap && hasVerticalOverlap;
  13. };