POJ 2242 The Circumference of the Circle

完全是数学知识,求面积时可以利用三角形有向面积公式

 

#include <iostream> #include <cmath> #include <iomanip> using namespace std; const double PI = 3.141592653589793; double nX1, nX2, nX3, nY1, nY2, nY3; double square(double x) { return x*x; } int main() { while(cin>>nX1>>nY1>>nX2>>nY2>>nX3>>nY3) { //double s = nX1*nY2 + nX3*nY1 +nX2*nY3 - nX3*nY2 - nX2*nY1 - nX1*nY3; double a = sqrt(square(nX1 - nX2) + square(nY1 - nY2)); double b = sqrt(square(nX1 - nX3) + square(nY1 - nY3)); double c = sqrt(square(nX2 - nX3) + square(nY2 - nY3)); double p = (a + b + c) / 2; double s = sqrt(p * (p - a) * (p - b) * (p - c)); double r = a * b * c / (s * 4); cout<<fixed<<setprecision(2)<<2 * PI * r<<endl; } return 0; } //有向面积解法 #include <iostream> #include <iomanip> #include <cmath> using namespace std; const double PI = 3.141592653589793; double nX1, nX2, nX3, nY1, nY2, nY3; double square(double x) { return x*x; } int main() { while(cin>>nX1>>nY1>>nX2>>nY2>>nX3>>nY3) { double s = (nX1*nY2 + nX3*nY1 +nX2*nY3 - nX3*nY2 - nX2*nY1 - nX1*nY3)/2; if(s < 1e-9) //判断小数为负数用这种方法 { s = -s; } double a = sqrt(square(nX1 - nX2) + square(nY1 - nY2)); double b = sqrt(square(nX1 - nX3) + square(nY1 - nY3)); double c = sqrt(square(nX2 - nX3) + square(nY2 - nY3)); double r = a * b * c / (4 * s); cout<<fixed<<setprecision(2)<<2 * PI * r<<endl; } return 0; }

你可能感兴趣的:(POJ 2242 The Circumference of the Circle)