LightOJ 1433 Minimum Arc Distance

本文介绍了一种通过计算向量的叉积和点积来确定劣弧弧长的方法,并提供了一个具体的C语言实现示例。文章针对LightOJ 1433题目,通过输入圆心坐标及两端点坐标计算弧长。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接 http://lightoj.com/volume_showproblem.php?problem=1433

题意: 求劣弧弧长

思路: 先算角度再算弧长。虽然题目中说精度到小数点后3位就行,但测试数据很强,尽量少开方。

             这题比 LightOJ 1130 好算多了……

#include <stdio.h>
#include <math.h>
 
const double PI=acos(-1.0);
const double STD=1e-9;
struct Point
{
    double x,y;
    Point friend Vec (Point a,Point b)   //构造向量
    {
        Point res;
        res.x=a.x-b.x;
        res.y=a.y-b.y;
        return res;
    }
}cir,a,b;
 
double get_cj (Point a,Point b)        //计算叉积
{
    return a.x*b.y-a.y*b.x;
}
 
double get_dj(Point a,Point b)        //计算点积
{
    return a.x*b.x+a.y*b.y;
}
 
double get_dis (Point a,Point b)        //计算两点间距离
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
 
double get_dis2 (Point a,Point b)       //计算两点间距离的平方
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}

double Deal ()
{
    scanf("%lf%lf%lf%lf%lf%lf",&cir.x,&cir.y,&a.x,&a.y,&b.x,&b.y);
    if (fabs(a.x-b.x)< STD && fabs(a.y-b.y)<STD)
        return 0;
    double da=get_dis2 (a,cir);
    double db=get_dis2 (b,cir);
    double cj=get_cj (Vec(a,cir),Vec(b,cir));
    double dj=get_dj (Vec(a,cir),Vec(b,cir));
    double dsin=cj/sqrt(da*db);       //da,db是平方过的,先开方再乘WA……
    double alpha=asin(dsin);
    if (dj < 0)           //当夹角大于90度时
        if (alpha>0)
            alpha=PI-alpha;
        else
            alpha=-PI-alpha;
    return get_dis (a,cir)*fabs(alpha);
}

int main ()
{
    int T;
    scanf("%d",&T);
    for (int cas=1;cas<=T;cas++)
        printf("Case %d: %.6lf\n",cas,Deal());
    return 0;
}
 
/*
5
1 1 1 1 1 1
10 10 9 10 11 10
10000 10000 9000 10000 10000 9000
10000 10000 10000 9000 9000 10000
1 1 101 1 1 101

Output
Case 1: 0.00000000
Case 2: 3.14159265
Case 3: 1570.79632679
Case 4: 1570.79632679
Case 5: 157.07963268
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值