Circle Through Three Points

Circle Through Three Points

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 37   Accepted Submission(s) : 10
Problem 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
PKU
 1 #include <string.h>
 2 #include <stdlib.h>
 3 #include <stdio.h>
 4 #include <math.h>
 5 typedef struct point { double x, y; }point;
 6 typedef struct line{point a,b;}line;
 7 point intersection(line u,line v)
 8 {
 9     point ret=u.a;
10     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));
11     ret.x+=(u.b.x-u.a.x)*t;
12     ret.y+=(u.b.y-u.a.y)*t;
13     return ret;
14 }
15 point circumcenter(point a,point b,point c)
16 {
17     line u,v;
18     u.a.x=(a.x+b.x)/2;
19     u.a.y=(a.y+b.y)/2;
20     u.b.x=u.a.x-a.y+b.y;
21     u.b.y=u.a.y+a.x-b.x;
22     v.a.x=(a.x+c.x)/2;
23     v.a.y=(a.y+c.y)/2;
24     v.b.x=v.a.x-a.y+c.y;
25     v.b.y=v.a.y+a.x-c.x;
26     return intersection(u,v);
27 }
28 double distance(point p1,point p2)
29 {
30     return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
31 }
32 int main()
33 {
34     point L1,L2,L3,S;
35     int T,i,x1,y1,x2,y2,x3,y3;
36     double Len;
37     while( scanf("%lf%lf%lf%lf%lf%lf",&L1.x,&L1.y,&L2.x,&L2.y,&L3.x,&L3.y)!=EOF)
38     {
39         S=circumcenter(L1,L2,L3);
40         Len=distance(S,L1);
41         printf("(x ");
42         if(S.x>0)printf("- %.3f)^2 + (y ",S.x);
43         else printf("+ %.3f)^2 + (y ",-S.x);
44         if(S.y>0)printf("- %.3f)^2 = %.3f^2\n",S.y,Len);
45         else printf("+ %.3f)^2 = %.3f^2\n",-S.y,Len);
46         printf("x^2 + y^2 ");
47         if(2*S.x>0)printf("- %.3fx ",2*S.x);
48         else printf("+ %.3fx ",-2*S.x);
49         if(2*S.y>0)printf("- %.3fy ",2*S.y);
50         else printf("+ %.3fy ",-2*S.y);
51         if(S.x*S.x+S.y*S.y-Len*Len>0)printf("+ %.3f = 0\n",S.x*S.x+S.y*S.y-Len*Len);
52         else printf("- %.3f = 0\n",-(S.x*S.x+S.y*S.y-Len*Len));
53         putchar(10);
54     }
55     return 0;
56 }
View Code

 

posted @ 2014-08-22 13:54  Wurq  阅读(229)  评论(0编辑  收藏  举报