2013年10月9日 星期三

UVa 438 - The Circumference of the Circle

Problem link

Keyword: math, geometry

Algorithm:

A circle passing through 3 points is the circumcircle of this triangle.
So how to find the diameter? We have to use law of sines.
To use that, we have to find sin theta first.
We may use vector to find cos theta of one angle.
After that we can find sin theta because sin^2+cos^2=1.
Finally, we can find the diameter and the answer! :D

Code:
#include<cstdio>
#include<cmath>
inline double dist( double x, double y ) { return sqrt( x*x + y*y ); }

int main()
{
    double x1, y1, x2, y2, x3, y3, cosine, sine, pi = acos( -1 );

    while( scanf( "%lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3 ) != EOF )
    {
        cosine = ( ( x2-x1 ) * ( x3-x1 ) + ( y2-y1 ) * ( y3-y1 ) ) / dist( y2-y1, x2-x1 ) / dist( y3-y1, x3-x1 );
        sine = sqrt( 1 - cosine*cosine );
        printf( "%.2lf\n", dist( y2-y3, x2-x3 ) / sine * pi + 1e-12 );
    }

    return 0;
}

沒有留言:

張貼留言