const double pi=acos(-1);
const double eps=1e-6;//精度
int sgn(double a, double b) {
if(fabs(a-b)<eps) return 0;
if(a<b) return -1;
return 1;
}
struct point {
double x,y,z;
point() {}
point( double tx, double ty,double tz) {
x = tx, y = ty,z = tz;
}
bool operator < (const point &a) const {//按照 x<y<z 的优先顺序对点进行排序
return x<a.x or (x==a.x and y<a.y) or (x==a.x and y==a.y and z==a.z);
}
friend point operator + (const point &a,const point &b) {// 向量加法
return point(a.x + b.x, a.y + b.y, a.z + b.z);
}
friend point operator - (const point &a,const point &b) {//向量减法
return point(a.x - b.x, a.y - b.y, a.z - b.z);
}
friend double operator * (const point &a,const point &b) {//点乘积
return a.x*b.x+a.y*b.y+a.z*b.z;
}
bool operator == (const point &a)const {//允许两点之间有精度误差
return sgn(x,a.x)==0 and sgn(y,a.y)==0 and sgn(z,a.z);
}
};
double dis(point a,point b) { //三维平面点和点之间的距离
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));
}
double dis2(point a,point b) { //三维平面点和点之间的距离的平方
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z);
}
double cross2(point a,point b) { //二维平面叉积
return a.x*b.y-a.y*b.x;
}
double cross3(point a,point b) { //三维平面叉积
point num;
num.x=a.y*b.z-b.y*a.z;
num.y=a.x*b.z-b.x*a.z;
num.z=a.x*b.y-b.x*a.y;
return sqrt(num.x*num.x+num.y*num.y+num.z*num.z);
}
double geth(point a,point b,point c) { //余弦定理求角度,c为顶点
double aa=dis(a,c),bb=dis(b,c),cc=dis(a,b);
return acos((aa*aa+bb*bb-cc*cc)/(2*aa*bb));
}
double getd(point a,point b,point c) { //求点c到直线ab的距离,a,b为直线上任意两点
return cross3(b-c,a-c);
}
double gets(point a,point b,point c){//求三角形面积
return dis(a,b)*dis(a,c)*sin(geth(b,c,a));
}
//绕原点旋转角度B(弧度值),后x,y的变化
void transXY(double B){
double tx = x,ty = y;
x = tx*cos(B) - ty*sin(B);
y = tx*sin(B) + ty*cos(B);
}
判断点在线段的哪一侧
int cross(int x1,int y1,int x2,int y2){
return x1*y2-x2*y1;
}
bool judge(point a,point b,point c){//a为点,bc为线段,b在上,c在下
if(cross(b.x-a.x,b.y-a.y,c.x-a.x,c.y-a.y)>0) return true;//a在线段bc的逆时针方向,一般说左侧
else return false;//a在线段bc的顺时针方向,一般说右侧 特别地,==0时为共线
}
//顺(逆)时针方向,以下方的c点为圆心,b点旋转
判断两线段是否相交
const double eps=1e-7;
//a,b为一条线段两端点 c,d为另一条线段的两端点 相交返回false, 不相交返回true
bool Judge(point a, point b, point c, point d) {
if (cross(c.x-a.x,c.y-a.y,b.x-a.x,b.y-a.y) *
cross(b.x-a.x,b.y-a.y,d.x-a.x,d.y-a.y) > eps
and cross(a.x-d.x,a.y-d.y,c.x-d.x,c.y-d.y) *
cross(c.x-d.x,c.y-d.y,b.x-d.x,b.y-d.y) > eps)
return false;
return true;
}
当确定直线和线段相交时,求他们的交点 (利用叉积可判断,线段两端点是否在直线的两侧)
struct Point {
double x,y;
Point(double x=0,double y=0):x(x),y(y) {}
} p[N];
double cross(Point a,Point b,Point c) {
return (a.x-c.x)*(a.y-b.y)-(a.y-c.y)*(a.x-b.x);
}
bool judge(Point p1, Point p2, Point p3, Point p4) {//p1 p2为直线上两点,p3 p4为线段两端点
int d1 = dcmp(cross(p1,p2,p3));
int d2 = dcmp(cross(p1,p2,p4));
if(d1*d2<=0) return true;//线段两端点在直线两侧,==0的时候有一点在直线上
if(d1*d2>0) return false;//线段两端点在直线一侧
}
bool intersection(Point p1, Point p2, Point p3, Point p4, Point& p) {//p即为交点,传参的时候传进去一个
double a1, b1, c1, a2, b2, c2, d;
a1=p1.y-p2.y;
b1=p2.x-p1.x;
c1=p1.x*p2.y-p2.x*p1.y;
a2=p3.y-p4.y;
b2=p4.x-p3.x;
c2=p3.x*p4.y-p4.x*p3.y;
d=a1*b2-a2*b1;
if (!d) return false;
p.x=(-c1*b2+c2*b1)/d;
p.y=(-a1*c2+a2*c1)/d;
return true;
}
求两多边形的交的面积:
/*
* 多边形的交,多边形的边一定是要按逆时针方向给出
* 还要判断是凸包还是凹包,调用相应的函数
* 面积并,只要和面积减去交即可
*/
#include <bits/stdc++.h>
using namespace std;
const int maxn = //300;
const double eps = 1e-8;
int dcmp(double x)
{
if(x > eps) return 1;
return x < -eps ? -1 : 0;
}
struct Point
{
double x, y;
};
double cross(Point a,Point b,Point c) ///叉积
{
return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
Point intersection(Point a,Point b,Point c,Point d)
{
Point p = a;
double t =((a.x-c.x)*(c.y-d.y)-(a.y-c.y)*(c.x-d.x))/((a.x-b.x)*(c.y-d.y)-(a.y-b.y)*(c.x-d.x));
p.x +=(b.x-a.x)*t;
p.y +=(b.y-a.y)*t;
return p;
}
//计算多边形面积
double PolygonArea(Point p[], int n)
{
if(n < 3) return 0.0;
double s = p[0].y * (p[n - 1].x - p[1].x);
p[n] = p[0];
for(int i = 1; i < n; ++ i)
s += p[i].y * (p[i - 1].x - p[i + 1].x);
return fabs(s * 0.5);
}
double CPIA(Point a[], Point b[], int na, int nb)//ConvexPolygonIntersectArea
{
Point p[20], tmp[20];
int tn, sflag, eflag;
a[na] = a[0], b[nb] = b[0];
memcpy(p,b,sizeof(Point)*(nb + 1));
for(int i = 0; i < na && nb > 2; i++)
{
sflag = dcmp(cross(a[i + 1], p[0],a[i]));
for(int j = tn = 0; j < nb; j++, sflag = eflag)
{
if(sflag>=0) tmp[tn++] = p[j];
eflag = dcmp(cross(a[i + 1], p[j + 1],a[i]));
if((sflag ^ eflag) == -2)
tmp[tn++] = intersection(a[i], a[i + 1], p[j], p[j + 1]); ///求交点
}
memcpy(p, tmp, sizeof(Point) * tn);
nb = tn, p[nb] = p[0];
}
if(nb < 3) return 0.0;
return PolygonArea(p, nb);
}
double SPIA(Point a[], Point b[], int na, int nb)///SimplePolygonIntersectArea 调用此函数
{
int i, j;
Point t1[4], t2[4];
double res = 0, num1, num2;
a[na] = t1[0] = a[0], b[nb] = t2[0] = b[0];
for(i = 2; i < na; i++)
{
t1[1] = a[i-1], t1[2] = a[i];
num1 = dcmp(cross(t1[1], t1[2],t1[0]));
if(num1 < 0) swap(t1[1], t1[2]);
for(j = 2; j < nb; j++)
{
t2[1] = b[j - 1], t2[2] = b[j];
num2 = dcmp(cross(t2[1], t2[2],t2[0]));
if(num2 < 0) swap(t2[1], t2[2]);
res += CPIA(t1, t2, 3, 3) * num1 * num2;
}
}
return res;
}
Point p1[maxn], p2[maxn];
int n1, n2;
int main()
{
while(cin>>n1>>n2)
{
for(int i = 0; i < n1; i++) scanf("%lf%lf", &p1[i].x, &p1[i].y);
for(int i = 0; i < n2; i++) scanf("%lf%lf", &p2[i].x, &p2[i].y);
double Area = SPIA(p1, p2, n1, n2);
}
return 0;
}