牛客小白月赛21(求三角形的外心模板)

题目链接https://ac.nowcoder.com/acm/contest/3947/A
在这里插入图片描述
分析
三角形的外心是三条边垂直平分线的交点。
代码

#include <stdio.h>
#include <math.h>
#define PI 3.141592653589793
typedef struct      //定义点
{
    double x,y;
} Point;
double dis(Point a,Point b)    //两点距离
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
Point getWai(Point a,Point b,Point c)    //解析几何方法,求三角形abc的外心
{
    Point w;
    Point cen1,cen2;    //边ab和边ac的中点
    cen1.x = (a.x+b.x)/2;
    cen1.y = (a.y+b.y)/2;
    cen2.x = (a.x+c.x)/2;
    cen2.y = (a.y+c.y)/2;
    if(a.y==b.y)     //ab的垂线垂直,不存在斜率k的情况
    {
        double k2 = -1.0/((a.y-c.y)/(a.x-c.x));
        double b2 = cen2.y - k2*cen2.x;
        w.x = cen1.x;
        w.y = cen1.x*k2 + b2;
        return w;
    }
    else if(a.y==c.y)     //ac的垂线垂直
    {
        double k1 = -1.0/((a.y-b.y)/(a.x-b.x));
        double b1 = cen1.y - k1*cen1.x;
        w.x = cen2.x;
        w.y = cen2.x*k1 + b1;
        return w;
    }
    else      //不存在垂线垂直的情况
    {
        double k1 = -1.0/((a.y-b.y)/(a.x-b.x));
        double b1 = cen1.y - k1*cen1.x;
        double k2 = -1.0/((a.y-c.y)/(a.x-c.x));
        double b2 = cen2.y - k2*cen2.x;
        w.x = (b2-b1)/(k1-k2);
        w.y = k1*w.x+b1;
        return w;
    }
}
int main()
{
    Point a,b,c;    //三角形的三点
    while(scanf("%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y)!=EOF)
    {
        Point w = getWai(a,b,c);
        printf("%.3lf %.3lf\n",w.x,w.y);
    }
    return 0;
}
发布了165 篇原创文章 · 获赞 6 · 访问量 5041

猜你喜欢

转载自blog.csdn.net/lylzsx20172018/article/details/104038727