UVA 10867 Cutting a Polygon

呵呵

又A不过去  还搜不到题解  给跪

给组高端样例

input:

9 1
0 0
0 2
1 1
2 2
3 1
4 2
5 1
6 2
6 0
0 2 2 0
0 0

output:

2.828

 

//大白p263
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <functional>
#include <set>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const double eps=1e-8;//精度
const int INF=1<<29;
const double PI=acos(-1.0);
int dcmp(double x){//判断double等于0或。。。
	if(fabs(x)<eps)return 0;else return x<0?-1:1;
}
struct Point{
	double x,y;
	Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
typedef vector<Point> Polygon;
Vector operator+(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}//向量+向量=向量
Vector operator-(Point a,Point b){return Vector(a.x-b.x,a.y-b.y);}//点-点=向量
Vector operator*(Vector a,double p){return Vector(a.x*p,a.y*p);}//向量*实数=向量
Vector operator/(Vector a,double p){return Vector(a.x/p,a.y/p);}//向量/实数=向量
bool operator<( const Point& A,const Point& B ){return dcmp(A.x-B.x)<0||(dcmp(A.x-B.x)==0&&dcmp(A.y-B.y)<0);}
bool operator==(const Point&a,const Point&b){return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;}
bool operator!=(const Point&a,const Point&b){return a==b?false:true;}
struct Segment{
	Point a,b;
	Segment(){}
	Segment(Point _a,Point _b){a=_a,b=_b;}
	bool friend operator<(const Segment& p,const Segment& q){return p.a<q.a||(p.a==q.a&&p.b<q.b);}
	bool friend operator==(const Segment& p,const Segment& q){return (p.a==q.a&&p.b==q.b)||(p.a==q.b&&p.b==q.a);}
};
struct Circle{
	Point c;
	double r;
	Circle(){}
	Circle(Point _c, double _r):c(_c),r(_r) {}
	Point point(double a)const{return Point(c.x+cos(a)*r,c.y+sin(a)*r);}
	bool friend operator<(const Circle& a,const Circle& b){return a.r<b.r;}
};
struct Line{
	Point p;
	Vector v;
	double ang;
	Line() {}
	Line(const Point &_p, const Vector &_v):p(_p),v(_v){ang = atan2(v.y, v.x);}
	bool operator<(const Line &L)const{return  ang < L.ang;}
};
double Dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;}//|a|*|b|*cosθ 点积
double Length(Vector a){return sqrt(Dot(a,a));}//|a| 向量长度
double Angle(Vector a,Vector b){return acos(Dot(a,b)/Length(a)/Length(b));}//向量夹角θ
double Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;}//叉积 向量围成的平行四边形的面积
double Area2(Point a,Point b,Point c){return Cross(b-a,c-a);}//同上 参数为三个点
double DegreeToRadius(double deg){return deg/180*PI;}
Vector Normal(Vector a){//计算单位法线
	double L=Length(a);
	return Vector(-a.y/L,a.x/L);
}
Point GetLineProjection(Point p,Point a,Point b){//点在直线上的投影
	Vector v=b-a;
	return a+v*(Dot(v,p-a)/Dot(v,v));
}
Point GetLineIntersection(Point p,Vector v,Point q,Vector w){//求直线交点 有唯一交点时可用
	Vector u=p-q;
	double t=Cross(w,u)/Cross(v,w);
	return p+v*t;
}
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2){//线段规范相交判定
	double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1);
	double c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
	return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;
}
double DistanceToSegment(Point p,Segment s){//点到线段的距离
	if(s.a==s.b) return Length(p-s.a);
	Vector v1=s.b-s.a,v2=p-s.a,v3=p-s.b;
	if(dcmp(Dot(v1,v2))<0) return Length(v2);
	else if(dcmp(Dot(v1,v3))>0) return Length(v3);
	else return fabs(Cross(v1,v2))/Length(v1);
}
bool isPointOnSegment(Point p,Segment s){
	return dcmp(Cross(s.a-p,s.b-p))==0&&dcmp(Dot(s.a-p,s.b-p))<0;
}
int isPointInPolygon(Point p, Point* poly,int n){//点与多边形的位置关系
	int wn=0;
	for(int i=0;i<n;i++){
		Point& p2=poly[(i+1)%n];
		if(isPointOnSegment(p,Segment(poly[i],p2))) return -1;//点在边界上
		int k=dcmp(Cross(p2-poly[i],p-poly[i]));
		int d1=dcmp(poly[i].y-p.y);
		int d2=dcmp(p2.y-p.y);
		if(k>0&&d1<=0&&d2>0)wn++;
		if(k<0&&d2<=0&&d1>0)wn--;
	}
	if(wn) return 1;//点在内部
	else return 0;//点在外部
}
//--------------------------------------
//--------------------------------------
//--------------------------------------
//--------------------------------------
//--------------------------------------
int n,m;
Point arr[1010];
struct node{
	Point p;
	bool mark;
	bool friend operator<(node a,node b){
		return a.p<b.p;
	}
}ans[3000];
int main(){
	while(scanf("%d%d",&n,&m)&&(n+m)){
		for(int i=0;i<n;i++){
			scanf("%lf%lf",&arr[i].x,&arr[i].y);
		}
		int idx;
		while(m--){
			Point a,b;
			scanf("%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y);
			idx=0;
			double out=0;
			for(int i=0;i<n;i++){
				Segment s(arr[i],arr[(i+1)%n]);
				if(dcmp(Cross(s.b-s.a,b-a))!=0){
					Point ip=GetLineIntersection(a,b-a,s.a,s.b-s.a);
					if(dcmp(DistanceToSegment(ip,s))==0) ans[idx].p=ip;
					Vector v=(b-a)/Length(b-a);
					Point ip2=ip;
					if(dcmp(v.x)<0) v=v*-1;
					else if(dcmp(v.x)==0&&dcmp(v.y)<0) v.y=-v.y;
					ip2.x+=v.x*eps*10;
					ip2.y+=v.y*eps*10;
					if(isPointInPolygon(ip2,arr,n)==1) ans[idx++].mark=true;
					else ans[idx++].mark=false;
				}
				else{
					Point pp=GetLineProjection(s.a,a,b);
					if(s.a==pp) out+=Length(s.a-s.b);
				}
			}
			sort(ans,ans+idx);
			for(int i=1;i<idx;i++)if(ans[i-1].mark){
				out+=Length(ans[i].p-ans[i-1].p);
			}
			printf("%.3lf\n",out);
		}
	}
	return 0;
}


 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值