1. 程式人生 > >POJ1329 Circle Through Three Points(三角形外接圓)

POJ1329 Circle Through Three Points(三角形外接圓)

ive math points ogr sig single 代碼 multiple ati

題目鏈接:

  http://poj.org/problem?id=1329

題目描述:

Circle Through Three Points

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

題目大意:

  給三個點求出三角形外接圓

  並輸出圓的標準型和一般型

思路:

  套模板

  註意輸出處理數值為0(輸出x^2)

代碼:

 1 #include <iostream>
 2
#include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #include <vector> 7 using namespace std; 8 9 const double EPS = 1e-10; //精度系數 10 const double PI = acos(-1.0); //π 11 12 13 struct Point { 14 double x, y; 15 Point(double x = 0, double y = 0) :x(x), y(y) {} 16 }; //點的定義 17 18 typedef Point Vector; //向量的定義 19 20 Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); } //向量減法 21 22 int dcmp(double x) { 23 if (fabs(x) < EPS)return 0; else return x < 0 ? -1 : 1; 24 } //與0的關系 25 26 double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; } //向量點乘 27 double Length(Vector A) { return sqrt(Dot(A, A)); } //向量長度 28 29 struct Circle { 30 Point c; 31 double r; 32 Circle() :c(Point(0, 0)), r(0) {} 33 Circle(Point c, double r = 0) :c(c), r(r) {} 34 Point point(double a) { return Point(c.x + cos(a)*r, c.y + sin(a)*r); } //輸入極角返回點坐標 35 }; // 36 37 Circle CircumscribedCircle(Point p1, Point p2, Point p3) { 38 double Bx = p2.x - p1.x, By = p2.y - p1.y; 39 double Cx = p3.x - p1.x, Cy = p3.y - p1.y; 40 double D = 2 * (Bx*Cy - By*Cx); 41 double cx = (Cy*(Bx*Bx + By*By) - By*(Cx*Cx + Cy*Cy)) / D + p1.x; 42 double cy = (Bx*(Cx*Cx + Cy*Cy) - Cx*(Bx*Bx + By*By)) / D + p1.y; 43 Point p = Point(cx, cy); 44 return Circle(p, Length(p1 - p)); 45 } //三角形外接圓 46 47 Point A, B, C; 48 49 void print(double num) { 50 if (dcmp(num) < 0) { printf("- "); num = -num; } else printf("+ "); 51 printf("%.3lf", num); 52 } //輸出 53 54 int main() { 55 while (~scanf("%lf%lf%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y, &C.x, &C.y)) { 56 Circle ans = CircumscribedCircle(A, B, C); 57 double a = ans.c.x, b = ans.c.y; 58 double c = -2 * a, d = -2 * b, e = a*a + b*b - ans.r*ans.r; 59 int tmp = dcmp(a); 60 if (tmp == 0)printf("x^2"); 61 else { printf("(x "); print(-a); printf(")^2"); } 62 printf(" + "); 63 tmp = dcmp(b); 64 if (tmp == 0)printf("y^2"); 65 else { printf("(y "); print(-b); printf(")^2"); } 66 printf(" = %.3lf^2\n", ans.r); 67 printf("x^2 + y^2 "); print(c); printf("x "); print(d); printf("y "); print(e); 68 printf(" = 0\n\n"); 69 } 70 }

POJ1329 Circle Through Three Points(三角形外接圓)