题意:判断两个多边形是否有面积大于0的公共部分

思路:扫描线基础。

#pragma comment(linker, "/STACK:10240000")
#include <bits/stdc++.h>
using namespace std; #define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a)) typedef long long ll;
typedef pair<int, int> pii; namespace Debug {
void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<" ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
}
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}
/* -------------------------------------------------------------------------------- */ const double eps = 1e-10;/** 设置比较精度 **/
struct Real {
double x;
double get() { return x; }
Real(const double &x) { this->x = x; }
Real() {} Real operator + (const Real &that) const { return Real(x + that.x);}
Real operator - (const Real &that) const { return Real(x - that.x);}
Real operator * (const Real &that) const { return Real(x * that.x);}
Real operator / (const Real &that) const { return Real(x / that.x);} Real operator += (const Real &that) { return Real(x += that.x); }
Real operator -= (const Real &that) { return Real(x -= that.x); }
Real operator *= (const Real &that) { return Real(x *= that.x); }
Real operator /= (const Real &that) { return Real(x /= that.x); } bool operator < (const Real &that) const { return x - that.x <= -eps; }
bool operator > (const Real &that) const { return x - that.x >= eps; }
bool operator == (const Real &that) const { return x - that.x > -eps && x - that.x < eps; }
bool operator <= (const Real &that) const { return x - that.x < eps; }
bool operator >= (const Real &that) const { return x - that.x > -eps; }
}; struct Point {
Real x, y;
int read() { return scanf("%lf%lf", &x.x, &y.x); }
Point(const Real &x, const Real &y) { this->x = x; this->y = y; }
Point() {}
Point operator + (const Point &that) const { return Point(this->x + that.x, this->y + that.y); }
Point operator - (const Point &that) const { return Point(this->x - that.x, this->y - that.y); }
Real operator * (const Point &that) const { return x * that.x + y * that.y; }
Point operator * (const Real &that) const { return Point(x * that, y * that); }
Point operator += (const Point &that) { return Point(this->x += that.x, this->y += that.y); }
Point operator -= (const Point &that) { return Point(this->x -= that.x, this->y -= that.y); }
Point operator *= (const Real &that) { return Point(x *= that, y *= that); }
Real cross(const Point &that) const { return x * that.y - y * that.x; }
};
typedef Point Vector; struct Segment {
Point a, b;
Segment(const Point &a, const Point &b) { this->a = a; this->b = b; }
Segment() {}
bool intersect(const Segment &that) const {
Point c = that.a, d = that.b;
Vector ab = b - a, cd = d - c, ac = c - a, ad = d - a, ca = a - c, cb = b - c;
return ab.cross(ac) * ab.cross(ad) < && cd.cross(ca) * cd.cross(cb) < ;
}
Point getLineIntersection(const Segment &that) const {
Vector u = a - that.a, v = b - a, w = that.b - that.a;
Real t = w.cross(u) / v.cross(w);
return a + v * t;
}
}; Point p1[], p2[];
Segment side1[], side2[]; bool cmp(const pair<Segment, int> &a, const pair<Segment, int> &b) {
return a.X.a.x + a.X.b.x < b.X.a.x + b.X.b.x;
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
int n, m, cas = ;
while (cin >> n) {
for (int i = ; i < n; i ++) {
p1[i].read();
if (i) side1[i - ] = Segment(p1[i - ], p1[i]);
}
side1[n - ] = Segment(p1[n - ], p1[]);
cin >> m;
for (int i = ; i < m; i ++) {
p2[i].read();
if (i) side2[i - ] = Segment(p2[i - ], p2[i]);
}
side2[m - ] = Segment(p2[m - ], p2[]);
/** 得到所有的扫描线并排序去重 **/
vector<Real> Y;
for (int i = ; i < n; i ++) Y.pb(p1[i].y);
for (int i = ; i < m; i ++) Y.pb(p2[i].y);
for (int i = ; i < n; i ++) {
for (int j = ; j < m; j ++) {
if (side1[i].intersect(side2[j])) {
Y.pb(side1[i].getLineIntersection(side2[j]).y);
}
}
}
sort(all(Y));
Y.resize(unique(all(Y)) - Y.begin());
//Debug::print("Y.size=", Y.size());
Real area = ;
for (int i = ; i < Y.size(); i ++) {
vector<pair<Segment, int> > V;
/** 得到扫描线之间的所有线段 **/
for (int j = ; j < n; j ++) {
Real miny = side1[j].a.y, maxy = side1[j].b.y;
if (miny > maxy) swap(miny, maxy);
if (miny <= Y[i - ] && maxy >= Y[i]) {
Point dot1 = side1[j].getLineIntersection(Segment(Point(, Y[i - ]), Point(, Y[i - ])));
Point dot2 = side1[j].getLineIntersection(Segment(Point(, Y[i]), Point(, Y[i])));
V.pb(mp(Segment(dot1, dot2), ));
}
}
for (int j = ; j < m; j ++) {
Real miny = side2[j].a.y, maxy = side2[j].b.y;
if (miny > maxy) swap(miny, maxy);
if (miny <= Y[i - ] && maxy >= Y[i]) {
Point dot1 = side2[j].getLineIntersection(Segment(Point(, Y[i - ]), Point(, Y[i - ])));
Point dot2 = side2[j].getLineIntersection(Segment(Point(, Y[i]), Point(, Y[i])));
V.pb(mp(Segment(dot1, dot2), ));
}
}
sort(all(V), cmp);
//Debug::print("V.size=", V.size());
/** 从左至右统计 **/
bool in1 = , in2 = ;/** 当前延伸的区域是否在多边形内部 **/
for (int i = ; i < V.size(); i ++) {
if (in1 && in2) area += V[i].X.a.x - V[i - ].X.a.x + V[i].X.b.x - V[i - ].X.b.x;
if (V[i].Y) in2 ^= ;
else in1 ^= ;
}
}
printf("Case %d: %s\n", ++ cas, area > ? "Yes" : "No");
}
return ;
}

[UVA Live 12931 Common Area]扫描线的更多相关文章

  1. UVA 10405 Longest Common Subsequence

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=16&p ...

  2. UVA 10405 Longest Common Subsequence (dp + LCS)

    Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...

  3. UVA 10405 Longest Common Subsequence --经典DP

    最长公共子序列,经典问题.算是我的DP开场题吧. dp[i][j]表示到s1的i位置,s2的j位置为止,前面最长公共子序列的长度. 状态转移: dp[i][j] = 0                 ...

  4. [UVa OJ] Longest Common Subsequence

    This is the classic LCS problem. Since it only requires you to print the maximum length, the code ca ...

  5. UVA 10522 Height to Area(知三角形三高求面积)

    思路:海伦公式, AC代码: #include<bits/stdc++.h> using namespace std; int main() { int n; scanf("%d ...

  6. 【uva 12219】Common Subexpression Elimination(图论--树+自定义比较器+映射+递归)

    题意:如题,用表达式树来表示一个表达式,且消除公共的部分,即用编号表示.编号 K 定义为表达式第 K 个出现的字符串. 解法:先构造表达式树,给每棵子树用(string,left_son,right_ ...

  7. hdu---(Tell me the area)(几何/三角形面积以及圆面积的一些知识)

    Tell me the area Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. HDU 1798 Tell me the area

    http://acm.hdu.edu.cn/showproblem.php?pid=1798 Problem Description     There are two circles in the ...

  9. HDU 1798 Tell me the area (数学)

    题目链接 Problem Description     There are two circles in the plane (shown in the below picture), there ...

随机推荐

  1. Android内存优化—dumpsys meminfo详解

    原创置顶 不死鸟JGC 最后发布于2018-12-24 14:19:28 阅读数 3960 收藏展开dumpsys 介绍Dumpsys用户系统诊断,它运行在设备上,并提供系统服务状态信息 命令格式: ...

  2. Suctf知识记录&&PHP代码审计,无字母数字webshell&&open_basedir绕过&&waf+idna+pythonssrf+nginx

    Checkin .user.ini构成php后门利用,设置auto_prepend_file=01.jpg,自动在文件前包含了01.jpg,利用.user.ini和图片马实现文件包含+图片马的利用. ...

  3. BJDCTF 2nd web

    先贴一下Y1ng大佬的WP elementmaster 脑洞确实大,源码中hidden的id可以用hex解码成Po. 在URL后面输入Po.php得到一个点, 然后不知所措 被水淹没 实际上这里是要遍 ...

  4. [YII2] 自带分页调整

    在search Model的search()方法里有一个$dataProvider 属性 ,在这个属性数组里添加 'pagination' => ['pageSize' => 10,],设 ...

  5. python进入adb shell交互模式

    import subprocess #方法一:进入某个环境执行语句(adb shell),注意shell内部命令需要带\n,执行完后一定记得执行exit命令退出,否则会阻塞 obj = subproc ...

  6. 2019-2020-1 20199310《Linux内核原理与分析》第一周作业

    1.问题描述 1.1 问题一 Linux文件系统中的文件是数据的集合,文件系统不仅包含着文件中的数据而且还有文件系统的结构,探究根目录下主要文件用途. 1.2 问题二 有一个非常重要的文件(passw ...

  7. Docker镜像与仓库(四)

    Dockerfile方式创建镜像 https://hub.docker.com/_/centos/ #找一个centos6.6 的dockerfile链接 [root@linux-node1 ~]# ...

  8. JDK11的重要新特性

    文章目录 JDK11发布啦 Oracle不再提供JRE和Server JRE下载 删除部署工具 JavaFX不再包含在JDK中 删除Java EE和CORBA模块 JDK11发布啦 JDK11 在20 ...

  9. OC的消息机制简单介绍

    在OC的消息机制中主要分为三个阶段,分别为: 1.消息发送阶段:从类以及父类的方法缓存列表和方法列表查找方法. 2.动态解析阶段:在消息发送阶段没有找到方法,则会进入这个阶段,负责动态添加方法实现. ...

  10. PCA主成分分析(上)

    PCA主成分分析 PCA目的 最大可分性(最大投影方差) 投影 优化目标 关键点 推导 为什么要找最大特征值对应的特征向量呢? 之前看3DMM的论文的看到其用了PCA的方法,一开始以为自己对于PCA已 ...