标签:
题目:平面中有两个点A,B,还有一个圆心在(0,0)半径为R的圆,问连接A,B的不穿过圆的最短弧长。
分析:计算几何。分两种情况计算:AB线段不穿过圆,AB线段穿过圆;
1.AB线段不穿过圆:圆心到AB距离大于半径或者AB两点在垂线同侧,且两端点到圆心距离都大于R;
这时直接求AB两点间距离即可;
2.AB线段穿过圆:这时分成三段计算,两条切线短加上一段弧长;
如图,弧CD的圆心角利用△OAB中的余弦定理求解a1+a2+a3,a1,a2的余弦分别是R/OA与R/OB。
说明:注意精度╮(╯▽╰)╭。
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> using namespace std; typedef struct pnode { double x,y; pnode(){} pnode(double X, double Y) { x = X;y = Y; } }point; typedef struct lnode { double x,y,dx,dy; lnode(){} lnode(double X, double Y, double DX, double DY) { x = X;y = Y;dx = DX;dy = DY; } }line; double dist_p2p(point a, point b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } double crossproduct(point a, point b, point c) { return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y); } double dist_p2l(point p, point a, point b) { line l(p.x, p.y, a.y-b.y, b.x-a.x); if ((l.dx*(a.y-l.y)-l.dy*(a.x-l.x))*(l.dx*(b.y-l.y)-l.dy*(b.x-l.x)) >= 0) return min(dist_p2p(p, a), dist_p2p(p, b)); return fabs(crossproduct(a, b, p)/dist_p2p(a, b)); } int main() { point A,B,O(0, 0); double R,OA,OB,AB,a; int t; while (~scanf("%d",&t)) while (t --) { scanf("%lf%lf%lf%lf%lf",&A.x,&A.y,&B.x,&B.y,&R); if (dist_p2l(O, A, B)+1e-7 > R) printf("%.3lf\n",dist_p2p(A, B)); else { OA = dist_p2p(A, O); OB = dist_p2p(B, O); AB = dist_p2p(A, B); a = acos((OA*OA+OB*OB-AB*AB)/(2.0*OA*OB))-acos(R/OA)-acos(R/OB); printf("%.3lf\n",sqrt(OA*OA-R*R)+sqrt(OB*OB-R*R)+a*R); } } return 0; }
UVa 10180 - Rope Crisis in Ropeland!
标签:
原文地址:http://blog.csdn.net/mobius_strip/article/details/44709195