题意: 给一个圆和一个多边形,多边形点可能按顺时针给出,也可能按逆时针给出,先判断多边形是否为凸包,再判断圆是否在凸包内。

解法: 先判是否为凸包,沿着i=0~n,先得出初始方向dir,dir=1为逆时针,dir=-1为顺时针,然后如果后面有两个相邻的边叉积后得出旋转方向为nowdir,如果dir*nowdir < 0,说明方向逆转了,即出现了凹点,说明不是凸多边形。

然后判圆是否在多边形内: 先判圆心是否在多边形内,用环顾法,然后如果在之内,则依次判断圆心与每条凸包边的距离与半径的距离,如果所有的dis都大于等于R,说明圆在凸包内。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define pi acos(-1.0)
#define eps 1e-8
using namespace std; struct Point{
double x,y;
Point(double x=, double y=):x(x),y(y) {}
void input() { scanf("%lf%lf",&x,&y); }
};
typedef Point Vector;
struct Circle{
Point c;
double r;
Circle(){}
Circle(Point c,double r):c(c),r(r) {}
Point point(double a) { return Point(c.x + cos(a)*r, c.y + sin(a)*r); }
void input() { scanf("%lf%lf%lf",&c.x,&c.y,&r); }
};
int dcmp(double x) {
if(x < -eps) return -;
if(x > eps) return ;
return ;
}
template <class T> T sqr(T x) { return x * x;}
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; }
bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; }
bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == && dcmp(a.y-b.y) == ; }
double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } double DistanceToSeg(Point P, Point A, Point B)
{
if(A == B) return Length(P-A);
Vector v1 = B-A, v2 = P-A, v3 = P-B;
if(dcmp(Dot(v1, v2)) < ) return Length(v2);
if(dcmp(Dot(v1, v3)) > ) return Length(v3);
return fabs(Cross(v1, v2)) / Length(v1);
}
//点是否在多边形内部
int CheckPointInPolygon(Point A,Point* p,int n){
double TotalAngle = 0.0;
for(int i=;i<n;i++) {
if(dcmp(Cross(p[i]-A,p[(i+)%n]-A)) >= ) TotalAngle += Angle(p[i]-A,p[(i+)%n]-A);
else TotalAngle -= Angle(p[i]-A,p[(i+)%n]-A);
}
if(dcmp(TotalAngle) == ) return ; //外部
else if(dcmp(fabs(TotalAngle)-*pi) == ) return ; //完全内部
else if(dcmp(fabs(TotalAngle)-pi) == ) return ; //边界上
else return ; //多边形顶点
}
//判断未知时针方向的多边形是否是凸包
bool CheckConvexHull(Point* p,int n){
int dir = ; //旋转方向
for(int i=;i<n;i++) {
int nowdir = dcmp(Cross(p[(i+)%n]-p[i],p[(i+)%n]-p[i]));
if(!dir) dir = nowdir;
if(dir*nowdir < ) return false; //非凸包
}
return true;
} Point p[]; int main()
{
int n,i,j;
Circle Peg;
while(scanf("%d",&n)!=EOF && n >= )
{
scanf("%lf",&Peg.r); Peg.c.input();
for(i=;i<n;i++) p[i].input();
if(!CheckConvexHull(p,n)) { puts("HOLE IS ILL-FORMED"); continue; }
if(CheckPointInPolygon(Peg.c,p,n))
{
for(i=;i<n;i++)
{
double dis = DistanceToSeg(Peg.c,p[i],p[(i+)%n]);
if(dcmp(dis-Peg.r) < ) break;
}
if(i == n) { puts("PEG WILL FIT"); continue; }
}
puts("PEG WILL NOT FIT");
}
return ;
}

参考文章: http://blog.csdn.net/lyy289065406/article/details/6648606

射线法:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define eps 1e-8
using namespace std;
struct Point{
double x,y;
Point(double x=, double y=):x(x),y(y) {}
void input() { scanf("%lf%lf",&x,&y); }
};
typedef Point Vector;
struct Circle{
Point c;
double r;
Circle(){}
Circle(Point c,double r):c(c),r(r) {}
Point point(double a) { return Point(c.x + cos(a)*r, c.y + sin(a)*r); }
void input() { scanf("%lf%lf%lf",&c.x,&c.y,&r); }
};
int dcmp(double x) {
if(x < -eps) return -;
if(x > eps) return ;
return ;
}
template <class T> T sqr(T x) { return x * x;}
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; }
bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; }
bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == && dcmp(a.y-b.y) == ; }
double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
Vector VectorUnit(Vector x){ return x / Length(x);}
Vector Normal(Vector x) { return Point(-x.y, x.x) / Length(x);}
double angle(Vector v) { return atan2(v.y, v.x); } bool OnSegment(Point P, Point A, Point B) {
return dcmp(Cross(A-P,B-P)) == && dcmp(Dot(A-P,B-P)) <= ;
}
double DistanceToSeg(Point P, Point A, Point B)
{
if(A == B) return Length(P-A);
Vector v1 = B-A, v2 = P-A, v3 = P-B;
if(dcmp(Dot(v1, v2)) < ) return Length(v2);
if(dcmp(Dot(v1, v3)) > ) return Length(v3);
return fabs(Cross(v1, v2)) / Length(v1);
}
//判断未知时针方向的多边形是否是凸包
bool CheckConvexHull(Point* p,int n){
int dir = ; //旋转方向
for(int i=;i<n;i++) {
int nowdir = dcmp(Cross(p[(i+)%n]-p[i],p[(i+)%n]-p[i]));
if(!dir) dir = nowdir;
if(dir*nowdir < ) return false; //非凸包
}
return true;
}
int Ray_PointInPolygon(Point A,Point* p,int n) {
int wn = ;
for(int i=;i<n;i++) {
//if(OnSegment(A,p[i],p[(i+1)%n])) return -1; //边界
int k = dcmp(Cross(p[(i+)%n]-p[i], A-p[i]));
int d1 = dcmp(p[i].y-A.y);
int d2 = dcmp(p[(i+)%n].y-A.y);
if(k > && d1 <= && d2 > ) wn++;
if(k < && d2 <= && d1 > ) wn--;
}
if(wn) return ; //内部
return ; //外部
} Point p[]; int main()
{
int n,i,j;
Circle Peg;
while(scanf("%d",&n)!=EOF && n >= )
{
scanf("%lf",&Peg.r); Peg.c.input();
for(i=;i<n;i++) p[i].input();
if(!CheckConvexHull(p,n)) { puts("HOLE IS ILL-FORMED"); continue; }
if(Ray_PointInPolygon(Peg.c,p,n))
{
for(i=;i<n;i++)
{
double dis = DistanceToSeg(Peg.c,p[i],p[(i+)%n]);
if(dcmp(dis-Peg.r) < ) break;
}
if(i == n) { puts("PEG WILL FIT"); continue; }
}
puts("PEG WILL NOT FIT");
}
return ;
}

最新文章

  1. json转类
  2. Android菜鸟成长记14 -- AsnyTask
  3. 事故记录:php-cgi进程过多导致系统资源耗尽
  4. 一键自动发布ipa(更新svn,拷贝资源,压缩资源,加密图片资源,加密数据文件,加密lua脚本,编译代码,ipa签名,上传ftp)
  5. JNI开发流程-JNI/NDK【转】
  6. JS浮点数运算BUG破法
  7. poj3683 Priest John's Busiest Day
  8. asp:手机扫描二维码跳转手机版
  9. 【pano2vr】网页Flash中简单实现炫酷的3D模型制作
  10. MySQL安装-windows安装
  11. Activex、OLE、COM、OCX、DLL之间区别、联系[转]
  12. EF5.0区别于EF4.0的crud区别
  13. display position 和float的作用和关系
  14. centos6.9上mongdb安装
  15. FunDA(16)- 示范:整合并行运算 - total parallelism solution
  16. Mysql 索引原理及优化
  17. 如何调试bash脚本
  18. CefSharp禁止弹出新窗体,在同一窗口打开链接,并且支持带type=&quot;POST&quot; target=&quot;_blank&quot;的链接
  19. 遇到问题---java---安装新版本jdk后Failed reading value of registry key
  20. Android 进程间通信——Service、Messenger

热门文章

  1. ionic rang在弹出modal中不可拖拽的问题
  2. C C++ TDD单元测试非常好的书
  3. 转使用chrome命令行:disable-web-security 实现浏览器跨域
  4. 根据字符串生成类---类的类型.self---根据字符串创建控制器对象
  5. OC NSSet
  6. iOS 学习 - 10下载(1) NSURLConnection 篇
  7. eclipse maven 插件 安装 和 配置
  8. Git 的 .gitignore 配置
  9. 一些性能查询的SQL 备忘
  10. MVC6的内置ActionResult类型