zstu 2666 求三角形各种心到顶点的距离和

直接用模板的,但浙大费马点的模板貌似不太好用啊。。。就自己写了

注意题目给出的是三边长,要先把这三边转换成三个顶点坐标。

另外,三角形中有一个角的角度大于120度时,距离之和就为三角形较短两边之和

View Code
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
const double pi=acos(-1.0);
struct point{double x,y;};
struct line{point a,b;};
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 intersection(line u,line v){
point ret=u.a;
double t=((u.a.x-v.a.x)*(v.a.y-v.b.y)-(u.a.y-v.a.y)*(v.a.x-v.b.x))
/((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x));
ret.x+=(u.b.x-u.a.x)*t;
ret.y+=(u.b.y-u.a.y)*t;
return ret;
}
point incenter(point a,point b,point c){
line u,v;
double m,n;
u.a=a;
m=atan2(b.y-a.y,b.x-a.x);
n=atan2(c.y-a.y,c.x-a.x);
u.b.x=u.a.x+cos((m+n)/2);
u.b.y=u.a.y+sin((m+n)/2);
v.a=b;
m=atan2(a.y-b.y,a.x-b.x);
n=atan2(c.y-b.y,c.x-b.x);
v.b.x=v.a.x+cos((m+n)/2);
v.b.y=v.a.y+sin((m+n)/2);
return intersection(u,v);
}
point perpencenter(point a,point b,point c){
line u,v;
u.a=c;
u.b.x=u.a.x-a.y+b.y;
u.b.y=u.a.y+a.x-b.x;
v.a=b;
v.b.x=v.a.x-a.y+c.y;
v.b.y=v.a.y+a.x-c.x;
return intersection(u,v);
}
point barycenter(point a,point b,point c){
line u,v;
u.a.x=(a.x+b.x)/2;
u.a.y=(a.y+b.y)/2;
u.b=c;
v.a.x=(a.x+c.x)/2;
v.a.y=(a.y+c.y)/2;
v.b=b;
return intersection(u,v);
}
point fermentpoint(point a,point b,point c){
point u,v;
double step=fabs(a.x)+fabs(a.y)+fabs(b.x)+fabs(b.y)+fabs(c.x)+fabs(c.y);
int i,j,k;
u.x=(a.x+b.x+c.x)/3;
u.y=(a.y+b.y+c.y)/3;
while (step>1e-14)
for (k=0;k<10;step/=2,k++)
for (i=-1;i<=1;i++)
for (j=-1;j<=1;j++){
v.x=u.x+step*i;
v.y=u.y+step*j;
if (dis(u,a)+dis(u,b)+dis(u,c)>dis(v,a)+dis(v,b)+dis(v,c))
u=v;
}
return u;
}
point circumcenter(point a,point b,point c){
line u,v;
u.a.x=(a.x+b.x)/2;
u.a.y=(a.y+b.y)/2;
u.b.x=u.a.x-a.y+b.y;
u.b.y=u.a.y+a.x-b.x;
v.a.x=(a.x+c.x)/2;
v.a.y=(a.y+c.y)/2;
v.b.x=v.a.x-a.y+c.y;
v.b.y=v.a.y+a.x-c.x;
return intersection(u,v);
}
int main()
{
int t;
double num[6];
point A,B,C;
point F,I,G,O;
double a,b,c;
double Fd,Id,Gd,Od;
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf%lf",&num[0],&num[1],&num[2]);
sort(num,num+3);a=num[0];b=num[1];c=num[2];
A.x=0.0,A.y=0.0;
B.x=num[2],B.y=0.0;
C.x=1.0*(num[2]*num[2]+num[0]*num[0]-num[1]*num[1])/(2*num[2]);
C.y=sqrt(num[0]*num[0]-C.x*C.x);
if(fabs(C.x)<1e-9) C.x=0.0;
F=fermentpoint(A,B,C);
I=incenter(A,B,C);
G=barycenter(A,B,C);
O=circumcenter(A,B,C);
Id=dis(I,A)+dis(I,B)+dis(I,C);
Gd=dis(G,A)+dis(G,B)+dis(G,C);
Od=dis(O,A)+dis(O,B)+dis(O,C);
double angle=acos((a*a+b*b-c*c)/(2*a*b));
if(angle>(2*pi)/3)
Fd=a+b;
else
Fd=sqrt(a*a+b*b-2*a*b*cos(angle+pi/3));
printf("%.3lf %.3lf %.3lf %.3lf\n",Fd,Id,Gd,Od);
}
return 0;
}

你可能感兴趣的:(T)