http://acm.hdu.edu.cn/showproblem.php?pid=1374
已知三点坐标,求三点确定的圆的周长
#include <iostream> #include <cmath> #include <algorithm> using namespace std ; //由正弦定理 sin90°/d=sinA/a 既d=a/sinA //s=1/2(bcsinA) 既sinA=2s/bc //由海伦公式 s=sqrt(p(p-a)(p-b)(p-c)),p=(a+b+c)/2 #define PI 3.141592653589793 int main() { double x1,y1,x2,y2,x3,y3 ; while(~scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3)) { double a=sqrt(pow(x1-x2,2)+pow(y1-y2,2)) ; double b=sqrt(pow(x2-x3,2)+pow(y2-y3,2)) ; double c=sqrt(pow(x1-x3,2)+pow(y1-y3,2)) ; double p=(a+b+c)/2 ; double s=sqrt(p*(p-a)*(p-b)*(p-c)) ; double d=a/(2*s/(b*c)) ; printf("%.2lf ",PI*d) ; } return 0 ; }
用向量算面积精确度更高
#include <iostream> #include <cmath> #include <algorithm> using namespace std ; //由正弦定理 sin90°/d=sinA/a 既d=a/sinA //s=1/2(bcsinA) 既sinA=2s/bc //由多边形面积公式算得s #define PI 3.141592653589793 int main() { double x1,y1,x2,y2,x3,y3 ; while(~scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3)) { double a=sqrt(pow(x1-x2,2)+pow(y1-y2,2)) ; double b=sqrt(pow(x2-x3,2)+pow(y2-y3,2)) ; double c=sqrt(pow(x1-x3,2)+pow(y1-y3,2)) ; double _2s=fabs(x1*y2-x2*y1+x2*y3-x3*y2+x3*y1-x1*y3) ; double d=a/(_2s/(b*c)) ; printf("%.2lf ",PI*d) ; } return 0 ; }