POJ 1279 Art Gallery 半平面交/多边形求核
http://poj.org/problem?id=1279
顺时针给你一个多边形...求能看到所有点的面积...用半平面对所有边取交即可,模版题
这里的半平面交是O(n^2)的算法...比较逗比...暴力对每条线段做半平面交...要注意的地方写在注释里了...顺序写反了卡了我好久
/********************* Template ************************/
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std; #define EPS 1e-8
#define MAXN 10005
#define MOD (int)1e9+7
#define PI acos(-1.0)
#define INF ((1LL)<<50)
#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max3(a,b,c) (max(max(a,b),c))
#define min3(a,b,c) (min(min(a,b),c))
#define BUG cout<<"BUG! "<<endl
#define LLINE cout<<"------------------"<<endl
#define L(t) (t << 1)
#define R(t) (t << 1 | 1)
#define Mid(a,b) ((a + b) >> 1)
#define lowbit(a) (a & -a)
#define FIN freopen("in.txt","r",stdin)
#pragma comment (linker,"/STACK:102400000,102400000") // typedef long long LL;
// typedef unsigned long long ULL;
// typedef __int64 LL;
// typedef unisigned __int64 ULL;
// int gcd(int a,int b){ return b?gcd(b,a%b):a; }
// int lcm(int a,int b){ return a*b/gcd(a,b); } /********************* F ************************/
struct POINT{
double x,y;
POINT(double _x = , double _y = ):x(_x),y(_y){}
}p[MAXN],q[MAXN],t[MAXN];
int n;
struct LINE{
double a,b,c;
POINT A,B;
LINE(POINT _a, POINT _b):A(_a),B(_b){
a=B.y-A.y;
b=A.x-B.x;
c=B.x*A.y-A.x*B.y;
}
};
double multiply(POINT sp,POINT ep,POINT op){ //叉积 左+ 右-
return (sp.x-op.x) * (ep.y-op.y) - (ep.x-op.x) * (sp.y-op.y);
}
POINT Intersection(LINE a,LINE b){ //直线交点
double u = fabs(b.A.x * a.a + b.A.y * a.b + a.c);
double v = fabs(b.B.x * a.a + b.B.y * a.b + a.c);
POINT t;
t.x = (b.A.x * v + b.B.x * u) / (u + v);
t.y = (b.A.y * v + b.B.y * u) / (u + v);
return t;
}
double Triangle_area(POINT a,POINT b,POINT c){ //求三角形面积(带符号)
return multiply(a,b,c)/;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("outm.txt","w",stdout);
int ct = ;
int T;
cin>>T;
while(T--){
cin>>n;
for(int i = ; i < n ; i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
//暴力对每一个向量作半平面交 ...即将右侧的点和与其他直线的交点加入集合
for(int i = ; i < n ; i++) q[i] = p[i];
int cnt = n;
for(int i = ; i < n ; i++){
int c = ;
for(int j = ; j < cnt ; j++){
//点在右侧
if(multiply(p[i],p[(i+)%n],q[j]) <= EPS) {
t[c++] = q[j];
}else { //点在左侧,但是前后线段和该直线有交点
//这个顺序不要写反,否则不是顺时针会WA
if(multiply(p[i],p[(i+)%n],q[(j-+cnt)%cnt]) < -EPS){
t[c++] = Intersection(LINE(p[i],p[(i+)%n]) , LINE(q[j],q[(j-+cnt)%cnt]));
}
if(multiply(p[i],p[(i+)%n],q[(j+)%cnt]) < -EPS){
t[c++] = Intersection(LINE(p[i],p[(i+)%n]) , LINE(q[j],q[(j+)%cnt]));
}
}
}
for(int j = ; j < c ; j++) q[j] = t[j];
cnt = c;
}
double area = ;
for(int i = ; i < cnt ; i++){
area += Triangle_area(POINT(,),q[i],q[(i+)%cnt]);
}
area = fabs(area);
printf("%.2lf\n",area);
}
return ;
}
POJ 1279 Art Gallery 半平面交/多边形求核的更多相关文章
- POJ 1279 Art Gallery 半平面交 多边形的核
题意:求多边形的核的面积 套模板即可 #include <iostream> #include <cstdio> #include <cmath> #define ...
- POJ 1279 Art Gallery 半平面交求多边形核
第一道半平面交,只会写N^2. 将每条边化作一个不等式,ax+by+c>0,所以要固定顺序,方便求解. 半平面交其实就是对一系列的不等式组进行求解可行解. 如果某点在直线右侧,说明那个点在区域内 ...
- POJ 1279 Art Gallery(半平面交)
题目链接 回忆了一下,半平面交,整理了一下模版. #include <cstdio> #include <cstring> #include <string> #i ...
- 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 (Half Plane Intersection)
1279 -- Art Gallery 还是半平面交的问题,要求求出多边形中可以观察到多边形所有边的位置区域的面积.其实就是把每一条边看作有向直线然后套用半平面交.这题在输入的时候应该用多边形的有向面 ...
- POJ 1279 Art Gallery【半平面交】(求多边形的核)(模板题)
<题目链接> 题目大意: 按顺时针顺序给出一个N边形,求N边形的核的面积. (多边形的核:它是平面简单多边形的核是该多边形内部的一个点集该点集中任意一点与多边形边界上一点的连线都处于这个多 ...
- POJ 1279 Art Gallery(半平面交求多边形核的面积)
题目链接 题意 : 求一个多边形的核的面积. 思路 : 半平面交求多边形的核,然后在求面积即可. #include <stdio.h> #include <string.h> ...
- [POJ]1279: Art Gallery
题目大意:有一个N边形展馆,问展馆内有多少地方可以看到所有墙壁.(N<=1500) 思路:模板题,半平面交求出多边形的核后计算核的面积. #include<cstdio> #incl ...
随机推荐
- 【C#Windows 服务】 《二》INI配置文件
一.工具: VS2015+NET Framework4.5. 二.操作: 1.创建INIHelp帮助类 2.丰富帮助类操作 3.windows实例调用 三.代码: 1.INI帮助类: 1 2 3 4 ...
- SSD-tensorflow-3 重新训练模型(vgg16)
一.修改pascalvoc_2007.py 生成自己的tfrecord文件后,修改训练数据shape——打开datasets文件夹中的pascalvoc_2007.py文件,根据自己训练数据修改:NU ...
- type与isinstance使用区别
Python中,type与isinstance都可以用来判断变量的类型,但是type具有一定的适用性,用它来判断变量并不总是能够获取到正确的值. Python在定义变量的时候不用指明具体的的类型,解释 ...
- 操作系统——第五章 输入输出(I/O)管理
这就是SDT表和DCT表
- [NOIP2010提高组]引水入城
题目:洛谷P1514.Vijos P1777.codevs1066. 题目大意:有一个$n×m$的矩阵,每个点都有一个高度,可以在第一行的任意点建立蓄水厂.现在要把水输到最后一行的所有点上,规定水只能 ...
- [洛谷P1343]地震逃生
题目大意:有n个点m条单向边,每条边有一个容量.现有x人要分批从1走到n,问每批最多能走多少人,分几批运完(或输出无法运完). 解题思路:一看就是网络流的题目.每批最多能走多少人,即最大流.分几批运完 ...
- unity 天空盒有缝隙的解决方案
修改天空盒图片的属性:advanced->wrap mode->clamp
- Log4j2打印一行日志时返回本行日志的字符串
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.core.impl.Log4jLogEvent; impo ...
- Java NIO和IO的主要差别
我应该何时使用IO,何时使用NIO呢?在本文中,我会尽量清晰地解析Java NIO和IO的差异.它们的使用场景.以及它们怎样影响您的代码设计. Java NIO和IO的主要差别 下表总结了Java N ...
- HDU2452 Navy maneuvers 记忆化搜索
这题目意思能忍?读了半年,乱七八糟的 记忆化搜索 拖拖的,dp[i][0]代表以获得最小值为目标的船以i为起点.dp[i][1]代表以获得最大值为目标的船以i为起点.接下来暴力枚举入度为0的点为起点, ...