首页/ 接口索引/ 单位与换算

RectIntersect 面积

两矩形重叠面积 = max(0, min(x1+w1, x2+w2) - max(x1, x2)) * max(0, min(y1+h1, y2+h2) - max(y1, y2))。这是判断重叠的标准写法。

名称

RectIntersect 面积

说明

两矩形重叠面积 = max(0, min(x1+w1, x2+w2) - max(x1, x2)) * max(0, min(y1+h1, y2+h2) - max(y1, y2))。这是判断重叠的标准写法。

Python 示例(可直接运行)

依赖 pywin32。示例假定 app / doc / page / layer 已就绪。 见「连接与初始化」
Python
def overlap_ratio(a, b):
    """
    计算两个形状包围盒的重叠比例
    a, b : 任意形状对象
    返回 : 重叠面积 / 两者中较小的面积(1.0 表示完全重合)
    """
    ax, ay, aw, ah = a.GetBoundingBox()
    bx, by, bw, bh = b.GetBoundingBox()

    # 交集的两个角
    ix = min(ax + aw, bx + bw) - max(ax, bx)
    iy = min(ay + ah, by + bh) - max(ay, by)

    if ix <= 0 or iy <= 0:              # 完全不相交
        return 0.0

    inter = ix * iy
    smaller = min(aw * ah, bw * bh)
    return inter / smaller if smaller > 0 else 0.0

s1 = app.ActiveSelectionRange(1)
s2 = doc.ActivePage.Shapes.Item(2)
print("重叠比例:", round(overlap_ratio(s1, s2), 3))

关于本类:单位与换算

最容易出 25.4 倍错误的地方。先搞清楚 doc.Unit 是什么,再写数字。

同类其他接口