poj1329 Circle Through Three Points

Circle Through Three Points
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3130   Accepted: 1325

Description

Your team is to write a program that, given the Cartesian coordinates of three points on a plane, will find the equation of the circle through them all. The three points will not be on a straight line. 
The solution is to be printed as an equation of the form 
	(x - h)^2 + (y - k)^2 = r^2				(1)

and an equation of the form 
	x^2 + y^2 + cx + dy - e = 0				(2)

Input

Each line of input to your program will contain the x and y coordinates of three points, in the order Ax, Ay, Bx, By, Cx, Cy. These coordinates will be real numbers separated from each other by one or more spaces.

Output

Your program must print the required equations on two lines using the format given in the sample below. Your computed values for h, k, r, c, d, and e in Equations 1 and 2 above are to be printed with three digits after the decimal point. Plus and minus signs in the equations should be changed as needed to avoid multiple signs before a number. Plus, minus, and equal signs must be separated from the adjacent characters by a single space on each side. No other spaces are to appear in the equations. Print a single blank line after each equation pair.

Sample Input

7.0 -5.0 -1.0 1.0 0.0 -6.0
1.0 7.0 8.0 6.0 7.0 -2.0

Sample Output

(x - 3.000)^2 + (y + 2.000)^2 = 5.000^2
x^2 + y^2 - 6.000x + 4.000y - 12.000 = 0

(x - 3.921)^2 + (y - 2.447)^2 = 5.409^2
x^2 + y^2 - 7.842x - 4.895y - 7.895 = 0

Source

Southern California 1989,UVA 190



给三个点求圆的方程

推公式,没什么说的,用克莱姆法则解方程可以方便点不用考虑分母0的情况。

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;

struct Point
{
    double x,y,z;
};

double Cal(Point s,Point t)
{
    return (s.x*s.x-t.x*t.x+s.y*s.y-t.y*t.y)/2;
}

int main()
{
    Point a[3];
    int i;
    while(scanf("%lf%lf",&a[0].x,&a[0].y)!=EOF)
    {
        for (i=1;i<=2;i++)
        {
            scanf("%lf%lf",&a[i].x,&a[i].y);
        }
        Point a1,a2;
        a1.x=a[0].x-a[1].x;
        a2.x=a[0].x-a[2].x;
        a1.y=a[0].y-a[1].y;
        a2.y=a[0].y-a[2].y;
        a1.z=Cal(a[0],a[1]);
        a2.z=Cal(a[0],a[2]);
        double x=-(a2.z*a1.y-a1.z*a2.y)/(a2.y*a1.x-a1.y*a2.x);
        double y=(a2.z*a1.x-a1.z*a2.x)/(a2.y*a1.x-a1.y*a2.x);
        double r=sqrt((x-a[0].x)*(x-a[0].x)+(y-a[0].y)*(y-a[0].y));
        printf("(x %s %.3f)^2 + (y %s %.3f)^2 = %.3f^2\n",x<0?"+":"-",fabs(x),y<0?"+":"-",fabs(y),r);
        printf("x^2 + y^2 %s %.3fx %s %.3fy %s %.3f = 0\n",2*x<0?"+":"-",fabs(2*x),2*y<0?"+":"-",fabs(2*y),x*x+y*y-r*r>0?"+":"-",fabs(x*x+y*y-r*r));
        printf("\n");
    }
    return 0;
}


你可能感兴趣的:(poj1329 Circle Through Three Points)