原题链接--UVA12304 2D Geometry 110 in 1!

这是一道圆的入门基础题,要求有点多,写了一个下午写完,然后WA了六发,

最后隔了一周再做,找了一下午bug终于AC了,难受啊。。。。。 上代码:

#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const double Pi=acos(-1);
const double eps=1e-6;
struct Point{double x,y;Point(double x=0,double y=0):x(x),y(y){};
};
typedef Point Vector;
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 B){return Vector(A.x*B,A.y*B);}
Vector operator /(Vector A,double B){return Vector(A.x/B,A.y/B);}
int dcmp(double x){if(fabs(x)<eps)return 0;return x<0?-1:1;
}
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 dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;}
void Correct(double &A){while(dcmp(A)<0)A+=Pi;while(dcmp(A-Pi)>=0)A-=Pi;
}
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 Cross(Vector A,Vector B){return A.x*B.y-A.y*B.x;}
double Angle(Vector A,Vector B){return acos(Dot(A,B)/Length(A)/Length(B));}
double Angle(Vector A) {return atan2(A.y, A.x);}
Vector Normal(Vector A){return Vector(-A.y,A.x);}
Vector Rotate(Vector A,double rad){return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));}
Vector unit_Vector(Vector A){Vector B;double s=sqrt((A.x*A.x+A.y*A.y));B.x=A.x/s,B.y/s;return B;
}
vector<Point>sol;
struct Circle{    //圆
    Point c;double r;Circle(Point c = Point(0, 0), double r = 0):c(c),r(r){}Point point(double a){return Point(c.x+cos(a)*r,c.y+sin(a)*r);}
};
struct Line{    //直线
    Point p;Vector v;double ang;Line(){}Line(Point p,Vector v):p(p),v(v){ang=atan2(v.y,v.x);}bool operator < (const Line &L)const {return ang<L.ang;}Point point(double a){return p+v*a;}
};
Point line_point(Point p,Vector v,Point q,Vector w){//直线交点 Vector u=p-q;double t=Cross(w,u)/Cross(v,w);return p+v*t;
}
double dist_point_line(Point A,Point B,Point C){//点到直线的距离 Vector BC=C-B;Vector _BC=Rotate(BC,Pi/2);Point D=line_point(B,BC,A,_BC);Vector DA=A-D;return Length(DA);
}
Point mid_point(Point A,Point B){//中点
    Point C;C.x=(A.x+B.x)/2,C.y=(A.y+B.y)/2;return C;
}
void parallel(Point A,Point B,double d,vector<Line>&sol){//平行线 Vector AB=B-A;Vector _AB=Normal(AB);_AB=_AB/(Length(_AB))*d;Point ta,tb;ta=A+_AB;tb=B+_AB;Line l1=Line(ta,tb-ta);sol.push_back(l1);ta=A-_AB;tb=B-_AB;Line l2=Line(ta,tb-ta);sol.push_back(l2);
}
int line_circle(Line L,Circle C,double &t1,double &t2,vector<Point>&sol){//直线与圆的交点 double a=L.v.x,b=L.p.x-C.c.x,c=L.v.y,d=L.p.y-C.c.y;double e=a*a+c*c,f=2*(a*b+c*d),g=b*b+d*d-C.r*C.r;double delta=f*f-4.*e*g;if(dcmp(delta)<0) return 0;//相离if(dcmp(delta)==0)//相切
    {t1=t2=-f/(2.*e); sol.push_back(L.point(t1));return 1;}//相切t1=(-f-sqrt(delta))/(2.*e); sol.push_back(L.point(t1));t2=(-f+sqrt(delta))/(2.*e); sol.push_back(L.point(t2));return 2;
}
int circle_circle(Circle C1,Circle C2,vector<Point>&sol){//圆与圆的交点 double d=Length(C1.c-C2.c);if(dcmp(d)==0){if(dcmp(C1.r-C2.r)==0)return -1;return 0;}if(dcmp(C1.r+C2.r-d)<0)return 0;if(dcmp(fabs(C1.r-C2.r)-d)>0)return 0;double a=Angle(C2.c-C1.c);double da=acos((C1.r*C1.r+d*d-C2.r*C2.r)/(2*d*C1.r));Point p1=C1.point(a-da),p2=C1.point(a+da);sol.push_back(p1);if(p1==p2)return 1;sol.push_back(p2);return 2;
}
int gettangents(Point p,Circle C,Vector *v){//直线与圆的切线 Vector u=C.c-p;double dist=Length(u);if(dist<C.r)return 0;else if(dcmp(dist-C.r)==0){v[0]=Rotate(u,Pi/2);return 1;}else{double ang=asin(C.r/dist);v[0]=Rotate(u,-ang);v[1]=Rotate(u,+ang);return 2;}
}
Circle tangle_out_cir(Point A,Point B,Point C){//外接圆 Point D=mid_point(A,B);Point E=mid_point(A,C);Vector AB=B-A;Vector AC=C-A;Vector DO=Rotate(AB,Pi/2);Vector OE=Rotate(AC,Pi/2);Point O=line_point(D,DO,E,OE);Vector AO=O-A;Circle C1=Circle(O,Length(AO));return C1;
}
Circle tangle_in_cir(Point A,Point B,Point C){//内接圆 Vector AB=B-A;Vector AC=C-A;if(Cross(AB,AC)<0){Point t=A;A=C;C=t;}AB=B-A;AC=C-A;Vector BA=A-B;Vector BC=C-B;double a=Angle(AB,AC);double b=Angle(BA,BC);Vector AO=Rotate(AB,a/2);Vector BO=Rotate(BC,b/2);Point O=line_point(A,AO,B,BO);double r=dist_point_line(O,A,B);Circle C1=Circle(O,r);return C1;
}
void tangent_point_cir(Point O,double r,Point A){//过A与圆O的切线 Circle C1=Circle(O,r);Vector v[10];int n=gettangents(A,C1,v);double a[10];for(int i=0;i<n;i++){a[i]=atan2(v[i].y,v[i].x);Correct(a[i]);}sort(a,a+n);printf("[");for(int i=0;i<n;i++){printf("%.6lf",a[i]*180/Pi);if(i<n-1)printf(",");}printf("]\n");
}
void point_line_r_cir(Point A,Point B,Point C,double r){//过A与BC相切的圆 Circle C1=Circle(A,r);vector<Line>L;vector<Point>p;parallel(B,C,r,L);double t1,t2;for(int i=0;i<L.size();i++){line_circle(L[i],C1,t1,t2,p);}sort(p.begin(),p.end());printf("[");int h=p.size();for(int i=0;i<p.size();i++){printf("(%.6lf,%.6lf)",p[i].x,p[i].y);if(i<h-1)printf(",");}printf("]\n");
}
void line_line_cir(Point A,Point B,Point C,Point D,double r){//与AB和CD相切的圆 vector<Line>L1;vector<Line>L2;parallel(A,B,r,L1);parallel(C,D,r,L2);Point t;vector<Point>sol;for(int i=0;i<2;i++){for(int j=0;j<2;j++){t=line_point(L1[i].p,L1[i].v,L2[j].p,L2[j].v);sol.push_back(t);}}sort(sol.begin(),sol.end());printf("[");for(int i=0;i<4;i++){printf("(%.6lf,%.6lf)",sol[i].x,sol[i].y);if(i<3)printf(",");}printf("]\n");
}
void cir_cir_out_cir(Point A,double r1,Point B,double r2,double r){//与圆A和圆B外切的圆 Circle C1=Circle(A,r1+r);Circle C2=Circle(B,r2+r);vector<Point>sol;circle_circle(C1,C2,sol);sort(sol.begin(),sol.end());int n=sol.size();printf("[");for(int i=0;i<n;i++){printf("(%.6lf,%.6lf)",sol[i].x,sol[i].y);if(i<n-1)printf(",");}printf("]\n");
}
Point A,B,C,D,O;
double r,r1,r2;
string s;
int main(){while(cin>>s){if(s=="CircumscribedCircle"){scanf("%lf%lf%lf%lf%lf%lf",&A.x,&A.y,&B.x,&B.y,&C.x,&C.y);Circle C1=tangle_out_cir(A,B,C);printf("(%.6lf,%.6lf,%.6lf)\n",C1.c.x,C1.c.y,C1.r);}else if(s=="InscribedCircle"){scanf("%lf%lf%lf%lf%lf%lf",&A.x,&A.y,&B.x,&B.y,&C.x,&C.y);Circle C1=tangle_in_cir(A,B,C);printf("(%.6lf,%.6lf,%.6lf)\n",C1.c.x,C1.c.y,C1.r);}else if(s=="TangentLineThroughPoint"){scanf("%lf%lf%lf%lf%lf",&A.x,&A.y,&r,&B.x,&B.y);tangent_point_cir(A,r,B);}else if(s=="CircleThroughAPointAndTangentToALineWithRadius"){scanf("%lf%lf%lf%lf%lf%lf%lf",&A.x,&A.y,&B.x,&B.y,&C.x,&C.y,&r);point_line_r_cir(A,B,C,r);}else if(s=="CircleTangentToTwoLinesWithRadius"){scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf",&A.x,&A.y,&B.x,&B.y,&C.x,&C.y,&D.x,&D.y,&r);line_line_cir(A,B,C,D,r);}else{scanf("%lf%lf%lf%lf%lf%lf%lf",&A.x,&A.y,&r1,&B.x,&B.y,&r2,&r);cir_cir_out_cir(A,r1,B,r2,r);}}
}

转载于:https://www.cnblogs.com/ccsu-kid/p/9450367.html

UVA12304 2D Geometry 110 in 1!相关推荐

  1. LA 3263 That Nice Euler Circuit (2D Geometry)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  2. 第7期:计算几何(持续更新中......)

    // 2022/01/22更新 更新内容主要为算法竞赛入门经典--训练指南(升级版)(刘汝佳.陈锋编著)第4章几何问题 1 二维几何基础 在平面坐标系下,向量和点一样,也用两个数x,y表示.第6章介绍 ...

  3. 1、DirectX 系列之 Direct 2D

    Direct 2D是一个 采用立即模式 绘图的2D 图形API,也就是跟显卡驱动打交道的一个小程序,图形业务能力包括 2-D geometry, bitmaps, and text. WM_Paint ...

  4. [读书笔记]《Head First Servlets JSP》2nd

    书名:Head First Servlets and JSP: Passing the Sun Certified Web Component Developer Exam 出版商:O'Reilly ...

  5. 使用WPF创建画图箭头

    使用WPF创建画图箭头 原文:使用WPF创建画图箭头 今天要给leader line画个箭头,所以就google一下,找到下面的文章,写的不错,可以实现我的需求,所以就摘录下来. 我把源代码中的arr ...

  6. Go语言(golang)开源项目大全

    http://www.open-open.com/lib/view/open1396063913278.html#Compression 内容目录 Astronomy 构建工具 缓存 云计算 命令行选 ...

  7. 开源项目推荐:OpenGL/Vulkan/Cairo/Skia/angle/VTK/OpenVG/MyPaint/GIMP/Krita/Pencil2D/inkspace/enve等绘图库或画图软件

    绘图引擎简介 Windows环境下二维绘图引擎有多种选择:GDI.GDI+.DirectDraw.Qt/QPainter.Agg.Cairo.skia.Direct2D.Direct3D.OpenGL ...

  8. 点云 3D 目标检测 - PointPillars(CVPR 2019)

    点云 3D 目标检测 - PointPillars(CVPR 2019) 摘要 1. 导言 1.1 相关工作 1.1.1 使用CNNs进行目标检测 1.1.2 激光雷达点云中的目标检测 1.2 贡献 ...

  9. golang 开源项目全集

    一直更新中,地址:https://github.com/golang/go/wiki/Projects#zeromq Indexes and search engines These sites pr ...

  10. 图形图形处理方面的一位微软专家的主页,

    刚在Github上分享了一些不错的代码 http://hhoppe.com/ Hugues Hoppe   »DemosPublicationsTalksAcademicProfessionalMis ...

最新文章

  1. 独家 | 10个数据科学家常犯的编程错误(附解决方案)
  2. 添加cacti监控_小水谈监控---Cacti安装(监控)
  3. Away3D 的实体收集器Bug
  4. oracle查询表实际大小,简要分析估算oracle表的大小
  5. linux修改mysql配置文件_Linux下PHP开发环境搭建(Apache2.4+PHP7.1+MySQL5.7)
  6. ajax post传输到后台为空?【解决方案】
  7. React系列---Webpack环境搭建(二)不同环境不同配置
  8. html怎么给边框改样式,html里面怎么设置边框?html边框样式设置方法
  9. Python Pytest中fixture之yield唤醒teardown和终结函数addfinalizer
  10. java 获取web目录结构_Intellij Idea Javaweb 目录结构及获取项目内资源
  11. WINDOWS操作系统中可以允许最大的线程数
  12. 阶段1 语言基础+高级_1-3-Java语言高级_08-JDK8新特性_第1节 常用函数接口_8_常用的函数式接口_Supplier接口...
  13. 银行软件测试测试用例,银行测试用例设计经验总结,应该怎样去设计测试用例?...
  14. 电脑能正常上网上网,某些软件不能上网
  15. Google+ about Gmail?
  16. bigworld游戏服务器架构参考
  17. 基于Domoticz智能家居系统(十四)用ESP8266做MQTT客户端实验
  18. 使用Fiddler对手机App抓包
  19. 近10年NBA球队常规赛排名变化——tableau 实现bump chart
  20. Java api文档自动生成工具smartdoc+torna

热门文章

  1. Manjaro-KDE安装动态桌面插件
  2. POI使用详解 java 复杂excel导出(笔记)
  3. 基于Boost.Asio的异步编程
  4. matlab学霸表白公式,学霸隐藏式表白数学公式
  5. PWN之堆利用-unlink攻击
  6. 我是如何走进黑客世界的?
  7. led灯闪烁代码_如何设置LED灯并使其通过代码闪烁
  8. android 电子签名设备,[Android]实现电子签名并截屏
  9. R语言dplyr包bind_cols函数把两个dataframe数据的列横向合并起来、最终列数为原来两个dataframe列数的加和(Combine Data Frames)
  10. beautify配置