凸多边形的面积问题
Q:給定頂點坐標,求凸多邊形的面積,保留兩位小數。
樣例輸入:
4
3 3
3 0
1 2
1 0
樣例輸出
5.00
S:
#include<stdio.h> #include<math.h> #include<stdlib.h> double dist(struct point A,struct point B); //該子函數用于求兩點間的距離 double square(double a,double b,double c); //該子函數用于求任意三角形的面積 struct point //該結構體用于保存點的信息,包括橫、縱坐標 {double x,y; }; int main() {double a,b,c;//三角形的三條邊 double temp;//三角形的面積 double S=0;//總面積 int i;int n;//表示要求的多邊形為n邊形 struct point *P;P=(point*)malloc(sizeof(point));scanf("%d",&n);for(i=0;i<n;i++)//輸入多邊形頂點坐標 {scanf("%lf%lf",&P[i].x,&P[i].y);}for(i=1;i<n-1;i++)//n邊形可被分割為(n-2)個三角形 {if(i==1)//只需要單獨求一次a,之后有:a’=c a=dist(P[i],P[0]);b=dist(P[i],P[i+1]);c=dist(P[i+1],P[0]);temp=square(a,b,c);S+=temp;//累加總面積 a=c;//a’=c }free(P);printf("%.2lf\n",S);//輸出結果 return 0; } double dist(struct point A,struct point B) {double s;//double a=A.x;double b=B.x;double c=A.y;double d=B.y;//這四行使代碼簡潔 s=sqrt((a-b)*(a-b)+(c-d)*(c-d));//兩點間距離公式 return s; } double square(double a,double b,double c) {double p;double S;p=(a+b+c)/2;S=sqrt(p*(p-a)*(p-b)*(p-c));海倫公式 return S; }?
轉載于:https://www.cnblogs.com/LegendLa/p/4269660.html
總結
- 上一篇: 结构体数组排序
- 下一篇: 部署OCS后修改web farm地址