Calculate the perimeter and area of intersecting rectangles?

前端 未结 2 1528
囚心锁ツ
囚心锁ツ 2021-01-18 12:04

I searched a lot, but I didn\'t find a good answer that works for this case. We have some rectangles that are horizontal or vertical. They can be placed on the page randomly

2条回答
  •  庸人自扰
    2021-01-18 12:42

    The following is O(N2) solution.

    int area = 0;
    FOR(triange=0->N)
    {
        Area = area trianlges[triangle];
        FOR(int j = triangle+1 -> N)
        {
            area-= inter(triangle , j)
        }
    }
    return area;
    
    int inter(tri a,tri b)
    {
        if ( ( min(a.highY ,b.highY) > max(a.lowerY, b.lowerY) ) && ( min(a.highX ,b.highX) > max(a.lowerX, b.lowerX) ) )
        return ( min(a.highY ,b.highY) - max(a.lowerY, b.lowerY) ) * ( min(a.highX ,b.highX) - max(a.lowerX, b.lowerX) )
    
        else return 0;
    }
    

提交回复
热议问题