#include <stdlib.h>
#include <math.h>
#include <iostream> #define MAXN 1000
#define offset 10000
#define eps 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps)
#define _sign(x) ((x)>eps?1:((x)<-eps?2:0)) struct point{ double x, y; };
struct line{ point a, b; }; double xmult(point p1, point p2, point p0){
return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x)*(p1.y - p0.y);
} //判定凸多边形,顶点按顺时针或逆时针给出,允许相邻边共线
bool is_convex(int n, point* p){
int i, s[3] = { 1, 1, 1 };
for (i = 0; i < n && s[1] | s[2]; i++)
s[_sign(xmult(p[(i + 1)%n], p[(i + 2)%n], p[i]))] = 0;
return s[1] | s[2];
} //判定凸多边形,顶点按顺时针或逆时针给出,不允许相邻边共线
bool is_convex_v2(int n, point* p){
int i, s[3] = { 1, 1, 1 };
for (i = 0; i < n && s[0] && s[1] | s[2]; i++)
s[_sign(xmult(p[(i + 1)%n], p[(i + 2)%n], p[i]))] = 0;
return s[0] && s[1] | s[2];
} //判点在凸多边形内或多边形边上,顶点按顺时针或逆时针给出
bool inside_convex(point q, int n, point* p){
int i, s[3] = { 1, 1, 1 };
for (i = 0; i < n && s[1] | s[2]; i++)
s[_sign(xmult(p[(i + 1)%n], q, p[i]))] = 0;
return s[1] | s[2];
} //判点在凸多边形内,顶点按顺时针或逆时针给出,在多边形边上返回0
bool inside_convex_v2(point q, int n, point* p){
int i, s[3] = { 1, 1, 1 };
for (i = 0; i < n && s[0] && s[1] | s[2]; i++)
s[_sign(xmult(p[(i + 1)%n], q, p[i]))] = 0;
return s[0] && s[1] | s[2];
} //判点在任意多边形内,顶点按顺时针或逆时针给出
//on_edge表示点在多边形边上时的返回值,offset为多边形坐标上限
bool inside_polygon(point q, int n, point* p, int on_edge = 1){
point q2;
int i = 0, count;
while (i < n)
for (count = i = 0, q2.x = rand() + offset, q2.y = rand() + offset; i < n; i++)
if (zero(xmult(q, p[i], p[(i + 1)%n])) && (p[i].x - q.x)*(p[(i + 1)%n].x - q.x) < eps && (p[i].y - q.y)*(p[(i + 1)%n].y - q.y) < eps)
return on_edge;
else if (zero(xmult(q, q2, p[i])))
break;
else if (xmult(q, p[i], q2)*xmult(q, p[(i + 1)%n], q2) < -eps && xmult(p[i], q, p[(i + 1)%n])*xmult(p[i], q2, p[(i + 1)%n]) < -eps)
count++;
return count & 1;
} inline bool opposite_side(point p1, point p2, point l1, point l2){
return xmult(l1, p1, l2)*xmult(l1, p2, l2) < -eps;
} inline bool dot_online_in(point p, point l1, point l2){
return zero(xmult(p, l1, l2)) && (l1.x - p.x)*(l2.x - p.x) < eps && (l1.y - p.y)*(l2.y - p.y) < eps;
} //判线段在任意多边形内,顶点按顺时针或逆时针给出,与边界相交返回1
bool inside_polygon(point l1, point l2, int n, point* p){
point t[MAXN], tt;
int i, j, k = 0;
if (!inside_polygon(l1, n, p) || !inside_polygon(l2, n, p))
return 0;
for (i = 0; i < n; i++)
if (opposite_side(l1, l2, p[i], p[(i + 1)%n]) && opposite_side(p[i], p[(i + 1)%n], l1, l2))
return 0;
else if (dot_online_in(l1, p[i], p[(i + 1)%n]))
t[k++] = l1;
else if (dot_online_in(l2, p[i], p[(i + 1)%n]))
t[k++] = l2;
else if (dot_online_in(p[i], l1, l2))
t[k++] = p[i];
for (i = 0; i < k; i++)
for (j = i + 1; j < k; j++){
tt.x = (t[i].x + t[j].x) / 2;
tt.y = (t[i].y + t[j].y) / 2;
if (!inside_polygon(tt, n, p))
return 0;
}
return 1;
} double distance(point p1, point p2)
{
return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
}
double disptoline(point p, point l1, point l2){
return fabs(xmult(p, l1, l2)) / distance(l1, l2);
}
int intersect_seg_circle(point c, double r, point l1, point l2){
double t1 = distance(c, l1) - r, t2 = distance(c, l2) - r;
point t = c;
if (t1<eps || t2<eps)
return t1>-eps || t2>-eps;
t.x += l1.y - l2.y;
t.y += l2.x - l1.x;
return xmult(l1, c, t)*xmult(l2, c, t) < eps && disptoline(c, l1, l2) - r < eps;
}
//判断圆是否在多边形内
bool circle_in_polygen(double r, point o, int n, point *p)
{
for (int i = 1; i < n; i++)
{
bool flag = intersect_seg_circle(o, r, p[i - 1], p[i]);
if (flag) { return false; break; }
}
return true;
} point intersection(line u, line v){
point ret = u.a;
double t = ((u.a.x - v.a.x)*(v.a.y - v.b.y) - (u.a.y - v.a.y)*(v.a.x - v.b.x))
/ ((u.a.x - u.b.x)*(v.a.y - v.b.y) - (u.a.y - u.b.y)*(v.a.x - v.b.x));
ret.x += (u.b.x - u.a.x)*t;
ret.y += (u.b.y - u.a.y)*t;
return ret;
} point barycenter(point a, point b, point c){
line u, v;
u.a.x = (a.x + b.x) / 2;
u.a.y = (a.y + b.y) / 2;
u.b = c;
v.a.x = (a.x + c.x) / 2;
v.a.y = (a.y + c.y) / 2;
v.b = b;
return intersection(u, v);
} //多边形重心
point barycenter(int n, point* p){
point ret, t;
double t1 = 0, t2;
int i;
ret.x = ret.y = 0;
for (i = 1; i<n - 1; i++)
if (fabs(t2 = xmult(p[0], p[i], p[i + 1]))>eps){
t = barycenter(p[0], p[i], p[i + 1]);
ret.x += t.x*t2;
ret.y += t.y*t2;
t1 += t2;
}
if (fabs(t1) > eps)
ret.x /= t1, ret.y /= t1;
return ret;
}

Computational Geometry Template_Polygon的更多相关文章

  1. Computational Geometry Template

    顿时觉得神清气爽!! #include <iostream> #include <math.h> #define eps 1e-8 #define zero(x) (((x)& ...

  2. Computational Geometry

    矩形重叠 看过某司一道笔试题:给\(n\)个矩形左下和右上坐标(不能斜放),求重叠最多处矩形个数. 这道题本身不难:可以遍历所有矩形边界组成的点,计算该点被多少矩形包围,从而选出最大值. 由此引申出一 ...

  3. 2D Circular Geometry Kernel ( Geometry Kernels) CGAL 4.13 -User Manual

    1 Introduction The goal of the circular kernel is to offer to the user a large set of functionalitie ...

  4. 2D and 3D Linear Geometry Kernel ( Geometry Kernels) CGAL 4.13 -User Manual

    1 Introduction CGAL, the Computational Geometry Algorithms Library, is written in C++ and consists o ...

  5. OpenSUSE下编译安装OpenFoam

    在不是Ubuntu系统下安装OpenFoam,需要采用编译安装的方式.以下以OpenSuSE为例进行编译安装. 1 软件包准备 需要下载两个程序包: OpenFOAM-4.x-version-4.1. ...

  6. Programming Contest Problem Types

        Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...

  7. 中国计算机学会CCF推荐国际学术会议

    中国计算机学会推荐国际学术会议 (计算机系统与高性能计算) 一.A类 序号 会议简称 会议全称 出版社 网址 1 ASPLOS Architectural Support for Programmin ...

  8. Visulalize Boost Voronoi in OpenSceneGraph

    Visulalize Boost Voronoi in OpenSceneGraph eryar@163.com Abstract. One of the important features of ...

  9. Visulalization Voronoi in OpenSceneGraph

    Visulalization Voronoi in OpenSceneGraph eryar@163.com Abstract. In mathematics a Voronoi diagram is ...

随机推荐

  1. 修改Android系统字号(二)

    /*********************************************************************** * 修改Android系统字号(二) * 说明: * ...

  2. SAS9.4 安装注意事项

    OS:Windows Server 2012 R2 SAS:9.4 TS1M1 SAS9.4的安装需要注意的地方: 一. 注意开启Shortname(装了N遍,最容易忘记的地方) SAS安装使用Sho ...

  3. Heritrix源码分析(十四) 如何让Heritrix不间断的抓取(转)

    欢迎加入Heritrix群(QQ):109148319,10447185 , Lucene/Solr群(QQ) :  118972724 本博客已迁移到本人独立博客: http://www.yun5u ...

  4. php 二维数组的排序

    写这是之前一直二维数组排名困扰.自己写的好复杂. 正题: array_mutisort 官方帮助文档 <?php// 取得列的列表foreach ($data as $key => $ro ...

  5. 【转】iOS 开发之协议protocal-代理传值delegate

    原文网址:http://www.cnblogs.com/wzrong/p/3201938.html 刚开始做iOS开发的时候,对 protocol.delegate 的理解一直都是晕晕乎乎一知半解的状 ...

  6. Android WebView中那些不得不解决的坑~~

    前面那张hybrid开发心得 有人问 怎么解决不用onJsPrompt 来回调js函数的问题.其实很简单,就是在在你的jscalljava回调函数内 另外开个线程去load js代码即可: wb.po ...

  7. XSS 前端防火墙(5): 整装待发

    到目前为止,我们把能用前端脚本防御 XSS 的方案都列举了一遍. 尽管看起来似乎很复杂累赘,不过那些是理论探讨而已,在实际中未必要都实现.我们的目标只是为了预警,能发现问题就行,并非要做到滴水不漏的程 ...

  8. 插件五之滚动条jquery.slimscroll.js

    前言 slimscroll.js用于模拟传统的浏览器滚动条(竖向),原理为原内容内置于一个仅可视区域显示层,使用2个div层用于模拟滚动条和滚动条背景轨道监听滚动条div高度变化来控制内容层位置(猜测 ...

  9. Selenium2Library系列 keywords 之 _SelectElementKeywords 之 unselect_from_list(self, locator, *items)

    def unselect_from_list(self, locator, *items): """Unselects given values from select ...

  10. Select模型及tcp select模型

    参考:http://m.blog.csdn.net/article/details?id=51420015 一.套接字模式 套接字模式简单的决定了操作套接字时,Winsock函数是如何运转的.Wins ...