1503: 点到圆弧的距离

分析:

先判断点和圆心的连线是否在圆弧范围内,如果在,最短距离即到圆心的距离减去半径的绝对值;反之,为到端点的最短距离。

具体看注释

#include <bits/stdc++.h>
using namespace std; #define eps 1e-8
const double pi=acos(-1); struct Point
{
double x,y;
Point(double a=0,double b=0)
{
x=a;
y=b;
}
}; Point operator - (Point a,Point b)
{
return Point(a.x-b.x,a.y-b.y);
} double dist(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} double multi(Point a,Point b)
{
return a.x*b.x+a.y*b.y;
} double cross(Point a,Point b)
{
return a.x*b.y-a.y*b.x;
} Point TriangleCircumCenter(Point a,Point b,Point c)
{
Point res;
double a1=atan2(b.y-a.y,b.x-a.x)+pi/2;
double a2=atan2(c.y-b.y,c.x-b.x)+pi/2;
double ax=(a.x+b.x)/2;
double ay=(a.y+b.y)/2;
double bx=(c.x+b.x)/2;
double by=(c.y+b.y)/2;
double r1=(sin(a2)*(ax-bx)+cos(a2)*(by-ay))/(sin(a1)*cos(a2)-sin(a2)*cos(a1));
return Point(ax+r1*cos(a1),ay+r1*sin(a1));
} int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int x1,y1,x2,y2,x3,y3,xp,yp;
int kase=0;
while(~scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&xp,&yp))
{
Point p1=Point(x1,y1);
Point p2=Point(x2,y2);
Point p3=Point(x3,y3);
Point pp=Point(xp,yp);
Point pc=TriangleCircumCenter(p1,p2,p3); //算圆心
double temp=cross(p2-p1,p3-p1);
if(temp<0) //如果是顺时针,把p1和p3点互换
{
Point t=p1;
p1=p3;
p3=t;
}
double cosA=multi(p1-pc,p3-pc)/(dist(p1,pc)*dist(p3,pc));
if(fabs(cosA)>1) //如果fabs(cosA)>1,那么acos(cosA)算出的结果是不合法的
{
if(cosA<0) cosA+=eps;
else cosA-=eps;
}
double maxd=acos(cosA); //算p1-pc与p3-pc的夹角
if(cross(p1-pc,p3-pc)<0 && fabs(cross(p1-pc,p3-pc))>eps)
maxd=2*pi-maxd;
double cosB=multi(p1-pc,pp-pc)/(dist(p1,pc)*dist(pp,pc));
if(fabs(cosB)>1)
{
if(cosB<0) cosB+=eps;
else cosB-=eps;
}
double degree=acos(cosB); //算p1-pc与pp-pc的夹角
if(cross(p1-pc,pp-pc)<0 && fabs(cross(p1-pc,pp-pc))>eps)
degree=2*pi-degree;
if(degree<maxd)
printf("Case %d: %.3lf\n",++kase,fabs(dist(pp,pc)-dist(p1,pc)));
else
printf("Case %d: %.3lf\n",++kase,min(dist(pp,p1),dist(pp,p3)));
}
return 0;
}

最新文章

  1. C# 委托应用总结
  2. 29. Xshell连接Linux下Oracle无法回退的解决办法
  3. spring.net (3)依赖注入基础
  4. sprint3冲刺团队贡献分-软件工程
  5. js 定时跳转, 格式化字符串时间
  6. 如何获得bin/Debug目录的路径?
  7. sql遍历树
  8. Poj 3468-A Simple Problem with Integers 线段树,树状数组
  9. Mysql查询的一些操作(查表名,查字段名,查当月,查一周,查当天)
  10. 折腾Java设计模式之状态模式
  11. JS模块化开发(四)——构建工具gulp
  12. PCB特征阻抗计算
  13. problem: vue之数组元素中的数组类型值数据改变却无法在子组件视图更新问题
  14. MyBatis基础入门《九》ResultMap自动匹配
  15. 使用promisify解决fs的回调地狱问题
  16. 【Ruby】【基础】
  17. linux下find命令详解
  18. Meet in the middle学习笔记
  19. JS实现网页背景旋转缩放轮播效果
  20. Centos7中修改Hostname的方法

热门文章

  1. CPU 使用率 100% 怎么办
  2. mysql示例及练习2
  3. 3.13eval函数
  4. RAM与FLASH
  5. 第一章 DevOps概述
  6. MongoDB(7)- 文档插入操作
  7. Go语言网络通信---string与int互转,int64与[]byte互转,int直接互转,string与[]byte互转
  8. GPU核心技术开发
  9. 国内操作系统OS分析(上)
  10. Httprunner的使用