美文网首页
2018暑期SICNU-ACM组集训报告(1)

2018暑期SICNU-ACM组集训报告(1)

作者: 姬空魂 | 来源:发表于2018-07-30 00:25 被阅读0次

    题目:

    Xzz need to calculate Intersection over Union(IoU) of two rectangles, can you help him?

    rectangle (x, y, w, h) means a rectangle MNPQ, M(x,y), N(x, y+h), P(x+w, y+h), Q(x+w, y).

    IoU = Area of overlap / Area of union.

    Input

    First line of the input file contains an integer T(0 < T <= 100) that indicates how many cases of inputs are there.

    The description of each case is given below:

    The first line of each input set contains integer x1, y1, w1, h1.

    The second line of each input set contains integer x2, y2, w2, h2.

    0 ≤ x, y, w, h ≤ 100000

    Output

    The description of output for each test case is given below:

    The first line of the output for each test case contains number k- the IoU of two rectangles.

    Output should be rounded to 2 digits after decimal point.

    Sample Input

    2

    1 1 1 1

    1 1 2 2

    1 1 2 1

    1 1 1 2

    Sample Output

    0.25

    0.33

    原题链接

    题意:
    输入两组各四个数据按照一定的方式计算为两个矩形的四点坐标,计算两矩形重合面积和合并面积的比值
    解题思路:
    要算两个面积就分开讨论:
    重合面积:
    由于是矩形,所以知道两个对角点的坐标就可以计算重合面积的大小了。
    首先在横纵坐标中找到两个中间值也就是一共四个值,用它来分别表示重合面积四个边在坐标系中的横纵坐标的值,再相减得到重合矩形的长宽,即可得到该面积。

    合并面积:直接运算,相加后再减去重合面积即可

    这道题还是有坑点,要考虑是否相交。写完了也没检查直接交了一发,WA后检查发现在计算合并面积之后没有减去多加的重合面积(小坑点,但是自己不检查这个习惯真的很大,太自信了)

    AC代码:

    #include<iostream>
    #include<cstdio>
    #define max(x,y) (x>y?x:y)
    #define min(x,y) (x<y?x:y)
    double area_o;
    using namespace std;
    double area_over(int m1,int n1,int m2,int n2)
    {
        
        if(m2>m1 && n2>n1)
            area_o=(m2 -m1)*(n2-n1);
        else
            area_o=0.00;
        return 0;
    }
    
    
    int main(int argc, char const *argv[])
    {
        int n;
        cin>>n;
        double m1,n1,m2,n2;
        double area1=0,area2=0,area_union=0;
        double IoU=0;
        int x1,x2,y1,y2,w1,w2,h1,h2;
        for(int i=0;i<n;i++){
            cin>>x1>>y1>>w1>>h1;
            cin>>x2>>y2>>w2>>h2;
            m1 = max(min(x1,x1+w1),min(x2,x2+w2)); 
            n1 = max(min(y1+h1,y1),min(y2,y2+h2));  
            m2 = min(max(x1,x1+w1),max(x2,x2+w2));
            n2 = min(max(y1+h1,y1),max(y2,y2+h2));
            area_over(m1,n1,m2,n2);
            area1=w1*h1;area2=w2*h2;
            area_union=area1+area2;
            IoU=(area_o*1.0)/((area_union-area_o)*1.0);
            printf("%.2lf\n",IoU);
        }
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:2018暑期SICNU-ACM组集训报告(1)

          本文链接:https://www.haomeiwen.com/subject/erqxvftx.html