poj1329 Circle Through Three Points 三角形外接圆(水)
给出三个点,求外接圆的两种表达式://代码如下:
[cpp]
#include<iostream>
#include<cstdio>
#include<math.h>
#define eps 1e-8
struct point{double x,y;};
point a,b,c;
double distance(point p1,point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
point intersection(point p1,point p2,point p3,point p4)
{
point ret=p1;
double t=((p1.x-p3.x)*(p3.y-p4.y)-(p1.y-p3.y)*(p3.x-p4.x))
/((p1.x-p2.x)*(p3.y-p4.y)-(p1.y-p2.y)*(p3.x-p4.x));
ret.x+=(p2.x-p1.x)*t;
ret.y+=(p2.y-p1.y)*t;
return ret;
}
point circenter(point a,point b,point c)//Èý½ÇÐÎÍâ½ÓÔ²ÐÄ
{
point p1,p2,p3,p4;
p1.x=(a.x+b.x)/2.0;
p1.y=(a.y+b.y)/2.0;
p2.x=p1.x-(b.y-a.y);
p2.y=p1.y+(b.x-a.x);
p3.x=(a.x+c.x)/2.0;
p3.y=(a.y+c.y)/2.0;
p4.x=p3.x-(c.y-a.y);
p4.y=p3.y+(c.x-a.x);
return intersection(p1,p2,p3,p4);
}
int main()
{
while(~scanf("%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y))
{
point cen=circenter(a,b,c);
double dis=distance(cen,a);
printf("(x %c %.3lf)^2 + (y %c %.3lf)^2 = %.3lf^2\n",(cen.x>=0 ?'-':'+'),fabs(cen.x),(cen.y>=0 ?'-':'+'),fabs(cen.y),dis);
printf("x^2 + y^2 %c %.3lfx %c %.3lfy %c %.3lf = 0\n",
(cen.x>=0 ?'-':'+'), 2*fabs(cen.x), (cen.y>=0 ?'-':'+'),2*fabs(cen.y),(cen.x*cen.x+cen.y*cen.y-dis*dis>=0 ? '+':'-'),fabs(cen.x*cen.x+cen.y*cen.y-dis*dis));
printf("\n");
}
return 0;
}
作者:ssslpk
补充:软件开发 , C++ ,