18 矩形面积交(线段重叠)

【思路】:将各个边平行到x、y轴上,重叠部分相乘得面积。getLen中是两条线相交的各种判断。注意:因为输入一个矩形的两个点并没有说先输入左下再输入右上(测试数据也确实有先输入了右上),所以一定要两两排序。否则会出现a大于b的情况。

【AC代码】:

#include <iostream>#include <algorithm>#include <iomanip>#include <cstdio>#include <cstring>using namespace std;double getLen(double x[]){double a = x[0], b = x[1], c = x[2], d = x[3];if (b < c){return 0.0;}else if (b >= c && b <= d){if (a > c)return b-a;elsereturn b-c;}else if (b > d){if (a <= c)return d-c;else if (a > c && a < d)return d-a;elsereturn 0.0;}}int main(){//freopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout);double x[4], y[4];int i = 0;//inputfor (i = 0; i < 4; i++){cin >> x[i] >> y[i];}//get lensort(x, x+2);sort(x+2, x+4);sort(y, y+2);sort(y+2, y+4);double len_x = getLen(x);double len_y = getLen(y);//cout << len_x << " " << len_y << endl;cout << fixed << setprecision(2) << len_x*len_y;}线段相交曾经看过,应该有更好的方法。这个代码写的很烂。

,有的旅行时为了寻找逝去的年华,重温青春的惆怅。

18 矩形面积交(线段重叠)

相关文章:

你感兴趣的文章:

标签云: