已知三邊 a,b,c,半周長 p = (a+b+c)/2,面積 S = √[p(p-a)(p-b)(p-c)](海倫公式)。外接圓半徑 R = abc / (4S)。
證明:S = ½ ab sin C,正弦定理 c/sin C = 2R → R = c/(2 sin C) = abc/(4S)。
余弦定理:c² = a² + b² - 2ab cos C。正弦定理:c/sin C = 2R。利用 sin²C + cos²C = 1 消去角度可得 R。
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
double dist(double x1, double y1, double x2, double y2) {
return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
}
int main() {
double x1,y1,x2,y2,x3,y3;
while (cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3) {
double a = dist(x1,y1,x2,y2);
double b = dist(x1,y1,x3,y3);
double c = dist(x2,y2,x3,y3);
double p = (a+b+c)/2;
double S = sqrt(p*(p-a)*(p-b)*(p-c));
double R = a*b*c/(4*S);
double circumference = 2*PI*R;
cout << fixed << setprecision(2) << circumference << endl;
}
return 0;
}