题意:判断两个多边形是否有面积大于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. Java前台传值至后台中文乱码

    记一次常见问题 今天导入了一个网上下载的项目,运行后发现,前台传值 到Servlet,Servlet保存至数据库,数据库保存的中文数据出现乱码,检查了一下表中的编码是utf8没错. 输出测试了一下 原 ...

  2. Everything信息泄露

    Everything漏洞描述 [Everything]一款搜索文件非常快的工具,其速度之快令人震惊!它还有一个可以通过HTTP 或 FTP 分享搜索结果 的功能.它可以让用户在本地或局域网上的其他电脑 ...

  3. 如何使用Jsoup爬取网页内容

    前言: 这是一篇迟到很久的文章了,人真的是越来越懒,前一阵用jsoup实现了一个功能,个人觉得和selenium的webdriver原理类似,所以今天正好有时间,就又来更新分享了. 实现场景: 爬取博 ...

  4. 分享一个404页面(猴子动态SVG图)

    说明 在CSDN看到的一个404界面,简洁大气非常棒我的个人网站已经用上了! 代码 防止原页面失效,代码 粘贴在下面 <!DOCTYPE html> <html lang=" ...

  5. python 中自带的堆模块heapq

    import heapq my_heap = [] #使用列表保存数据 #网列表中插入数据,优先级使用插入的内容来表示,就是一个比较大小的操作,越大优先级越高 heapq.heappush(my_he ...

  6. 用long类型让我出了次生产事故,写代码还是要小心点

    昨天发现线上试跑期的一个程序挂了,平时都跑的好好的,查了下日志是因为昨天运营跑了一家美妆top级淘品牌店,会员量近千万,一下子就把128G的内存给爆了,当时并行跑了二个任务,没辙先速写一段代码限流,后 ...

  7. Spark RDD----pyspark第四次作业

    1.pyspark交互式编程 查看群里发的“data01.txt”数据集,该数据集包含了某大学计算机系的成绩,数据格式如下所示: Tom,DataBase,80 Tom,Algorithm,50 To ...

  8. 前端面试题之HTML和css-很实用的知识点

    display: none; 与 visibility: hidden; 的区别 相同: 它们都能让元素不可见 区别: display:none;会让元素完全从渲染树中消失,渲染的时候不占据任何空间: ...

  9. 作业十一——LL(1)文法的判断,递归下降分析程序

    作业十一——LL(1)文法的判断,递归下降分析程序 判断是否为LL(1)文法 选取有多个产生式的求select,只有一条产生式的无需求select 同一个非终结符之间求交集,全部判断为空后则为LL(1 ...

  10. FastJson踩坑:@JsonField在反序列化时失效

    问题描述 一个对象(某个字段为枚举类型,为了不采用默认的序列化过程,用@JSONField指定了序列化器和反序列器,过程见旧博文),将其放到JSONArray中再序列化JSONArray对象,用得到的 ...