Description
Bobo 有一个三角形和一个矩形,他想求他们交的面积。
1,y 1,x 2,y 2,x 3,y 3,x 4,y 4 描述。 表示三角形的顶点坐标是 (x 1,y 1),(x 1,y 2),(x 2,y 1), 矩形的顶点坐标是 (x 3,y 3),(x 3,y 4),(x 4,y 4),(x 4,y 3).
Input
输入包含不超过 30000 组数据。
1,y 1,x 2,y 2 (x 1≠x 2,y 1≠y 2).
3,y 3,x 4,y 4 (x 3<x 4,y 3<y 4).
i,y i≤10 4)
Output
-6
Sample Input
1 1 3 3
0 0 2 2
0 3 3 1
0 0 2 2
4462 1420 2060 2969
4159 257 8787 2970
Sample Output
#include<set>标签:矩形,1812,get,int,double,mid,CSU,include,define From: https://blog.51cto.com/u_15870896/5838617
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define ff first
#define ss second
#define mp(i,j) make_pair(i,j)
#define pb push_back
#define pii pair<int,LL>
#define in(x) scanf("%d", &x);
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-9;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
int T, n, m;
int x[5], y[5];
double get(double g)
{
if (g < min(x[1], x[2]) || g > max(x[1], x[2])) return -INF;
double k = 1.0*fabs(g - x[2]) / fabs(x[1] - x[2]);
double L = y[1], R = y[1] + (y[1] < y[2] ? k : -k)*abs(y[1] - y[2]);
if (L > R) swap(L, R);
return max(min(1.0*y[4], R) - max(1.0*y[3], L), 0.0);
}
int main()
{
while (scanf("%d%d", &x[1], &y[1]) != EOF)
{
rep(i, 2, 4) scanf("%d%d", &x[i], &y[i]);
double L = get(x[3]), ans = 0;
rep(i, x[3] + 1, x[4])
{
double R = get(i);
if (L != -INF && R != -INF)
{
if (fabs(L + R - 2 * get(i - 0.5)) < eps) ans += (L + R) / 2;
else
{
double l = i - 1, r = i;
while (l + eps < r)
{
double mid = (l + r) / 2;
if (fabs(get(mid) - L) < eps) l = mid; else r = mid;
}
double q = i - 1, h = i;
while (q + eps < h)
{
double mid = (q + h) / 2;
if (fabs(get(mid) - R) < eps) h = mid; else q = mid;
}
ans += L*(l - i + 1) + R*(i - q) + (L + R)*(q - l) / 2;
}
}
L = R;
}
printf("%.8lf\n", ans);
}
return 0;
}