poj 1279 Art Gallery (Half Plane Intersection)
还是半平面交的问题,要求求出多边形中可以观察到多边形所有边的位置区域的面积。其实就是把每一条边看作有向直线然后套用半平面交。这题在输入的时候应该用多边形的有向面积来判断输入的顺序是顺时针的还是逆时针的。
对于半平面交问题,要注意最后半平面返回的是多少个点。对于小于3个点的情况应该直接返回结果,避免计算过程中产生错误。
代码如下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath> using namespace std; struct Point {
double x, y;
Point() {}
Point(double x, double y) : x(x), y(y) {}
} ;
template<class T> T sqr(T x) { return x * x;}
typedef Point Vec;
Vec operator + (Vec a, Vec b) { return Vec(a.x + b.x, a.y + b.y);}
Vec operator - (Vec a, Vec b) { return Vec(a.x - b.x, a.y - b.y);}
Vec operator * (Vec a, double p) { return Vec(a.x * p, a.y * p);}
Vec operator / (Vec a, double p) { return Vec(a.x / p, a.y / p);} const double EPS = 1e-;
const double PI = acos(-1.0);
inline int sgn(double x) { return (x > EPS) - (x < -EPS);} inline double dotDet(Vec a, Vec b) { return a.x * b.x + a.y * b.y;}
inline double crossDet(Vec a, Vec b) { return a.x * b.y - a.y * b.x;}
inline double dotDet(Point o, Point a, Point b) { return dotDet(a - o, b - o);}
inline double crossDet(Point o, Point a, Point b) { return crossDet(a - o, b - o);}
inline double vecLen(Vec x) { return sqrt(dotDet(x, x));}
inline double toRad(double deg) { return deg / 180.0 * PI;}
inline double angle(Vec v) { return atan2(v.y, v.x);}
inline Vec vecUnit(Vec x) { return x / vecLen(x);}
inline Vec normal(Vec x) { return Vec(-x.y, x.x) / vecLen(x);} const int N = ;
struct DLine {
Point p;
Vec v;
double ang;
DLine() {}
DLine(Point p, Vec v) : p(p), v(v) { ang = atan2(v.y, v.x);}
bool operator < (DLine L) const { return ang < L.ang;}
} dl[N];
Point pt[N]; inline bool onLeft(DLine L, Point p) { return crossDet(L.v, p - L.p) > ;}
Point dLineIntersect(DLine a, DLine b) {
Vec u = a.p - b.p;
double t = crossDet(b.v, u) / crossDet(a.v, b.v);
return a.p + a.v * t;
} struct Poly {
vector<Point> pt;
Poly() { pt.clear();}
~Poly() {}
Poly(vector<Point> &pt) : pt(pt) {}
Point operator [] (int x) { return pt[x];}
int size() { return pt.size();}
double area() {
double ret = 0.0;
int sz = pt.size();
pt.push_back(pt[]);
for (int i = ; i <= sz; i++) ret += crossDet(pt[i], pt[i - ]);
pt.pop_back();
return fabs(ret / 2.0);
}
} ; Poly halfPlane(DLine *L, int n) {
Poly ret = Poly();
sort(L, L + n);
int fi, la;
Point *p = new Point[n];
DLine *q = new DLine[n];
q[fi = la = ] = L[];
for (int i = ; i < n; i++) {
while (fi < la && !onLeft(L[i], p[la - ])) la--;
while (fi < la && !onLeft(L[i], p[fi])) fi++;
q[++la] = L[i];
if (sgn(crossDet(q[la].v, q[la - ].v)) == ) {
la--;
if (onLeft(q[la], L[i].p)) q[la] = L[i];
}
if (fi < la) p[la - ] = dLineIntersect(q[la - ], q[la]);
}
while (fi < la && !onLeft(q[fi], p[la - ])) la--;
if (la < fi) return ret;
p[la] = dLineIntersect(q[la], q[fi]);
for (int i = fi; i <= la; i++) ret.pt.push_back(p[i]);
return ret;
} bool isClockwise(Point *pt, int n) {
double sum = 0.0;
pt[n] = pt[];
Point O = Point(0.0, 0.0);
for (int i = ; i < n; i++) {
sum += crossDet(O, pt[i], pt[i + ]);
}
return sum < ;
} int main() {
int T, n;
cin >> T;
while (T-- && cin >> n) {
for (int i = ; i < n; i++) cin >> pt[i].x >> pt[i].y;
pt[n] = pt[];
if (isClockwise(pt, n)) for (int i = ; i < n; i++) dl[i] = DLine(pt[i + ], pt[i] - pt[i + ]);
else for (int i = ; i < n; i++) dl[i] = DLine(pt[i], pt[i + ] - pt[i]);
Poly tmp = halfPlane(dl, n);
if (tmp.size() >= ) printf("%.2f\n", tmp.area());
else puts("0.00");
}
return ;
}
——written by Lyon
poj 1279 Art Gallery (Half Plane Intersection)的更多相关文章
- poj 1279 Art Gallery - 求多边形核的面积
/* poj 1279 Art Gallery - 求多边形核的面积 */ #include<stdio.h> #include<math.h> #include <al ...
- poj 1279 -- Art Gallery (半平面交)
鏈接:http://poj.org/problem?id=1279 Art Gallery Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- POJ 1279 Art Gallery 半平面交/多边形求核
http://poj.org/problem?id=1279 顺时针给你一个多边形...求能看到所有点的面积...用半平面对所有边取交即可,模版题 这里的半平面交是O(n^2)的算法...比较逗比.. ...
- POJ 1279 Art Gallery 半平面交求多边形核
第一道半平面交,只会写N^2. 将每条边化作一个不等式,ax+by+c>0,所以要固定顺序,方便求解. 半平面交其实就是对一系列的不等式组进行求解可行解. 如果某点在直线右侧,说明那个点在区域内 ...
- POJ 1279 Art Gallery 半平面交 多边形的核
题意:求多边形的核的面积 套模板即可 #include <iostream> #include <cstdio> #include <cmath> #define ...
- POJ 1279 Art Gallery(半平面交)
题目链接 回忆了一下,半平面交,整理了一下模版. #include <cstdio> #include <cstring> #include <string> #i ...
- POJ 1279 Art Gallery(半平面交求多边形核的面积)
题目链接 题意 : 求一个多边形的核的面积. 思路 : 半平面交求多边形的核,然后在求面积即可. #include <stdio.h> #include <string.h> ...
- [POJ]1279: Art Gallery
题目大意:有一个N边形展馆,问展馆内有多少地方可以看到所有墙壁.(N<=1500) 思路:模板题,半平面交求出多边形的核后计算核的面积. #include<cstdio> #incl ...
- POJ 1279 Art Gallery【半平面交】(求多边形的核)(模板题)
<题目链接> 题目大意: 按顺时针顺序给出一个N边形,求N边形的核的面积. (多边形的核:它是平面简单多边形的核是该多边形内部的一个点集该点集中任意一点与多边形边界上一点的连线都处于这个多 ...
随机推荐
- Leetcode96.Unique Binary Search Trees不同的二叉搜索树
给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 示例: 输入: 3 输出: 5 解释: 给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 假设n个节点存在二叉排序树的 ...
- LinqToExcel 简洁与优美开源库
转载:https://www.cnblogs.com/codefish/archive/2013/04/08/3009098.html 正在做项目,同事问道有啥简单的方法读取excel到DataTab ...
- Oracle启动和禁用约束及删除违反约束的记录
一.禁用约束 alter table table_name disable novalidate constraint constraint_name 二.批量导入数据 三.在开启约束之前一定要检查违 ...
- JZOJ 5459 购物
Description X 城的商场中,有着琳琅满目的各种商品.一日,小X 带着小Y 前来购物,小Y 一共看中了n件商品,每一件商品价格为Pi.小X 现在手中共有m个单位的现金,以及k 张优惠券.小X ...
- 对List<Map<String, Object>>集合排序
private void mySort(List<Map<String, Object>> list) { //list为待排序的集合,按SEQ字段排序 Comparator& ...
- Floyd算法模板--详解
对于无权的图来说: 若从一顶点到另一顶点存在着一条路径,则称该路径长度为该路径上所经过的边的数目,它等于该路径上的顶点数减1. 由于从一顶点到另一顶点可能存在着多条路径,每条路径上所经过的边数可能不同 ...
- python-null
很早之前,遇到过一个面试官问的python中有没有null,当时有点紧张,想成了None,就脱口而出是有的.今天遇到了none问题,所以就正好说一下. python中是没有NULL的. Python中 ...
- Nginx教程(三) Nginx日志管理 (转)
Nginx教程(三) Nginx日志管理 1 日志管理 1.1 Nginx日志描述 通过访问日志,你可以得到用户地域来源.跳转来源.使用终端.某个URL访问量等相关信息:通过错误日志,你可以得到系统某 ...
- Python学习之路11☞异常处理
一 错误和异常 part1:程序中难免出现错误,而错误分成两种 1.语法错误(这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正) #语法错误示范一 if #语法错误示范二 de ...
- SqlSugar 笔记
分组: 日期分组一: var result = await temp .GroupBy("date_format(Day,'%Y-%m')") .Select(s => ne ...