这模版真累啊
//
// main.cpp
// Richard
//
// Created by 邵金杰 on 16/8/9.
// Copyright © 2016年 邵金杰. All rights reserved.
//
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
#define EPS 1e-6
struct Point{
double x,y;
Point(double x,double y): x(x),y(y) {}
};
struct Vector{
double x,y;
Vector(double x,double y): x(x),y(y) {}
};
struct Line{
Point a,b;
Line(Point a,Point b): a(a),b(b) {}
};
bool IsZero(double x)
{
return -EPS<x&&x<EPS;
}
double operator * (Vector p,Vector q)
{
return p.x*q.x+p.y*q.y;
}
Vector operator - (Point p,Point q)
{
return Vector(p.x-q.x,p.y-q.y);
}
double operator ^ (Vector p,Vector q)
{
return p.x*q.y-p.y*q.x;
}
double area(Vector p,Vector q)
{
return (p^q)/2;
}
Vector operator * (double k,Vector p)
{
return Vector(k*p.x,k*p.y);
}
Point operator + (Point p,Vector q)
{
return Point(p.x+q.x,p.y+q.y);
}
bool IsFormalCross(Point p1,Point p2,Point p3,Point p4){
return ((p3-p1)^(p2-p1))*((p2-p1)^(p4-p1))<-EPS&&((p1-p3)^(p4-p3))*((p4-p3)^(p2-p3))<-EPS;
}
double Cross(Vector p,Vector q)
{
return p.x*q.y-p.y*q.x;
}
bool operator == (Point p,Point q)
{
return p.x==q.x&&p.y==q.y;
}
bool FlessEq(double a,double b){
return b-a>EPS||IsZero(b-a);
}
bool PointSeg(Point p,Line s){
double temp=Cross(p-s.a,s.b-s.a);
if(!IsZero(temp)) return false;
if((FlessEq(min(s.a.x,s.b.x),p.x))
&&FlessEq(p.x,max(s.a.x,s.b.x))
&&FlessEq(min(s.a.y,s.b.y),p.y)
&&FlessEq(p.y,max(s.a.y,s.b.y)))
return true;
return false;
}
double length(Vector p){
return sqrt(p*p);
}
double Distance(Point p,Line s){
return area(p-s.a,s.b-s.a)/length(s.b-s.a);
}
int sign(double x)
{
return fabs(x)<EPS?0:x>0?1:-1;
}
//两条线段相交的各种情况
//结果保存在pair<int,Point> 中
//返回值 result.first:
//0 规范相交,
//1 端点重合,但不平行,不共线
//2 一个端点在另一个内部 s1端点在 s2内部 (不平行,不共线)
//3 一个端点在另一个内部 s2端点在 s1内部 (不平行,不共线)
//4 无交点,不平行,不共线,两直线交点是result.second
//5 平行
//6 共线且有公共点
//7 共线且无公共点
//8 s1,s2无交点,但是s2所在直线和s1有交点,即交点在s1上
//9 s1,s2无交点,但是s1所在直线和s2有交点,即交点在s2上
pair<int,Point> CrossPoint(Line s1,Line s2){
Point p1=s1.a,p2=s1.b;
Point p3=s2.a,p4=s2.b;
double a1=area(p3-p1,p4-p1);
double a2=area(p3-p2,p4-p2);
if(IsFormalCross(p1, p2, p3, p4))
return make_pair(0,p1+(a1/(a1+a2))*(p2-p1));
if(!IsZero(Cross(p2-p1,p4-p3))){
if(p1==p3||p1==p4)
return make_pair(1,p1);
if(p2==p3||p2==p4)
return make_pair(1,p2);
if(PointSeg(p1,s2))
return make_pair(2,p1);
if(PointSeg(p2,s2))
return make_pair(2,p2);
if(PointSeg(p3,s1))
return make_pair(3,p3);
if(PointSeg(p4,s1))
return make_pair(3,p4);
Point crossPoint=p1+(a1/(a1+a2))*(p2-p1);
if(PointSeg(crossPoint,s1))
return make_pair(8,crossPoint);
if(PointSeg(crossPoint,s2))
return make_pair(9,crossPoint);
return make_pair(4,crossPoint);
}
if(!IsZero(Distance(p1,s2)))
return make_pair(5,Point(0,0));
if(PointSeg(p1,s2))
return make_pair(6,p1);
if(PointSeg(p2,s2))
return make_pair(6,p2);
if(PointSeg(p3,s1))
return make_pair(6,p3);
if(PointSeg(p4,s1))
return make_pair(6,p4);
return make_pair(7,Point(0,0));
}
typedef vector<Point> Polygon;
int CutPolygon(const Polygon &src,Point a,Point b,Polygon &result)
{
int n=(int)src.size();
result.clear();
for(int i=0;i<n;i++)
{
Point c=src[i];
Point d=src[(i+1)%n];
if(sign(Cross(b-a,c-a))>=0) result.push_back(c);
pair<int,Point> r=CrossPoint(Line(c,d),Line(a,b));
if(r.first==0||r.first==8||r.first==3) result.push_back(r.second);
}
return (int)result.size();
}