Circle Through Three Points
程序员文章站
2022-04-01 12:42:04
...
Circle Through Three Points
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
and an equation of the form
Input
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)
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.0Sample 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
思路:只需通过数学计算,由三点求出圆心坐标,即可轻易得到半径,以及标准圆方程的展开方程各项系数。
圆心坐标计算:
假设三点分别为(x1,y1),(x2,y2),(x3,y3),则圆心(x0,y0)可由以下公式计算得:
再将一个点带入圆心坐标方程,即可得到半径r,然后将圆心坐标方程展开的到一般方程,系数c=-2*h,d=-2*k,e=h^2+k^2-r^2;
输出时用条件表达式来控制符号。
代码:
#include<stdio.h>
#include<math.h>
int main() {
double ax, ay, bx, by, cx, cy,a,b,c,d,e,f,h,k,r,cc,dd,ee;
while ((scanf("%lf%lf%lf%lf%lf%lf", &ax, &ay, &bx, &by, &cx, &cy) != EOF)) {
a = ax - bx; b = ay - by; c = ax - cx; d = ay - cy;
e = ((ax*ax - bx*bx) - (by*by - ay*ay)) / 2;
f = ((ax*ax - cx*cx) - (cy*cy - ay*ay)) / 2;
h = -(d*e - b*f) / (b*c - a*d);
k = -(a*f - c*e) / (b*c - a*d);
r = (ax - h)*(ax - h) + (ay - k)*(ay - k);
printf("(x %c %.3lf)^2 + (y %c %.3lf)^2 = %.3lf^2\n",
h<0?'+':'-',h<0?-h:h,k<0?'+':'-',k<0?-k:k,sqrt(r));
cc = -2 * h; dd = -2 * k; ee = h*h + k*k - r;
printf("x^2 + y^2 %c %.3lfx %c %.3lfy %c %.3lf = 0\n\n",
cc < 0 ? '-' : '+', cc < 0 ? -cc : cc, dd < 0 ? '-' : '+', dd < 0 ? -dd : dd,
ee < 0 ? '-' : '+', ee < 0 ? -ee : ee);
}
}
推荐阅读
-
北邮 BOJ 60556 Three Points On A Line
-
poj1329 Circle Through Three Points 三角形外接圆(水)
-
题解:Circle Through Three Points(三角形外心板子)
-
UVA190 Circle Through Three Points
-
POJ 1329 Circle Through Three Points (三角形外心)
-
UVA 190 - Circle Through Three Points
-
Circle Through Three Points
-
POJ - 1329 Circle Through Three Points 求圆
-
计算几何学 | 圆与直线的交点 | Cross Points of a Circle and a Line | C/C++实现
-
UVA190 Circle Through Three Points(计算几何)