1. 程式人生 > >uva 190(計算幾何)

uva 190(計算幾何)

 uva 190  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

equation20

and an equation of the form

equation25

Each line of input to your program will contain the x

 and y coordinates of three points, in the order tex2html_wrap_inline46 , tex2html_wrap_inline48tex2html_wrap_inline50 , tex2html_wrap_inline52 , tex2html_wrap_inline54 , tex2html_wrap_inline56 . These coordinates will be real numbers separated from each other by one or more spaces.

Your program must print the required equations on two lines using the format given in the sample below. Your computed values for hkrcd, 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
解題思路:
已知兩點(x1,y1)(x2,y2)求直線方程(ax+by+c=0) :
解得:a=y1-y2,b=x2-x1,c=x1*y2-x2*y1;
已知兩點(x1,y1)(x2,y2)求直線中垂線方程(ax+by+c=0) :
解得:a= x2-x1,b=y2-y1,c=(x1*x1-x2*x2+y1*y1-y2*y2)/2;
已知兩直線(不共線)方程(ax+by+c=0)求交點:
解得:point (  (b1*c2-b2*c1)/ (a1*b2-b1*a2)  ,  (c1*a2-c2*a1)/(a1*b2-b1*a2)  )
程式碼:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
	double x1,y1,x2,y2,x3,y3;
	while(cin>>x1>>y1>>x2>>y2>>x3>>y3)
	{
		double a1,b1,c1,a2,b2,c2;
		a1=x2-x1;
		b1=y2-y1;
		c1=(x1*x1-x2*x2+y1*y1-y2*y2)/2.0;
		a2=x3-x2;
		b2=y3-y2;
		c2=(x2*x2-x3*x3+y2*y2-y3*y3)/2.0;
		double h,k,r,c,d,e;
		h=(b1*c2-b2*c1)/(a1*b2-b1*a2);
		k=(c1*a2-c2*a1)/(a1*b2-b1*a2);
		r=sqrt((x1-h)*(x1-h)+(y1-k)*(y1-k));
		c=-2.0*h;
		d=-2.0*k;
		e=h*h+k*k-r*r;
		cout<<setiosflags(ios::fixed)<<setprecision(3);
		cout<<"(x "<<((h<=1e-9)?"+ ":"- ")<<((h<=1e-9)?-h:h)<<")^2 + (y "<<((k<=1e-9)?"+ ":"- ")<<((k<=1e-9)?-k:k)<<")^2 = "<<r<<"^2"<<endl;
		cout<<"x^2 + y^2 "<<((c<=1e-9)?"- ":"+ ")<<((c<=1e-9)?-c:c)<<"x "<<((d<=1e-9)?"- ":"+ ")<<((d<=1e-9)?-d:d)<<"y "<<((e<=1e-9)?"- ":"+ ")<<((e<=1e-9)?-e:e)<<" = 0"<<endl<<endl;
	}
	return 0;
}