POJ 1329 求三角形外接圆

0
0
0
1. 云栖社区>
2. 博客>
3. 正文

## POJ 1329 求三角形外接圆

prime7 2013-02-28 08:47:00 浏览698

```#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;
}
```

prime7
+ 关注