题意给出三角形三点坐标让求出该三角形的外接圆标准方程和一般方程。
这题输出很恶心,注意0.000的时候要输出并且前面的符号为“ + ”,那么外接圆的半径r通过S=(a*b*c)/(4*r)可以求出r=(a*b*c)/(4*S),然后外心坐标是三边垂直平分线的交点,求出两个垂直平分线方程然后可以得出交点坐标。
#include <iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; typedef double PointType; struct point { PointType x,y; }; PointType TriangleArea(point pi,point pj,point pk) //判断向量PiPj在向量PiPk的顺逆时针方向 +顺-逆0共线 { return fabs((pj.x-pi.x)*(pk.y-pi.y)-(pk.x-pi.x)*(pj.y-pi.y))/2; } PointType Dis(point a,point b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } int main() { point a,b,c; double r; int f=0; while(~scanf("%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y)) { if(f) cout<<endl; f++; r=Dis(a,b)*Dis(a,c)*Dis(b,c)/TriangleArea(a,b,c)/4; double c1=(a.x*a.x+a.y*a.y-b.x*b.x-b.y*b.y)/2; double c2=(a.x*a.x+a.y*a.y-c.x*c.x-c.y*c.y)/2; point centre; centre.x=(c1*(a.y-c.y)-c2*(a.y-b.y))/((a.x-b.x)*(a.y-c.y)-(a.x-c.x)*(a.y-b.y)); centre.y=(c1*(a.x-c.x)-c2*(a.x-b.x))/((a.y-b.y)*(a.x-c.x)-(a.y-c.y)*(a.x-b.x)); //圆心为Center 半径为r cout<<"(x"; if(centre.x>0) cout<<" - ",printf("%.3f",centre.x); else if(centre.x<=0) cout<<" + ",printf("%.3f",-centre.x); cout<<")^2 + (y"; if(centre.y>0) cout<<" - ",printf("%.3f",centre.y); else if(centre.y<=0) cout<<" + ",printf("%.3f",-centre.y); cout<<")^2 = ",printf("%.3f",r),cout<<"^2"<<endl; //第二行输出开始 cout<<"x^2 + y^2"; if(centre.x>0) cout<<" - ",printf("%.3fx",2*centre.x); else if(centre.x<=0) cout<<" + ",printf("%.3fx",2*-centre.x); if(centre.y>0) cout<<" - ",printf("%.3fy",2*centre.y); else if(centre.y<=0) cout<<" + ",printf("%.3fy",2*-centre.y); if(centre.x*centre.x+centre.y*centre.y-r*r>=0) cout<<" + ",printf("%.3f",centre.x*centre.x+centre.y*centre.y-r*r); else if(centre.x*centre.x+centre.y*centre.y-r*r<0) cout<<" - ",printf("%.3f",-(centre.x*centre.x+centre.y*centre.y-r*r)); cout<<" = 0"<<endl; } return 0; }