传送门:csu 1812: 三角形和矩形
思路:首先,求出三角形的在矩形区域的顶点,矩形在三角形区域的顶点。然后求出所有的交点。这些点构成一个凸包,求凸包面积就OK了。
/**************************************************************
Problem:
User: youmi
Language: C++
Result: Accepted
Time:
Memory:
****************************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <cmath>
#include <queue>
#include <deque>
#include <string>
#include <vector>
#define zeros(a) memset(a,0,sizeof(a))
#define ones(a) memset(a,-1,sizeof(a))
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d%d",&a,&b)
#define sc3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define scs(a) scanf("%s",a)
#define sclld(a) scanf("%I64d",&a)
#define pt(a) printf("%d\n",a)
#define ptlld(a) printf("%I64d\n",a)
#define rep(i,from,to) for(int i=from;i<=to;i++)
#define irep(i,to,from) for(int i=to;i>=from;i--)
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define lson (step<<1)
#define rson (lson+1)
#define eps 1e-6
#define oo 0x3fffffff
#define TEST cout<<"*************************"<<endl
const double pi=*atan(1.0); using namespace std;
typedef long long ll;
template <class T> inline void read(T &n)
{
char c; int flag = ;
for (c = getchar(); !(c >= '' && c <= '' || c == '-'); c = getchar()); if (c == '-') flag = -, n = ; else n = c - '';
for (c = getchar(); c >= '' && c <= ''; c = getchar()) n = n * + c - ''; n *= flag;
}
ll Pow(ll base, ll n, ll mo)
{
if (n == ) return ;
if (n == ) return base % mo;
ll tmp = Pow(base, n >> , mo);
tmp = (ll)tmp * tmp % mo;
if (n & ) tmp = (ll)tmp * base % mo;
return tmp;
}
//*************************** int n;
const int maxn=+;
const ll mod=;
double xx[],yy[];
int sgn(double x)
{
if(fabs(x)<eps)
return ;
if(x<)
return -;
else
return ;
}
struct point
{
double x,y;
point(){};
point(double _x,double _y)
{
x=_x,y=_y;
}
point operator-(const point &_b)const
{
return point(x-_b.x,y-_b.y);
}
double operator *(const point &_b)const
{
return x*_b.x+y*_b.y;
}
double operator^(const point &_b)const
{
return x*_b.y-_b.x*y;
}
bool operator==(const point &_b)const
{
return sgn(x-_b.x)==&&sgn(y-_b.y)==;
}
};
point tri[],rec[];
double dist(point a,point b)
{
return sqrt((a-b)*(a-b));
}
struct line
{
point s, e;
line() {}
line(point _s, point _e)
{
s = _s;
e = _e;
}
pair<int, point> operator &(const line &b)const
{
point res = s;
if(sgn((s - e) ^ (b.s - b.e)) == ) {
if(sgn((s - b.e) ^ (b.s - b.e)) == )
return make_pair(, res); //重合
else return make_pair(, res); //平行
}
long double t = ((s - b.s) ^ (b.s - b.e)) / ((s - e) ^ (b.s - b.e));
res.x += (e.x - s.x) * t;
res.y += (e.y - s.y) * t;
return make_pair(, res);
}
};
point lst[maxn];
int stc[maxn],top;
bool _cmp(point p1,point p2)
{
double temp=(p1-lst[])^(p2-lst[]);
if(sgn(temp)>)
return true;
else if(sgn(temp)==&&sgn(dist(p1,lst[])-dist(p2,lst[]))<=)
return true;
else
return false;
}
bool on_line(point p,line uu)
{
return (sgn(p.x-uu.s.x)*sgn(p.x-uu.e.x))<=&&(sgn(p.y-uu.s.y)*sgn(p.y-uu.e.y)<=);
}
void graham()
{
if(n==)
{
top=;
return;
}
point p0=lst[];
int k=;
for(int i=;i<n;i++)
{
if((p0.y>lst[i].y)||(p0.y==lst[i].y&&p0.x>lst[i].x))
{
p0=lst[i];
k=i;
}
}
swap(lst[k],lst[]);
sort(lst+,lst+n,_cmp);
if(n==)
{
top=;
stc[]=;
return ;
}
top=;
stc[]=;
stc[]=;
if(n==)
return ;
for(int i=;i<n;i++)
{
while(top>&&sgn((lst[stc[top-]]-lst[stc[top-]])^(lst[i]-lst[stc[top-]]))<=)
top--;
stc[top++]=i;
}
}
bool in_tri(point p)
{
double s=fabs((tri[]-tri[])^(tri[]-tri[]));
double s1=fabs((p-tri[])^(p-tri[]));
double s2=fabs((p-tri[])^(p-tri[]));
double s3=fabs((p-tri[])^(p-tri[]));
return sgn(s1+s2+s3-s)==;
}
bool in_rec(point p)
{
return sgn(p.x-xx[])>=&&sgn(p.x-xx[])<=&&sgn(p.y-yy[])>=&&sgn(p.y-yy[])<=;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
while(~scanf("%lf%lf%lf%lf%lf%lf%lf%lf",xx+,yy+,xx+,yy+,xx+,yy+,xx+,yy+))
{
tri[]=point(xx[],yy[]),tri[]=point(xx[],yy[]),tri[]=point(xx[],yy[]);
rec[]=point(xx[],yy[]),rec[]=point(xx[],yy[]),rec[]=point(xx[],yy[]),rec[]=point(xx[],yy[]);
n=;
rep(i,,)
if(in_tri(rec[i]))
lst[n++]=rec[i];
rep(i,,)
if(in_rec(tri[i]))
lst[n++]=tri[i];
rep(i,,)
{
line gg=line(rec[i],rec[(i+)%]);
rep(j,,)
{
line hh=line(tri[j],tri[(j+)%]);
pair<int, point> res=hh≫
if(res.first==)
{
point &uu=res.second;
if(on_line(uu,gg)&&on_line(uu,hh))
lst[n++]=uu;
}
}
}
sort(lst,lst+n,_cmp);
n=unique(lst,lst+n)-lst;
graham();
double ans=;
if(top>=)
{
rep(i,,top-)
{
ans+=lst[stc[i]]^lst[stc[(i+)%top]];
}
}
ans/=;
printf("%.7f\n",ans);
}
}