湖南省第十届大学生计算机程序设计竞赛1503: 点到圆弧的距离(atan()函数的应用)

1503: 点到圆弧的距离
Time Limit: 1 Sec  Memory Limit: 128 MB  Special Judge
Submit: 400  Solved: 88
[Submit][Status][Web Board]
Description

输入一个点P和一条圆弧(圆周的一部分),你的任务是计算P到圆弧的最短距离。换句话说,你需要在圆弧上找一个点,到P点的距离最小。
提示:请尽量使用精确算法。相比之下,近似算法更难通过本题的数据。

 


Input

输入包含最多10000组数据。每组数据包含8个整数x1, y1, x2, y2, x3, y3, xp, yp。圆弧的起点是A(x1,y1),经过点B(x2,y2),结束位置是C(x3,y3)。点P的位置是 (xp,yp)。输入保证A, B, C各不相同且不会共线。上述所有点的坐标绝对值不超过20。

 


Output

对于每组数据,输出测试点编号和P到圆弧的距离,保留三位小数。你的输出和标准输出之间最多能有0.001的误差。

 


Sample Input
0 0 1 1 2 0 1 -1
3 4 0 5 -3 4 0 1

Sample Output
Case 1: 1.414
Case 2: 4.000

HINT

 


Source

湖南省第十届大学生计算机程序设计竞赛

 

 

 
上图即为atan2(x,y)反正切值在个个方向的大小变化
 atan2(x1-x,y1-y)表示(x1,y1)以(x,y)为原点的反正切值
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
const double maxs=100000000;
using namespace std;

double x,y,r;
/*
double chaji(double ax,double ay,double bx,double by,double cx,double cy)    //两条线段斜率的差,double !!!
{
    return (by-ay)*(cx-ax)-(cy-ay)*(bx-ax);
}
*/
void count(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a,b,c,d,e,f;
    a=2*(x2-x1);
    b=2*(y2-y1);
    c=x2*x2+y2*y2-x1*x1-y1*y1;
    d=2*(x3-x2);
    e=2*(y3-y2);
    f=x3*x3+y3*y3-x2*x2-y2*y2;
    x=(b*f-e*c)/(b*d-e*a);
    y=(d*c-a*f)/(b*d-e*a);
    r=sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1));

}
double bj(double xp,double yp,double x1,double y1)
{
    double s=sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));
    return s;
}
double nodecount(double x1,double y1,double x2,double y2,double x3,double y3,double xp,double yp)
{
    double f1=atan2(x1-x,y1-y);
    double f2=atan2(x2-x,y2-y);
    double f3=atan2(x3-x,y3-y);
    double f4=atan2(xp-x,yp-y);//atans(x,y)反切值函数,见c++函数应用,此时所求为(xp,yp)以(x,y)为原点的反切值

    double ans1=fabs(r-bj(xp,yp,x,y));//fabs绝对值函数
    double ans2=min(bj(xp,yp,x1,y1),bj(xp,yp,x3,y3));
    //cout<<ans1<<" "<<ans2<<endl;
    if(f1<f3)
    {
        if((f2>=f1&&f2<=f3)==(f4>=f1&&f4<=f3))
        return ans1;
        else
        return ans2;
    }
    else
    {
        if((f2>=f3&&f2<=f1)==(f4>=f3&&f4<=f1))
        return ans1;
        else
        return ans2;
    }
}
int main()
{
    int mark=0;
    double ans;
    double x1,y1,x2,y2,x3,y3,xp,yp;
    double k,k1,k2;
    while(~scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3,&xp,&yp))
    {
        mark++;
        count(x1,y1,x2,y2,x3,y3);
       // cout<<x<<y<<r<<endl;
        ans=nodecount(x1,y1,x2,y2,x3,y3,xp,yp);
        printf("Case %d: %.3lf
",mark,ans);
    }

}
原文地址:https://www.cnblogs.com/dshn/p/4750686.html