poj 1329 Circle Through Three Points
这个题是求外心的问题;
我们可以用直线相交做,也可以用向量做;
直线相交:
View Code
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cmath> #include<queue> #include<set> #include<map> #include<cstring> #include<vector> using namespace std; class Point { public: double x,y; }; double circle_R( Point a, double x, double y ) { return sqrt( ( a.x - x )*( a.x - x ) + ( a.y -y )*(a.y - y) ); } char sig(double x){return fabs(x)<1e-6 || x>=0?'-':'+';} void Solve( Point a, Point b ,Point c ) { double PY = a.x - b.x ,PX = -a.y + b.y; double QY = b.x - c.x ,QX = -b.y + c.y; Point p1,p2; p1.x = ( a.x + b.x )/2;p1.y = ( a.y + b.y )/2; p2.x = ( b.x + c.x )/2;p2.y = ( b.y + c.y )/2; double X = ( QX*( PY*p1.x + PX*p2.y ) - PX*( QX*p1.y + QY*p2.x ) )/( PY*QX - PX*QY ); double Y = ( PY*( QX*p2.y + QY*p1.x ) - QY*( PY*p2.x + PX*p1.y ) )/( PY*QX - PX*QY ); double R = circle_R( a , X ,Y ); double t = X*X + Y*Y - R*R; printf( "(x %c %.3lf)^2 + (y %c %.3lf)^2 = %.3lf^2\n",sig(X),fabs(X),sig(Y),fabs(Y),R ); printf( "x^2 + y^2 %c %.3lfx %c %.3lfy %c %.3lf = 0\n\n",sig(X),fabs(2*X),sig(Y),fabs(2*Y),sig(-t),fabs(t) ); } int main( ) { Point a,b,c; while( scanf( "%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y )==6 ) { Solve( a ,b ,c ); } //system( "pause" ); return 0; }
向量相交:
G是△ABC中一点,那么G是△ABC的外心的充要条件:
(GA+GB)·AB=(GB+GC)·BC=(GC+GA)·CA=0
View Code
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cmath> #include<queue> #include<set> #include<map> #include<cstring> #include<vector> using namespace std; class Point { public: double x,y; }; double circle_R( Point a, double x, double y ) { return sqrt( ( a.x - x )*( a.x - x ) + ( a.y -y )*(a.y - y) ); } int dcmp( double x ) { if( fabs( x ) < 1.0e-6 ) return 0; if( x < 0 ) return -1; return 1; } char sig(double x){return fabs(x)<1e-6 || x>=0?'-':'+';} void Solve( Point a, Point b ,Point c ) { double A = a.x + b.x,B = a.y + b.y; double C = b.x - a.x,D = b.y - a.y; double E = c.x + b.x,F = c.y + b.y; double G = c.x - b.x,H = c.y - b.y; double I = ( A*C + B*D )/2.0; double J = ( E*G + F*H )/2.0; double X = ( I*H - J*D )/( C*H - G*D); double Y = ( I*G - J*C )/( D*G - C*H); double R = circle_R( a , X ,Y ); double t = X*X + Y*Y - R*R; printf( "(x %c %.3lf)^2 + (y %c %.3lf)^2 = %.3lf^2\n",sig(X),fabs(X),sig(Y),fabs(Y),R ); printf( "x^2 + y^2 %c %.3lfx %c %.3lfy %c %.3lf = 0\n\n",sig(X),fabs(2*X),sig(Y),fabs(2*Y),sig(-t),fabs(t) ); } int main( ) { Point a,b,c; while( scanf( "%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y )==6 ) { Solve( a ,b ,c ); } //system( "pause" ); return 0; }


浙公网安备 33010602011771号