最小圆覆盖板子
2021/7/29 6:06:22
本文主要是介绍最小圆覆盖板子,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
int sgn(double x) { if(fabs(x) < eps)return 0; else return x<0?-1:1; }; struct Point {double x,y;}; double Distance(Point A,Point B) {return hypot(A.x-B.x,A.y-B.y);} //三角形外接圆圆心 Point circle_center(const Point a,const Point b,const Point c) { Point center; double a1=b.x-a.x,b1=b.y-a.y,c1=(a1*a1+b1*b1)/2; double a2=c.x-a.x,b2=c.y-a.y,c2=(a2*a2+b2*b2)/2; double d = a1*b2-a2*b1; center.x = a.x+(c1*b2-c2*b1)/d; center.y = a.y+(a1*c2-a2*c1)/d; return center; } void min_cover_circle(Point *p,int n,Point &c,double &r) { random_shuffle(p,p+n); c=p[0]; r=0; for(int i=1; i<n; i++) { if(sgn(Distance(p[i],c)-r)>0) { //p[i]在圆外 c = p[i]; r=0; for(int j=0; j<i; j++) { if(sgn(Distance(p[j],c)-r)>0) { c.x = (p[i].x + p[j].x) / 2; c.y = (p[i].y + p[j].y) / 2; r = Distance(p[j],c); for(int k=0; k<j; k++) { if(sgn(Distance(p[k],c)-r ) > 0) { c = circle_center(p[i],p[j],p[k]); r=Distance(p[i],c); } } } } } } } int n; Point p[N]; Point c; double r; void work() { while(~scanf("%d",&n) && n) { for(int i=0; i<n; i++) { scanf("%lf%lf",&p[i].x,&p[i].y); } min_cover_circle(p,n,c,r); printf("%.2f %.2f %.2f\n",c.x,c.y,r); } }
这篇关于最小圆覆盖板子的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-02springboot项目无法注册到nacos-icode9专业技术文章分享
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)