POJ 1329 Circle Through Three Points(求三角形的外接圆)

Circle Through Three Points


博客原文地址:http://blog.csdn.net/xuechelingxiao/article/details/40985403


题目大意:

给你三个不共线的三个点的坐标,求出过这三个点的圆的方程。写出方程的两种形式。


解题思路:

其实题目要求写出的方程的形式中包含圆心坐标跟半径,所以说关键问题其实就是求出过三点圆的圆心跟半径就OK了。

其实就是个求三角形外接圆的题目,最后加上一些蛋疼的输出控制就可以了。


代码写的有点麻烦,看到Discuss中有用克拉莫法则解的,代码很精炼。

贴个写的很搓的代码吧

#include <stdio.h>
#include <math.h>
const double eps = 1e-10;

struct Point {
    double x, y;
} P, Q, R;
struct Line {
    Point a, b;
} ;

char sign(double x) {
    if(x < -eps) {
        return '-';
    }
    else {
        return '+'; 
    }
}
char sign1(double x) {
    if(x > eps) {
        return '-';
    }
    else {
        return '+';
    }
}

double Distance(Point a, Point b) {
    return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));
}

Point intersection(Line u, Line v)
{
    Point ret = u.a;
    double t = ((u.a.x-v.a.x)*(v.a.y-v.b.y)
                -(u.a.y-v.a.y)*(v.a.x-v.b.x))
    / ((u.a.x-u.b.x)*(v.a.y-v.b.y)
       -(u.a.y-u.b.y)*(v.a.x-v.b.x));
    ret.x += (u.b.x-u.a.x)*t;
    ret.y += (u.b.y-u.a.y)*t;
    return ret;
}

Point circumcenter(Point a,Point b,Point c){
    Line u, v;
    u.a.x = (a.x+b.x)/2;
    u.a.y = (a.y+b.y)/2;
    u.b.x = u.a.x-a.y+b.y;
    u.b.y = u.a.y+a.x-b.x;
    v.a.x = (a.x+c.x)/2;
    v.a.y = (a.y+c.y)/2;
    v.b.x = v.a.x-a.y+c.y;
    v.b.y = v.a.y+a.x-c.x;
    return intersection(u, v);
}

int main()
{
    while(~scanf("%lf%lf%lf%lf%lf%lf", &P.x, &P.y, &Q.x, &Q.y, &R.x, &R.y)) {
        Point t = circumcenter(P, Q, R);
        double r = Distance(t, P);
        //printf("%lf %lf\n", t.x, t.y);
        printf("(x %c %.3lf)^2 + (y %c %.3lf)^2 = %.3lf^2\n", sign1(t.x), fabs(t.x), sign1(t.y), fabs(t.y), r);
        //if()
        printf("x^2 + y^2 %c %.3lfx %c %.3lfy %c %.3lf = 0\n\n", sign1(t.x), 2*fabs(t.x), sign1(t.y), 2*fabs(t.y), sign(t.x*t.x+t.y+t.y-r*r), fabs(t.x*t.x+t.y*t.y-r*r));
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值