首页 > 代码库 > 曾经写的一个程序
曾经写的一个程序
#include<stdio.h> #include<math.h> #define N 2 double mianji(double x1,double y1,double x2,double y2,double x3,double y3);//计算三角形面积 double getLength(double a1,double b1,double a2,double b2);//计算两点之间的距离 double getShortestLength(double x0,double y0, double x1,double y1,double x2,double y2);//计算点到线段的距离 void main() { int i,k=0; double x[N],y[N],z[N],n,m,chang=0,g=0,h=0,s=0,area2=0,a,b,area,area1; for(;;) { printf("输入这个点的横坐标和纵坐标:"); scanf("%lf %lf",&n,&m); printf("输入横坐标:"); for(i=0;i<N;i++) scanf("%lf",&x[i]); printf("输入纵坐标:"); for(i=0;i<N;i++) scanf("%lf",&y[i]); for(k=0;k<N-1;k++) z[k]=getShortestLength(n,m,x[k], y[k],x[k+1],y[k+1]); z[N-1]=getShortestLength(n,m,x[N-1], y[N-1],x[0],y[0]); chang=z[0]; for(k=1;k<N;k++) { if(chang>z[k]) chang=z[k];} for(i=0;i<N;i++){ g+=x[i]; h+=y[i];} a=g/N; b=h/N; for(i=0;i<N-1;i++){ area=mianji(x[i],y[i],x[i+1],y[i+1],a,b); s+=area;} area=mianji(x[0],y[0],x[N-1],y[N-1],a,b); s+=area; for(i=0;i<N-1;i++){ area1=mianji(x[i],y[i],x[i+1],y[i+1],n,m); area2+=area1;} area1=mianji(x[0],y[0],x[N-1],y[N-1],n,m); area2+=area1; printf("%lf %lf",s,area2); printf("\n"); if(area2-s<0.001) printf("%lf",chang-2*chang); else printf("%lf",chang); } } double mianji(double x1,double y1,double x2,double y2,double x3,double y3)//计算三角形面积 { double l1,l2,l3,q,e; l1=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); l2=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3)); l3=sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3)); q=(l1+l2+l3)/2; e=sqrt(q*(q-l1)*(q-l2)*(q-l3)); return e; } double getLength(double a1,double b1,double a2,double b2)//计算两点之间的距离 { double result; result = sqrt((a1-a2)*(a1-a2) + (b1-b2)*(b1-b2)); return result; } double getShortestLength(double x0,double y0, double x1,double y1,double x2,double y2)//计算点到线段的距离 { double a,b,c; // 边长 double d ; //垂线段长度 double A,B,C; //斜线表达式系数 double result ; //1 求a,b,c 子功能 a = getLength(x1,y1,x2,y2); b = getLength(x0,y0,x2,y2); c = getLength(x0,y0,x1,y1); //2 判断 B角是否钝角 if( a*a + c*c - b*b <= 0) //钝角 { result = c; }else{ //锐角 if( a*a + b*b - c*c <=0 ) //判断 C是否为钝角 { result = b; }else{ //双锐角 A = y1-y2; B = x2-x1; C = x1*y2 - x2*y1; d =fabs ( A*x0 + B*y0 +C ) / sqrt( A*A + B*B ); result = d; } } return result; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。