首页 > 代码库 > POJ 1329 Circle Through Three Points(求三角形的外接圆)

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;
}


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