POJ 1329 Circle Through Three Points

来源:互联网 发布:glanceplus软件翻译 编辑:程序博客网 时间:2024/05/11 05:07

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

题目:

Circle Through Three Points
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 3176 Accepted: 1347

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.01.0 7.0 8.0 6.0 7.0 -2.0

Sample Output

(x - 3.000)^2 + (y + 2.000)^2 = 5.000^2x^2 + y^2 - 6.000x + 4.000y - 12.000 = 0(x - 3.921)^2 + (y - 2.447)^2 = 5.409^2x^2 + y^2 - 7.842x - 4.895y - 7.895 = 0

解题思路:

计算几何,模板题,关键就是求三角形的外心,

(x,y)为圆心坐标:

已知三个点的坐标是:(x1,y1),(x2,y2), (x3,y3);

A2 = x2 - x1, B2 = y2 - y1, A3 = x3 - x1, B3 = y3 - y1,C2 = (x2 * x2 - x1*x1 + y2 * y2 - y1 * y1), C3  = (x3 * x3 - x1 * x1 + y3 * y3 - y1 * y1);

x = (C2 * B3 - C3 * B2) / (B3 * A2 - A3 * B2) / 2;
y = (C2 * A3 - C3 * A2) / (B2 * A3 - B3 * A2) / 2;

r = sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));


代码:

#include <cstdio>#include <cmath>using namespace std;struct Point{double x, y;};Point a, b, c;double x, y, r;void fun(){double A2 = b.x - a.x, B2 = b.y - a.y;double C2 = b.y * b.y - a.y * a.y + b.x * b.x - a.x * a.x;double A3 = c.x - a.x, B3 = c.y - a.y;double C3 = c.y * c.y - a.y * a.y + c.x * c.x - a.x * a.x;x = (C2 * B3 - C3 * B2) / (B3 * A2 - A3 * B2) / 2;y = (C2 * A3 - C3 * A2) / (B2 * A3 - B3 * A2) / 2;r = sqrt((x - a.x) * (x - a.x) + (y - a.y) * (y - a.y));}int main(){while(~scanf("%lf%lf%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y, &c.x, &c.y)){fun();printf("(x ");printf("%c", x > 0 ? '-' : '+');printf(" %.3f)^2 + (y ", fabs(x));printf("%c", y > 0 ? '-' : '+');printf(" %.3f)^2 = %.3f^2\n", fabs(y), r);printf("x^2 + y^2 ");printf("%c", -2 * x > 0 ? '+' : '-');printf(" %.3fx ", fabs(-2 * x));printf("%c", -2 * y > 0 ? '+' : '-');printf(" %.3fy ", fabs(-2 * y));printf("%c", x * x + y * y - r *r > 0 ? '+' : '-');printf(" %.3f = 0\n\n", fabs(x * x + y * y - r * r));}return 0;}


0 0
原创粉丝点击