c语言编写一个函数,函数计算三角形面积,并用这个函数计算出凸多边

2024-11-23 09:25:58
推荐回答(1个)
回答1:

#include

#include

float dis(float x1,float y1,float x2,float y2)

{return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

}

float area(float x1,float y1,float x2,float y2,float x3,float y3)

{float a,b,c,p;

 a=dis(x1,y1,x2,y2);

b=dis(x1,y1,x3,y3);

c=dis(x2,y2,x3,y3);

p=(a+b+c)/2.0;

return sqrt(p*(p-a)*(p-b)*(p-c));

}

int main()

{int i,j,n;

 float x[100],y[100],s=0;

 scanf("%d",&n);

 for(i=0;i

    scanf("%f%f",&x[i],&y[i]);

 for(i=0;i

  s+=area(x[i],y[i],x[i+1],y[i+1],x[(i+2)%n],y[(i+2)%n]);

printf("S=%.4f\n",s);

return 0;

}