C语言编程题 题目:任意输入4个点,求围成四边形的面积
3个回答
展开全部
我测试过了,四点一线,凹四边形,四边形,有两个点共点,都可以,这里使用一条知道N边形N个顶点坐标求N边形的面积的公式,这些情况其实已经可以不考虑,呵呵,自动求
凹四边形情况:
有三点共一线形成三角形的情况:
#include "stdio.h"
#include "math.h"
void main()
{
double x[4],y[4];
for(int i=0;i<4;i++)
{
scanf("%lf%lf",&x[i],&y[i]);
}
double mianji=0.0;
for(int ii = 1 ; ii< 4 ; ii++)
{
mianji+=(x[ii-1]*y[ii]-x[ii]*y[ii-1]);
}
mianji+=x[3]*y[0]-x[0]*y[3];
mianji= fabs(0.5*mianji);
printf("%lf\n",mianji);
}
更多追问追答
追问
怎么不对啊,我复制运行不出,你帮忙看看 哪错了,谢谢
追答
你一行一行的复制吧
直接全部复制,会变得没有格式,全部一行,要自己分行,你就一行,一行的复制好了
不过我刚刚直接复制这个页面上的代码,新建一个也可以运行呀,我是在VC6上面建立的代码
这个代码绝对没问题,而且你要求的题目我也刚刚做了,而且是更复杂的N边形面积问题,还要求N边形与直线相交后,求直线以下那部分N边形的面积的问题,都可以解决,我给你的只是一小部分代码而已
展开全部
//
// 算法说明:
// 凸四边形任意一条边的两个顶点位于另外两个顶点所在直线的同一侧
// 凹四边形有两条边的两个顶点位于另外两个顶点所在直线的两侧
// 四个点(0,1,2,3)围成4边形可能的顺序有3种情况:0123 0213 0132
//
#include<stdio.h>
#include<math.h>
#define ESP (1e-6)
double x[4]={0.0},y[4]={0.0};
double A[6],B[6],C[6];//直线方程 Ax+By+C=0
double D[6],E[6];//D、E保存另外两点带入直线方程的值,为零表示点在直线上,符号相同表示位于直线的同一侧
int pos[6][4]={{0,1,2,3},{0,2,1,3},{0,3,1,2},{1,2,0,3},{1,3,0,2},{2,3,0,1}};//用于数组ABCDEF
char str[6][8]={"0,1,2,3","0,2,1,3","0,3,1,2","1,2,0,3","1,3,0,2","2,3,0,1"};//用于打印输出
void TriangleArea(int a,int b, int c, int d)
{
double S;
if(((x[a]-x[b])*(x[a]-x[c])<0) || ((y[a]-y[b])*(y[a]-y[c])<0))
{
S = fabs(x[b]*y[c]-y[b]*x[c]+x[c]*y[d]-x[d]*y[c]+x[d]*y[b]-x[b]*y[d]);
printf("三点(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)共线,三角形(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)\n面积S=%lf\n",
x[a],y[a],x[b],y[b],x[c],y[c],x[b],y[b],x[c],y[c],x[d],y[d],S);
}
else if(((x[b]-x[a])*(x[b]-x[c])<0) || ((y[b]-y[a])*(y[b]-y[c])<0))
{
S = fabs(x[a]*y[c]-y[a]*x[c]+x[c]*y[d]-x[d]*y[c]+x[d]*y[a]-x[a]*y[d]);
printf("三点(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)共线,三角形(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)\n面积S=%lf\n",
x[a],y[a],x[b],y[b],x[c],y[c],x[a],y[a],x[c],y[c],x[d],y[d],S);
}
else if(((x[c]-x[a])*(x[c]-x[b])<0) || ((y[c]-y[a])*(y[c]-y[b])<0))
{
S = fabs(x[a]*y[c]-y[a]*x[c]+x[c]*y[d]-x[d]*y[c]+x[d]*y[a]-x[a]*y[d]);
printf("三点(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)共线,三角形(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)\n面积S=%lf\n",
x[a],y[a],x[b],y[b],x[c],y[c],x[a],y[a],x[b],y[b],x[d],y[d],S);
}
}
//凹四边形
void AoArea01122330()
{
double S = fabs(x[0]*y[1]-x[1]*y[0]+x[1]*y[2]-x[2]*y[1]+x[2]*y[3]-x[3]*y[2]+x[3]*y[0]-x[0]*y[3])/2;
printf("凹四边形\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\n面积S=%lf\n",
x[0],y[0],x[1],y[1],x[2],y[2],x[3],y[3],S);
}
void AoArea01133220()
{
double S = fabs(x[0]*y[1]-x[1]*y[0]+x[1]*y[3]-x[3]*y[1]+x[3]*y[2]-x[2]*y[3]+x[2]*y[0]-x[0]*y[2])/2;
printf("凹四边形\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\n面积S=%lf\n",
x[0],y[0],x[1],y[1],x[3],y[3],x[2],y[2],S);
}
void AoArea02211330()
{
double S = fabs(x[0]*y[2]-x[2]*y[0]+x[2]*y[1]-x[1]*y[2]+x[1]*y[3]-x[3]*y[1]+x[3]*y[0]-x[0]*y[3])/2;
printf("凹四边形\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\n面积S=%lf\n",
x[0],y[0],x[2],y[2],x[1],y[1],x[3],y[3],S);
}
//凸四边形
void Area01122330()
{
double S = fabs(x[0]*y[1]-x[1]*y[0]+x[1]*y[2]-x[2]*y[1]+x[2]*y[3]-x[3]*y[2]+x[3]*y[0]-x[0]*y[3])/2;
printf("凸四边形\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\n面积S=%lf\n",
x[0],y[0],x[1],y[1],x[2],y[2],x[3],y[3],S);
}
void Area01133220()
{
double S = fabs(x[0]*y[1]-x[1]*y[0]+x[1]*y[3]-x[3]*y[1]+x[3]*y[2]-x[2]*y[3]+x[2]*y[0]-x[0]*y[2])/2;
printf("凸四边形\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\n面积S=%lf\n",
x[0],y[0],x[1],y[1],x[3],y[3],x[2],y[2],S);
}
void Area02211330()
{
double S = fabs(x[0]*y[2]-x[2]*y[0]+x[2]*y[1]-x[1]*y[2]+x[1]*y[3]-x[3]*y[1]+x[3]*y[0]-x[0]*y[3])/2;
printf("凸四边形\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\n面积S=%lf\n",
x[0],y[0],x[2],y[2],x[1],y[1],x[3],y[3],S);
}
int CheckInput()
{
//判断输入的4个点是不同的四个点
if((fabs(x[0]-x[1])<ESP && fabs(y[0]-y[1])<ESP) ||
(fabs(x[0]-x[2])<ESP && fabs(y[0]-y[2])<ESP) ||
(fabs(x[0]-x[3])<ESP && fabs(y[0]-y[3])<ESP) ||
(fabs(x[1]-x[2])<ESP && fabs(y[1]-y[2])<ESP) ||
(fabs(x[1]-x[3])<ESP && fabs(y[1]-y[3])<ESP) ||
(fabs(x[2]-x[3])<ESP && fabs(y[2]-y[3])<ESP))
return 0;
return 1;
}
//判断是否是凸四边形 0123
int Check01122330(double *D, double *E)
{
if(D[0]*E[0]>0 && D[2]*E[2]>0 && D[3]*E[3]>0 && D[5]*E[5]>0)
return 1;
else
return 0;
}
//判断是否是凸四边形 0132
int Check01133220(double *D, double *E)
{
if(D[0]*E[0]>0 && D[1]*E[1]>0 && D[4]*E[4]>0 && D[5]*E[5]>0)
return 1;
else
return 0;
}
//判断是否是凸四边形 0213
int Check02211330(double *D, double *E)
{
if(D[1]*E[1]>0 && D[2]*E[2]>0 && D[3]*E[3]>0 && D[4]*E[4]>0)
return 1;
else
return 0;
}
void Calculate(int p, double *A, double *B, double *C, double *D, double *E)
{
int p1=pos[p][0];
int p2=pos[p][1];
int p3=pos[p][2];
int p4=pos[p][3];
if(fabs(y[p2]-y[p1])<ESP)
{
*A = 0.0;
*B = 1.0;
*C = -y[p1];
}
else if(fabs(x[p2]-x[p1])<ESP)
{
*A = 1.0;
*B = 0.0;
*C = -x[p1];
}
else
{
double t=(x[p2]-x[p1])/(y[p2]-y[p1]);
*A = 1.0;
*B = -t;
*C = t*y[p1]-x[p1];
}
*D = *A * x[p3] + *B * y[p3] + *C;
*E = *A * x[p4] + *B * y[p4] + *C;
}
int main()
{
double t;
int i,ii;
do
{
for(i=0;i<4;i++)
{
printf("Please Input Point %d(x,y):", i+1);
scanf("%lf,%lf", &x[i],&y[i]);
}
}while(CheckInput()==0);//检查输入的是4个不同的点
for(i=0;i<6;i++)
{
Calculate(i,&A[i],&B[i],&C[i],&D[i],&E[i]);
printf("(%s)\tA=%5.2lf\tB=%5.2lf\tC=%5.2lf\tD=%5.2lf\tE=%5.2lf\n", str[i], A[i], B[i], C[i], D[i], E[i]);
}
//判断4点共线
if(fabs(D[0])<ESP && fabs(E[0])<ESP)
{
printf("四点共线,面积为零\n");
return 0;
}
//判断3点共线
if(fabs(D[0])<ESP)
{
//0,1,2三点共线
TriangleArea(0,1,2,3);
return 0;
}
else if(fabs(E[0])<ESP)
{
//0,1,3三点共线
TriangleArea(0,1,3,2);
return 0;
}
else if(fabs(E[1])<ESP)
{
//0,2,3三点共线
TriangleArea(0,2,3,1);
return 0;
}
else if(fabs(E[3])<ESP)
{
//1,2,3三点共线
TriangleArea(1,2,3,0);
return 0;
}
//四边形
if(Check01122330(D,E)==0 && Check01133220(D,E)==0 && Check02211330(D,E)==0)
{
//凹四边形
AoArea01122330();
AoArea01133220();
AoArea02211330();
}
else if(Check01122330(D,E)==1)
{
//凸四边形
Area01122330();
}
else if(Check01133220(D,E)==1)
{
//凸四边形
Area01133220();
}
else if(Check02211330(D,E)==1)
{
//凸四边形
Area02211330();
}
return 0;
}
// 算法说明:
// 凸四边形任意一条边的两个顶点位于另外两个顶点所在直线的同一侧
// 凹四边形有两条边的两个顶点位于另外两个顶点所在直线的两侧
// 四个点(0,1,2,3)围成4边形可能的顺序有3种情况:0123 0213 0132
//
#include<stdio.h>
#include<math.h>
#define ESP (1e-6)
double x[4]={0.0},y[4]={0.0};
double A[6],B[6],C[6];//直线方程 Ax+By+C=0
double D[6],E[6];//D、E保存另外两点带入直线方程的值,为零表示点在直线上,符号相同表示位于直线的同一侧
int pos[6][4]={{0,1,2,3},{0,2,1,3},{0,3,1,2},{1,2,0,3},{1,3,0,2},{2,3,0,1}};//用于数组ABCDEF
char str[6][8]={"0,1,2,3","0,2,1,3","0,3,1,2","1,2,0,3","1,3,0,2","2,3,0,1"};//用于打印输出
void TriangleArea(int a,int b, int c, int d)
{
double S;
if(((x[a]-x[b])*(x[a]-x[c])<0) || ((y[a]-y[b])*(y[a]-y[c])<0))
{
S = fabs(x[b]*y[c]-y[b]*x[c]+x[c]*y[d]-x[d]*y[c]+x[d]*y[b]-x[b]*y[d]);
printf("三点(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)共线,三角形(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)\n面积S=%lf\n",
x[a],y[a],x[b],y[b],x[c],y[c],x[b],y[b],x[c],y[c],x[d],y[d],S);
}
else if(((x[b]-x[a])*(x[b]-x[c])<0) || ((y[b]-y[a])*(y[b]-y[c])<0))
{
S = fabs(x[a]*y[c]-y[a]*x[c]+x[c]*y[d]-x[d]*y[c]+x[d]*y[a]-x[a]*y[d]);
printf("三点(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)共线,三角形(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)\n面积S=%lf\n",
x[a],y[a],x[b],y[b],x[c],y[c],x[a],y[a],x[c],y[c],x[d],y[d],S);
}
else if(((x[c]-x[a])*(x[c]-x[b])<0) || ((y[c]-y[a])*(y[c]-y[b])<0))
{
S = fabs(x[a]*y[c]-y[a]*x[c]+x[c]*y[d]-x[d]*y[c]+x[d]*y[a]-x[a]*y[d]);
printf("三点(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)共线,三角形(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)(%5.2lf,%5.2lf)\n面积S=%lf\n",
x[a],y[a],x[b],y[b],x[c],y[c],x[a],y[a],x[b],y[b],x[d],y[d],S);
}
}
//凹四边形
void AoArea01122330()
{
double S = fabs(x[0]*y[1]-x[1]*y[0]+x[1]*y[2]-x[2]*y[1]+x[2]*y[3]-x[3]*y[2]+x[3]*y[0]-x[0]*y[3])/2;
printf("凹四边形\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\n面积S=%lf\n",
x[0],y[0],x[1],y[1],x[2],y[2],x[3],y[3],S);
}
void AoArea01133220()
{
double S = fabs(x[0]*y[1]-x[1]*y[0]+x[1]*y[3]-x[3]*y[1]+x[3]*y[2]-x[2]*y[3]+x[2]*y[0]-x[0]*y[2])/2;
printf("凹四边形\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\n面积S=%lf\n",
x[0],y[0],x[1],y[1],x[3],y[3],x[2],y[2],S);
}
void AoArea02211330()
{
double S = fabs(x[0]*y[2]-x[2]*y[0]+x[2]*y[1]-x[1]*y[2]+x[1]*y[3]-x[3]*y[1]+x[3]*y[0]-x[0]*y[3])/2;
printf("凹四边形\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\n面积S=%lf\n",
x[0],y[0],x[2],y[2],x[1],y[1],x[3],y[3],S);
}
//凸四边形
void Area01122330()
{
double S = fabs(x[0]*y[1]-x[1]*y[0]+x[1]*y[2]-x[2]*y[1]+x[2]*y[3]-x[3]*y[2]+x[3]*y[0]-x[0]*y[3])/2;
printf("凸四边形\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\n面积S=%lf\n",
x[0],y[0],x[1],y[1],x[2],y[2],x[3],y[3],S);
}
void Area01133220()
{
double S = fabs(x[0]*y[1]-x[1]*y[0]+x[1]*y[3]-x[3]*y[1]+x[3]*y[2]-x[2]*y[3]+x[2]*y[0]-x[0]*y[2])/2;
printf("凸四边形\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\n面积S=%lf\n",
x[0],y[0],x[1],y[1],x[3],y[3],x[2],y[2],S);
}
void Area02211330()
{
double S = fabs(x[0]*y[2]-x[2]*y[0]+x[2]*y[1]-x[1]*y[2]+x[1]*y[3]-x[3]*y[1]+x[3]*y[0]-x[0]*y[3])/2;
printf("凸四边形\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\t(%5.2lf,%5.2lf)\n面积S=%lf\n",
x[0],y[0],x[2],y[2],x[1],y[1],x[3],y[3],S);
}
int CheckInput()
{
//判断输入的4个点是不同的四个点
if((fabs(x[0]-x[1])<ESP && fabs(y[0]-y[1])<ESP) ||
(fabs(x[0]-x[2])<ESP && fabs(y[0]-y[2])<ESP) ||
(fabs(x[0]-x[3])<ESP && fabs(y[0]-y[3])<ESP) ||
(fabs(x[1]-x[2])<ESP && fabs(y[1]-y[2])<ESP) ||
(fabs(x[1]-x[3])<ESP && fabs(y[1]-y[3])<ESP) ||
(fabs(x[2]-x[3])<ESP && fabs(y[2]-y[3])<ESP))
return 0;
return 1;
}
//判断是否是凸四边形 0123
int Check01122330(double *D, double *E)
{
if(D[0]*E[0]>0 && D[2]*E[2]>0 && D[3]*E[3]>0 && D[5]*E[5]>0)
return 1;
else
return 0;
}
//判断是否是凸四边形 0132
int Check01133220(double *D, double *E)
{
if(D[0]*E[0]>0 && D[1]*E[1]>0 && D[4]*E[4]>0 && D[5]*E[5]>0)
return 1;
else
return 0;
}
//判断是否是凸四边形 0213
int Check02211330(double *D, double *E)
{
if(D[1]*E[1]>0 && D[2]*E[2]>0 && D[3]*E[3]>0 && D[4]*E[4]>0)
return 1;
else
return 0;
}
void Calculate(int p, double *A, double *B, double *C, double *D, double *E)
{
int p1=pos[p][0];
int p2=pos[p][1];
int p3=pos[p][2];
int p4=pos[p][3];
if(fabs(y[p2]-y[p1])<ESP)
{
*A = 0.0;
*B = 1.0;
*C = -y[p1];
}
else if(fabs(x[p2]-x[p1])<ESP)
{
*A = 1.0;
*B = 0.0;
*C = -x[p1];
}
else
{
double t=(x[p2]-x[p1])/(y[p2]-y[p1]);
*A = 1.0;
*B = -t;
*C = t*y[p1]-x[p1];
}
*D = *A * x[p3] + *B * y[p3] + *C;
*E = *A * x[p4] + *B * y[p4] + *C;
}
int main()
{
double t;
int i,ii;
do
{
for(i=0;i<4;i++)
{
printf("Please Input Point %d(x,y):", i+1);
scanf("%lf,%lf", &x[i],&y[i]);
}
}while(CheckInput()==0);//检查输入的是4个不同的点
for(i=0;i<6;i++)
{
Calculate(i,&A[i],&B[i],&C[i],&D[i],&E[i]);
printf("(%s)\tA=%5.2lf\tB=%5.2lf\tC=%5.2lf\tD=%5.2lf\tE=%5.2lf\n", str[i], A[i], B[i], C[i], D[i], E[i]);
}
//判断4点共线
if(fabs(D[0])<ESP && fabs(E[0])<ESP)
{
printf("四点共线,面积为零\n");
return 0;
}
//判断3点共线
if(fabs(D[0])<ESP)
{
//0,1,2三点共线
TriangleArea(0,1,2,3);
return 0;
}
else if(fabs(E[0])<ESP)
{
//0,1,3三点共线
TriangleArea(0,1,3,2);
return 0;
}
else if(fabs(E[1])<ESP)
{
//0,2,3三点共线
TriangleArea(0,2,3,1);
return 0;
}
else if(fabs(E[3])<ESP)
{
//1,2,3三点共线
TriangleArea(1,2,3,0);
return 0;
}
//四边形
if(Check01122330(D,E)==0 && Check01133220(D,E)==0 && Check02211330(D,E)==0)
{
//凹四边形
AoArea01122330();
AoArea01133220();
AoArea02211330();
}
else if(Check01122330(D,E)==1)
{
//凸四边形
Area01122330();
}
else if(Check01133220(D,E)==1)
{
//凸四边形
Area01133220();
}
else if(Check02211330(D,E)==1)
{
//凸四边形
Area02211330();
}
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没看懂你说的
追问
任意输入4个点坐标,求这四个点围成的四边形面积
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询