目录
- 1 专题说明
- 2 算法
- 参考
1 专题说明
本专题用来记录计算几何相关算法,包括:
- 求两个矩形的交集。
2 算法
算法:求两个矩形的交集
C++实现,
// 定义矩形结构体
struct Rectangle {int x1, y1; // 左下角坐标int x2, y2; // 右上角坐标
};// 求两个矩形的交集,返回交集矩形
Rectangle intersection(Rectangle rect1, Rectangle rect2) {Rectangle result;// 左下角坐标取两个矩形中横纵坐标的较大值result.x1 = max(rect1.x1, rect2.x1);result.y1 = max(rect1.y1, rect2.y1);// 右上角坐标取两个矩形中横纵坐标的较小值result.x2 = min(rect1.x2, rect2.x2);result.y2 = min(rect1.y2, rect2.y2);// 如果左下角坐标大于右上角坐标,则说明矩形不相交,置为无效值if (result.x1 > result.x2 || result.y1 > result.y2) {result.x1 = result.y1 = result.x2 = result.y2 = -1;}return result;
}
参考
待补充。。。