[计算几何笔记3]最小圆覆盖
2016-02-17 11:49
375 查看
随机增量算法
正确性在于半径的是单调递增的
bzoj2823 点<=10^6
感觉解圆心好困难= =
所以我是要背过么。。
蓝书P268
bzoj2823:
正确性在于半径的是单调递增的
void min_cover_circle(Point *p,int n,Point &c,double &r){ //c为圆心,r为半径 random_shuffle(p,p+n); // c=p[0]; r=0; for(int i=1;i<n;i++) { if(dis(p[i],c)>r+eps) //第一个点 { c=p[i]; r=0; for(int j=0;j<i;j++) if(dis(p[j],c)>r+eps) //第二个点 { c.x=(p[i].x+p[j].x)/2; c.y=(p[i].y+p[j].y)/2; r=dis(p[j],c); for(int k=0;k<j;k++) if(dis(p[k],c)>r+eps) //第三个点 {//求外接圆圆心,三点必不共线 c=circumcenter(p[i],p[j],p[k]); r=dis(p[i],c); } } } } }
bzoj2823 点<=10^6
感觉解圆心好困难= =
所以我是要背过么。。
蓝书P268
bzoj2823:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #define maxn 1000010 using namespace std; //-----------------------------------------------------------------// int n; const double eps = 1e-8; int Filter(double x){ return x > eps ? 1 : (x < -eps ? -1 : 0); } struct Point{ double x, y; Point(double x = 0, double y = 0):x(x), y(y){} void read(){ scanf("%lf%lf", &x, &y); } }b[maxn]; Point operator+(const Point& a, const Point& b){ return Point(a.x + b.x, a.y + b.y); } Point operator-(const Point& a, const Point& b){ return Point(a.x - b.x, a.y - b.y); } Point operator*(const double& t, const Point& a){ return Point(t * a.x, t * a.y); } Point operator/(const Point& a, const double& t){ return Point(a.x / t, a.y / t); } double Cross(const Point& a, const Point& b){ return a.x * b.y - a.y * b.x; } double dis(const Point& a){ return sqrt(a.x * a.x + a.y * a.y); } //-----------------------------------------------------------------// Point circumcenter(const Point& a, const Point& b, const Point& c){ Point ret; double Bx = b.x - a.x, By = b.y - a.y; double Cx = c.x - a.x, Cy = c.y - a.y; double D = 2 * (Bx * Cy - By * Cx); double B = Bx * Bx + By * By; double C = Cx * Cx + Cy * Cy; ret.x = (Cy * B - By * C) / D + a.x; ret.y = (Bx * C - Cx * B) / D + a.y; return ret; } int main(){ scanf("%d", &n); for(int i = 1; i <= n; i ++) b[i].read(); random_shuffle(b+1, b+1+n); Point o = b[1]; double r = 0, Eps = 1e-6; for(int i = 2; i <= n; i ++){ if(dis(b[i] - o) > r + Eps){ o = b[i]; r = 0; for(int j = 1; j < i; j ++){ if(dis(b[j] - o) > r + Eps){ o = (b[i] + b[j]) / 2; r = dis(b[i] - o); for(int k = 1; k < j; k ++){ if(dis(b[k] - o) > r + Eps){ o = circumcenter(b[i], b[j], b[k]); r = dis(b[i] - o); } } } } } } printf("%.2lf %.2lf %.2lf\n", o.x, o.y, r); return 0; }
相关文章推荐
- 反汇编异常定位
- 借助共享缓存redis实现分布式锁
- libevent的使用(socket)
- jfinal_BLOG v1.0
- 秒杀DX12!Vulkan 1.0规范正式发布
- 多核CPU的春天:Vulkan实测 完爆OpenGL
- Thrift入门及Java实例演示
- java 单例模式(Singleton)
- phpredisAdmin
- Android studio 自定义注释模板
- Ubuntu14.04安装tomcat7.0和jdk1.7
- Mysql常用命令详解
- Python 17.1 HTTP协议简介
- Tomcat学习—Tomcat的server.xml配置文件
- hdu4035 概率DP求期望
- Android出现“Read-only file system”解决办法
- js中的面向对象
- Css中overflow属性的认识
- Android Studio集成百度云推送
- Java CookBook--Using a Buffer--by Tim O'Brien 整理by博主