CSU 1503: 点到圆弧的距离(计算几何)

题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1503

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

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

PS:

分两种情况:

第一种:点跟圆心的连线在那段扇形的圆弧范围内,点到圆弧的最短距离为点到圆心的距离减去半径然后取绝对值;

第二种:点跟圆心的连线不在那段扇形的圆弧范围内,点到圆弧的最短的距离为到这段圆弧的两个端点的最小值。

代码如下:(参考夏笑声

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
const double PI = acos(-1.0);

struct Point
{
    double x,y;
    friend Point operator - (Point a,Point b) //重载友元运算符
    {
        Point temp;
        temp.x = a.x - b.x;
        temp.y = a.y - b.y;
        return temp;
    }
};

Point p1, p2, p3, pc, pp;
double r;

Point get_pc1(Point p1, Point p2, Point p3)  //求圆心
{
    double a, b, c, d, e, f;
    Point p;
    a = 2*(p2.x-p1.x);
    b = 2*(p2.y-p1.y);
    c = p2.x*p2.x+p2.y*p2.y-p1.x*p1.x-p1.y*p1.y;
    d = 2*(p3.x-p2.x);
    e = 2*(p3.y-p2.y);
    f = p3.x*p3.x+p3.y*p3.y-p2.x*p2.x-p2.y*p2.y;
    p.x = (b*f-e*c)/(b*d-e*a);
    p.y = (d*c-a*f)/(b*d-e*a);
    r = sqrt((p.x-p1.x)*(p.x-p1.x)+(p.y-p1.y)*(p.y-p1.y));//半径
    return p;
}
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_cha(Point p1,Point p2)   //叉积
{
    return p1.x * p2.y - p2.x * p1.y;
}

double get_ans(Point pc,Point pp,Point p1,Point p2,Point p3)
{
    double temp = mult_cha(p2-p1,p3-p1);//判断给点方向是顺时针还是逆时针
    double f1 = atan2((p1-pc).y,(p1-pc).x);//和x正半轴的夹角
    double f2 = atan2((p2-pc).y,(p2-pc).x);
    double f3 = atan2((p3-pc).y,(p3-pc).x);
    double f4 = atan2((pp-pc).y,(pp-pc).x);

    double ans1 = fabs(dis(pp,pc) - r);//到圆心减去半径
    double ans2 = min(dis(pp,p1),dis(pp,p3));//取到端点的较小值

    if(temp < 0)    //顺时针给点
    {
        if(f1 < f3) //判断区间方向,向下凹
        {
            if((f2 >= f1 && f2 <= f3) == (f4 >= f1 && f4 <= f3) )//p点和p2点在同一个区间
                return ans1;
            else                        //p点和p2点不在同一个区间
                return ans2;
        }
        else//向上凸
        {
            if((f2 >= f3 && f2 <= f1) == (f4 >=f3 && f4 <= f1) )
                return ans1;
            else
                return ans2;
        }
    }
    else
    {
        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 cas = 0;
    while(~scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y,&p3.x,&p3.y,&pp.x,&pp.y))
    {
        pc = get_pc1(p1,p2,p3);//圆心
        double ans = get_ans(pc,pp,p1,p2,p3);

        printf("Case %d: %.3lf\n",++cas,ans);
    }
    return 0;
}
时间: 2024-08-28 06:18:19

CSU 1503: 点到圆弧的距离(计算几何)的相关文章

CSU 1503 点到圆弧的距离(2014湖南省程序设计竞赛A题)

题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1503 解题报告:分两种情况就可以了,第一种是那个点跟圆心的连线在那段扇形的圆弧范围内,这样的话点到圆弧的最短距离就是点到圆心的距离减去半径然后再取绝对值就可以了,第二种情况是那个点跟圆心的连线不在那段扇形的圆弧范围内,这样的话,最短的距离就是到这段圆弧的端点的最小值. 接下来的第一步就是求圆心的坐标跟圆的半径,只要求出圆心坐标半径就好说了,求圆心坐标我用的方法是: 设圆心坐标是(a,b

csu 1503: 点到圆弧的距离-湖南省第十届大学生计算机程序设计竞赛

就是--判断p与圆心的连线与圆的交点在不在圆弧上,在就是它到p的距离,不在就是p跟端点的最短距离 #include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector>

csuoj 1503: 点到圆弧的距离

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1503 1503: 点到圆弧的距离 时间限制: 1 Sec  内存限制: 128 MB  Special Judge 提交: 247  解决: 59 [提交][状态][讨论版] 题目描述 输入一个点P和一条圆弧(圆周的一部分),你的任务是计算P到圆弧的最短距离.换句话说,你需要在圆弧上找一个点,到P点的距离最小. 提示:请尽量使用精确算法.相比之下,近似算法更难通过本题的数据. 输入 输入包含最多

csu-acm 1503: 点到圆弧的距离

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

点到圆弧的距离(csu1503)+几何

1503: 点到圆弧的距离 Time Limit: 1 Sec  Memory Limit: 128 MB  Special JudgeSubmit: 325  Solved: 70[Submit][Status][Web Board] Description 输入一个点P和一条圆弧(圆周的一部分),你的任务是计算P到圆弧的最短距离.换句话说,你需要在圆弧上找一个点,到P点的距离最小.提示:请尽量使用精确算法.相比之下,近似算法更难通过本题的数据. Input 输入包含最多10000组数据.每组

poj1584 A Round Peg in a Ground Hole 判断多边形凹凸,点到线的距离【基础计算几何】

大致思路:首先对于所给的洞的点,判断是否是凸多边形,图形的输入和输出可以是顺时针或者逆时针,而且允许多点共线 Debug 了好几个小时,发现如下问题 判断三点是否共线,可用斜率公式判断 POINT point_A, point_B, point_C; if(point_A.x == point_B.x || point_B.x == point_C.x){ if(point_A.x == point_B.x && point_B.x == point_C.x) continue; }els

5-7 点到原点的距离(多态)

给出下面的一个基类框架: class Point { protected: int dimension;//点的维数,最多不超过100维 private: int point_length[100]://点数组 public: Point ();//构造函数根据需要重载 float distance( );//计算当前点到原点的距离 void display();//输出点 } 以Point为基类建一个派生类Point_2D,增加以下数据成员: float x;//2D平面上点的x坐标 floa

POJ1584 判断多边形是否为凸多边形,并判断点到直线的距离

求点到直线的距离: double dis(point p1,point p2){   if(fabs(p1.x-p2.x)<exp)//相等的  {    return fabs(p2.x-pegx);    }  else     {   double k=(p2.y-p1.y)/(p2.x-p1.x);   double b=p2.y-k*p2.x;   return fabs(k*pegx-pegy+b)/sqrt(k*k+1);//返回的是距离的   }}判断多边形是否为凸多边形 if

URAL 1348 Goat in the Garden 2(点到线段的距离)

[题目链接]:click here~~ [题目大意]:求某点到一条线段的最小距离与最大距离. [思路]: 分析可知,最大距离一定在端点处取得.那么接下来求最小距离时,先求出垂线与线段所在直线的交点,然后判断交点在不在线段上.如果在,则最小距离为垂线段的距离,即交点到此点的距离.如果不在,则最小距离必在另一端点取得.问题转换如何判断点与线段的垂足是否在线段上,可以利用叉积方便的求出. 代码: /* * Problem: NO:URAL 1348 * Running time: 1MS * Comp