190 Circle Through Three Points
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
and an equation of the form
Each line of input to your program will contain the x and y coordinates of three points, in the order , , , , , . 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 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
解题思路:
已知两点(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> using namespace std; struct Point{ double x,y; Point(double x0=0,double y0=0){ x=x0; y=y0; } friend istream& operator>>(istream &in,Point &p){ in>>p.x>>p.y; return in; } friend ostream& operator<<(ostream &out,Point &p){ out<<p.x<<" "<<p.y; return out; } double getdis(Point p){ return double(sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y))); } }; struct Line{//Line ax+by+c=0 double a,b,c; Line(double a0=0,double b0=0,double c0=0){ a=a0; b=b0; c=c0; } }; Line getLine(Point p1,Point p2){//get the Line that cross point (p1 + p2)/2 //a= x2-x1,b=y2-y1,c=(x1*x1-x2*x2+y1*y1-y2*y2)/2; Line tmpLine; tmpLine.a=p2.x-p1.x; tmpLine.b=p2.y-p1.y; tmpLine.c=(p1.x*p1.x-p2.x*p2.x+p1.y*p1.y-p2.y*p2.y)/double(2); //cout<<tmpLine.a<<" "<<tmpLine.b<<" "<<tmpLine.c<<endl; return tmpLine; } Point getCrossPoint(Line l1,Line l2){//get the cross point of Line l1 and l2 //point((b1*c2-b2*c1)/ (a1*b2-b1*a2),(c1*a2-c2*a1)/(a1*b2-b1*a2)) Point tmpPoint; tmpPoint.x=(l1.b*l2.c-l2.b*l1.c)/(l1.a*l2.b-l1.b*l2.a); tmpPoint.y=(l1.c*l2.a-l2.c*l1.a)/(l1.a*l2.b-l1.b*l2.a); //cout<<tmpPoint<<endl; return tmpPoint; } void output(Point p,double r){ //(x - 3.000)^2 + (y + 2.000)^2 = 5.000^2 printf("(x "); if(p.x<=1e-9) printf("+"); else printf("-"); printf(" %.3lf)^2 + (y ",abs(p.x)); if(p.y<=1e-9) printf("+"); else printf("-"); printf(" %.3lf)^2 = %.3lf^2\n",abs(p.y),r); //x^2 + y^2 - 6.000x + 4.000y - 12.000 = 0 printf("x^2 + y^2 "); if(p.x<=1e-9) printf("+"); else printf("-"); printf(" %.3lfx ",abs(2*p.x)); if(p.y<=1e-9) printf("+"); else printf("-"); printf(" %.3lfy ",abs(2*p.y)); double last=r*r-p.x*p.x-p.y*p.y; if(last<=1e-9) printf("+"); else printf("-"); printf(" %.3lf = 0\n\n",abs(last)); } int main(){ Point a,b,c,center; while(cin>>a>>b>>c){ Line l1=getLine(a,b); Line l2=getLine(b,c); center=getCrossPoint(l1,l2); double r=center.getdis(a); output(center,r); } return 0; }