csu 1503 点到圆弧的距离(第十届湖南省省赛A题,几何)

点到圆弧的距离

Time Limit: 1 Sec  Memory Limit:128 MB  Special Judge
Submit: 614  Solved: 101
[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

题意:中文题不解释

思路:CSU上的数据貌似出问题了,用省赛给的数据10000组都过了还过不了CUS上面的题目。 网上的代码也过不了。

这个题关键在于判断圆弧是劣弧还是优弧,所以要利用到B点。

如果面积OAB+OBC=OAC&&OAP'+OP'C==OAC,那么圆弧是个劣弧

P'是OP在圆上的交点,注意是在圆弧内的那一个

判断出P点在哪就简单了,首先答案是PA和PC较小的一个,然后如果P点是在题目给的扇形范围内,再加一个|OP-r|

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#define eps (1e-4)
#define N 205
#define dd double
#define sqr(x) ((x)*(x))
const double pi = acos(-1);
using namespace std;
struct Point
{
    double x,y;
};
double cross(Point a,Point b,Point c) ///叉积
{
    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
double dis(Point a,Point b) ///距离
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double mult(Point a,Point b,Point c)  ///点积
{
    return (a.x-c.x)*(b.x-c.x)+(a.y-c.y)*(b.y-c.y);
}
Point waixin(Point a,Point b,Point c) ///外接圆圆心坐标
{
    Point p;
    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;
    p.x = a.x + (c1*b2 - c2*b1)/d, p.y=a.y + (a1*c2 -a2*c1)/d;
    return p;
}
Point intersection(Point a,Point b,Point c,Point d)///直线交点
{
    Point p = a;
    double t =
        ((a.x-c.x)*(c.y-d.y)-(a.y-c.y)*(c.x-d.x))/((a.x-b.x)*(c.y-d.y)-(a.y-b.y)*(c.x-d
                .x));
    p.x +=(b.x-a.x)*t;
    p.y +=(b.y-a.y)*t;
    return p;
}
void intersection_line_circle(Point c,double r,Point l1,Point l2,Point& p1,Point& p2)///直线与圆的交点
{
    Point p=c;
    double t;
    p.x+=l1.y-l2.y;
    p.y+=l2.x-l1.x;
    p=intersection(p,c,l1,l2);
    t=sqrt(r*r-dis(p,c)*dis(p,c))/dis(l1,l2);
    p1.x=p.x+(l2.x-l1.x)*t;
    p1.y=p.y+(l2.y-l1.y)*t;
    p2.x=p.x-(l2.x-l1.x)*t;
    p2.y=p.y-(l2.y-l1.y)*t;
}
double getarea(Point a,Point b,Point c,double r){///已知圆弧上三点求扇形面积(只能求劣弧)
    double temp = mult(a,b,c)/(dis(a,c)*dis(b,c));
    if(temp<=-1) temp = -1;///注意acos范围和-1到1,但是由于精度的原因可能会出现temp=-1.000000010,直接acos会溢出
    if(temp>=1) temp = 1;
    double angle = acos(temp);
    return fabs(angle*r*r/2);
}
int main()
{
    Point a,b,c,p,p1,p2,p3;
    int t = 1;
    //freopen("a.in","r",stdin);
    //freopen("a.txt","w",stdout);
    while(scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&p.x,&p.y)!=EOF)
    {
        Point circle = waixin(a,b,c);
        double r = dis(circle,a);
        double ans = min(dis(p,a),dis(p,c));
        double op = dis(circle,p);
        double area1 = getarea(a,c,circle,r);
        double area2 = getarea(a,b,circle,r);
        double area3 = getarea(b,c,circle,r);
        intersection_line_circle(circle,r,circle,p,p1,p2);
        if(acos(mult(p,p1,circle)/((dis(p,circle))*dis(p1,circle)))<0) p3 = p2;
        else p3 = p1;
        double area4 = getarea(a,p3,circle,r);
        double area5 = getarea(c,p3,circle,r);
        double temp = mult(a,c,circle)/(dis(a,circle)*dis(c,circle));
        if(temp<=-1) temp = -1;
        if(temp>=1) temp = 1;
        double angle_aoc = acos(temp);
        if(fabs(angle_aoc-pi)<eps){ ///平角特殊处理
            if(cross(p,c,circle)*cross(b,c,circle)>=0){
                ans = min(ans,fabs(op-r));
            }
            printf("Case %d: %.3lf\n",t++,ans);
            continue;
        }
        if(fabs(area1-area2-area3)<eps)  ///劣弧
            if(fabs(area1-area4-area5)<eps) 
                ans = min(ans,fabs(op-r));
        else
        {
            if(fabs(area1-area4-area5)>eps) 
                ans = min(ans,fabs(op-r));
        }
        printf("Case %d: %.3lf\n",t++,ans);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值