矩形边界Rect64、RectD是什么?

摘要:layout: default title: 第4章:矩形边界 - Rect64、RectD 第4章:矩形边界 - Rect64、RectD 4.1 概述 在多边形裁剪运算中,矩形边界框(Bounding Box)是一个关键的优化结构。通过
第4章:矩形边界 - Rect64、RectD 4.1 概述 在多边形裁剪运算中,矩形边界框(Bounding Box)是一个关键的优化结构。通过快速判断两个多边形的边界框是否相交,可以在早期排除不可能相交的情况,大大提高算法效率。Clipper2 提供了 Rect64 和 RectD 两种矩形结构。 4.2 Rect64 结构 4.2.1 结构定义 public struct Rect64 { public long left; public long top; public long right; public long bottom; } 坐标系统: left:左边界(最小 X) top:上边界(最小 Y) right:右边界(最大 X) bottom:下边界(最大 Y) 注意:在 Clipper2 中,Y 轴默认向下,因此 top < bottom。 4.2.2 构造函数 // 从四个边界值创建 public Rect64(long l, long t, long r, long b) { left = l; top = t; right = r; bottom = b; } // 创建有效或无效矩形 public Rect64(bool isValid) { if (isValid) { left = 0; top = 0; right = 0; bottom = 0; } else { // 无效矩形:用于初始化,等待后续更新 left = long.MaxValue; top = long.MaxValue; right = long.MinValue; bottom = long.MinValue; } } // 复制构造函数 public Rect64(Rect64 rec) { left = rec.left; top = rec.top; right = rec.right; bottom = rec.bottom; } 4.2.3 无效矩形的设计 无效矩形是一个巧妙的设计,用于边界框的累积计算: // 创建无效矩形 Rect64 bounds = new Rect64(false); // 此时: left = MaxValue, right = MinValue // 任何点都会扩展这个边界 // 累积边界 foreach (Point64 pt in path) { if (pt.X < bounds.left) bounds.left = pt.X; if (pt.X > bounds.right) bounds.right = pt.X; if (pt.Y < bounds.top) bounds.top = pt.Y; if (pt.Y > bounds.bottom) bounds.bottom = pt.Y; } 4.2.4 宽度和高度属性 public long Width { readonly get => right - left; set => right = left + value; } public long Height { readonly get => bottom - top; set => bottom = top + value; } 这些属性同时提供了 getter 和 setter,允许通过设置宽高来调整矩形大小。
阅读全文