题目:判断两矩形是否相交,相交的话 求出相交的面积和相交处比例
分析:
1. 首先判断两矩形分别上下左右四个点是否有交集,没交点返回0,否则则有交集。继续运行。
2. 求出x轴上相交的距离
3. 求出y轴上相交的距离
4. 求出相交的面积
5.求出相交处所占的比例
float bbOverlap(const BoundingBox& box1,const BoundingBox& box2)
{
if (box1.x > box2.x+box2.width) { return 0.0; }
if (box1.y > box2.y+box2.height) { return 0.0; }
if (box1.x+box1.width < box2.x) { return 0.0; }
if (box1.y+box1.height < box2.y) { return 0.0; }
float colInt = abs( min(box1.x+box1.width,box2.x+box2.width) - max(box1.x, box2.x));
float rowInt = abs( min(box1.y+box1.height,box2.y+box2.height) - max(box1.y,box2.y));
float overlapArea = colInt * rowInt; //相交处面积
float area1 = box1.width*box1.height;
float area2 = box2.width*box2.height;
return intersection / (area1 + area2 - intersection); //相交处的比例
}