求三角形外心的模版 解析几何做法
程序员文章站
2022-04-01 16:11:23
...
immmm…….. 虽然这种算法不够优美,能用就行吧
#include <stdio.h>
#include <math.h>
#define PI 3.141592653589793
typedef struct { //定义点
double x,y;
} Point;
double dis(Point a,Point b) { //两点距离
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
Point getWai(Point a,Point b,Point c) //解析几何方法,求三角形abc的外心
{
Point w;
Point cen1,cen2; //边ab和边ac的中点
cen1.x = (a.x+b.x)/2;
cen1.y = (a.y+b.y)/2;
cen2.x = (a.x+c.x)/2;
cen2.y = (a.y+c.y)/2;
if(a.y==b.y){ //ab的垂线垂直,不存在斜率k的情况
double k2 = -1.0/((a.y-c.y)/(a.x-c.x));
double b2 = cen2.y - k2*cen2.x;
w.x = cen1.x;
w.y = cen1.x*k2 + b2;
return w;
}
else if(a.y==c.y){ //ac的垂线垂直
double k1 = -1.0/((a.y-b.y)/(a.x-b.x));
double b1 = cen1.y - k1*cen1.x;
w.x = cen2.x;
w.y = cen2.x*k1 + b1;
return w;
}
else { //不存在垂线垂直的情况
double k1 = -1.0/((a.y-b.y)/(a.x-b.x));
double b1 = cen1.y - k1*cen1.x;
double k2 = -1.0/((a.y-c.y)/(a.x-c.x));
double b2 = cen2.y - k2*cen2.x;
w.x = (b2-b1)/(k1-k2);
w.y = k1*w.x+b1;
return w;
}
}
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)!=EOF){
Point w = getWai(a,b,c);
double r = dis(w,a);
printf("%.2lf\n",2*PI*r);
}
return 0;
}