POJ2451:半平面交模板题

POJ2451

代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
double const eps = 1e-10;
double const M = 10000;
int const N = 20000 + 10;
int n;
int dcmp(double x){
	if(fabs(x) <= eps)	return 0;
	else return x < 0 ? -1 : 1;	
}
typedef struct Point 
{
	double x,y;
	Point(){};
	Point(double x,double y):x(x),y(y){};
	Point operator - (const Point& e)const{
		return Point(x - e.x,y - e.y);
	}
	Point operator + (Point const &e)const{
		return Point(x + e.x,y + e.y);
	}
	Point operator * (double const &e)const{
		return Point(x * e,y * e);
	}
}Vector;
Point t[N],p[N];
double cross(Vector a,Vector b){
	return a.x * b.y - a.y * b.x;
}
struct Line
{
	Point a,b;
	double ang;
	void getang(){ang = atan2(b.y - a.y,b.x - a.x);}
	bool operator < (const Line& e)const{
		return ang < e.ang || !dcmp(ang - e.ang) && dcmp(cross(e.b - e.a,b - e.a)) > 0;
	}
	bool operator == (const Line& e)const{
		return !dcmp(ang - e.ang);
	}
}s[N],q[N];
bool onright(Point a,Point b,Point p){
	return dcmp(cross(b - a,p - a)) < 0;
}
Point interserction(Point p,Vector v,Point q,Vector w){
	Vector u = p - q;
	double t = cross(w,u) / cross(v,w);
	return p + v * t;
}
int halfplaneintersection(){
	int l,r;
	q[l=r=0] = s[0];
	for(int i=1;i<n;i++){
		if(s[i] == s[i-1])	continue;
		while(l < r && onright(s[i].a,s[i].b,t[r-1]))	r--;
		while(l < r && onright(s[i].a,s[i].b,t[l]))	l++;
		q[++r] = s[i];
		if(l < r)	t[r-1] = interserction(q[r].a,q[r].a-q[r].b,q[r-1].a,q[r-1].a-q[r-1].b);
	}s
	while(l < r && onright(q[l].a,q[l].b,t[r-1]))	r--;
	t[r] = interserction(q[l].a,q[l].a-q[l].b,q[r].a,q[r].a-q[r].b);
	int m = 0;
	for(int i=l;i<=r;i++)	p[m++] = t[i];	
	return m;
}
double getarea(int n){
	double ans = 0;
	for(int i=1;i<n-1;i++)
		ans += cross(p[i] - p[0],p[i+1] - p[0]);
	return fabs(ans) / 2;
}
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++)
		scanf("%lf%lf%lf%lf",&s[i].a.x,&s[i].a.y,&s[i].b.x,&s[i].b.y);
	s[n++] = (Line){Point(0,0),Point(M,0)};
	s[n++] = (Line){Point(M,0),Point(M,M)};
	s[n++] = (Line){Point(M,M),Point(0,M)};
	s[n++] = (Line){Point(0,M),Point(0,0)};
	for(int i=0;i<n;i++)	s[i].getang();
	sort(s,s+n);
	int m = halfplaneintersection();
	printf("%.1f\n",getarea(m));
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值