当前位置:首页 » 编程语言 » c语言判断一个四边形凹凸性
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言判断一个四边形凹凸性

发布时间: 2023-07-03 02:22:07

c语言编程题 题目:任意输入4个点,求围成四边形的面积

我测试过了,四点一线,凹四边形,四边形,有两个点共点,都可以,这里使用一条知道N边形N个顶点坐标求N边形的面积的公式,这些情况其实已经虚冲察可以不考虑,呵呵,自动求

凹四边形情况:

#include"stdio.h"

#include"math.h"

voidmain()

{

doublex[4],y[4];

for(inti=0;i<4;i++)

{

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

}

doublemianji=0.0;

for(intii差茄=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 ",mianji);

}

② C语言编程题 题目:任意输入4个点,求围成四边形的面积

//
// 算法说明:
// 凸四边形任意一条边的两个顶点位于另外两个顶点所在直线的同一侧
// 凹四边形有两条边的两个顶点位于另外两个顶点所在直线的两侧
// 四个点(0,1,2,3)围成4边形可能的顺序有3种情况:0123 0213 0132
//
#include
#include
#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 AoArea0230()
{
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 Area0230()
{
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 Check0230(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 && Check0230(D,E)==0)
{
//凹四边形
AoArea01122330();
AoArea01133220();
AoArea0230();
}
else if(Check01122330(D,E)==1)
{
//凸四边形
Area01122330();
}
else if(Check01133220(D,E)==1)
{
//凸四边形
Area01133220();
}
else if(Check0230(D,E)==1)
{
//凸四边形
Area0230();
}
return 0;
}

③ C语言怎么判断4个坐标坐标是否构成矩形

代码:

#include <stdio.h>


typedef struct {

double x;

double y;

}point;



int is_rectangle(point a,point b,point c,point d){

int ab_y = a.y - b.y;

int ab_x = a.x - b.x;

int cd_y = c.y - d.y;

int cd_x = c.x - d.x;

// 首先判断ab是否平行于cd

if (ab_x == 0 || cd_x == 0){

if(ab_x != cd_x){

return 0;

}

}else{

if(ab_y/ab_x != cd_y/cd_x){

return 0;

}

}



// 再判断ab是否等于cd

if(ab_y*ab_y + ab_x*ab_x != cd_y*cd_y + cd_x*cd_x){

return 0;

}


// 然后判断ab是否垂直于ac

int ac_y = a.y - c.y;

int ac_x = a.x - c.x;

if(ab_x == 0 ){

if(ac_y != 0)

return 0;

}else if(ac_x == 0){

if(ab_y != 0)

return 0;

}else if((ac_y/ac_x)*(ab_y/ab_x) != -1){

return 0;

}

return 1;

}


int main(){

printf("输入4个坐标值: ");

point a,b,c,d;

scanf("%lf %lf",&a.x,&a.y);

scanf("%lf %lf",&b.x,&b.y);

scanf("%lf %lf",&c.x,&c.y);

scanf("%lf %lf",&d.x,&d.y);

if(is_rectangle(a,b,c,d)){

printf("能构成矩形 ");

}else{

printf("无法构成矩形 ");

}

return 0;

}

运行结果:

④ c语言课程设计关于四边形证明

/*
AC中点M,BD中点N,BA、CD延长后交于R
验证area(ABCD)==4*area(RMN)
*/#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define EPS 1e-10typedef struct
{
double x;
double y;
} Point;/*两点之间的直线距离*/
double length(Point *A,Point *B)
{
return sqrt(pow(A->x-B->x,2)+pow(A->y-B->y,2));
}double area_of_triangle(Point A,Point B,Point C)
{
double area,a,b,c,p;
a=length(&B,&C);
b=length(&C,&A);
c=length(&A,&B);
p=(a+b+c)/2;
area=sqrt(p*(p-a)*(p-b)*(p-c));
return area;
}/*初步检测四边形*/
int ok(Point *p)
{
double side[4];/*4边长*/
int i,j;
for(i=0;i<4;i++)
{
double t;
j=(i+1)%4;
t=length(&p[i],&p[j]);
if(fabs(t)<EPS)return 0; /*不允许有重合在一起的点*/
side[i]=t;
}

{
double k1,k2;/*斜率*/
k1=(p[1].y-p[0].y)/(p[1].x-p[0].x);
k2=(p[3].y-p[2].y)/(p[3].x-p[2].x);
if(fabs(k2-k1)<EPS)
return 0; /*不允许AB与CD近乎平行*/
}return 1;
}#define a P[0]
#define b P[1]
#define c P[2]
#define d P[3]main()
{
char pname[]="ABCD",*p=pname;
Point m,n,r,P[4],*PP=P;
double abcd,rmn,abcd1;
while(*p)
{
printf("input (x,y) of point %c:",*p);
scanf("%lf%*c%lf",&PP->x,&PP->y);
p++;PP++;
} /*至此得到四边形的4个顶点的坐标*/ if(!ok(P))/*如果AB//CD则无法相交只好退出*/
{
fprintf(stderr,"invalid input...\n\n");
exit(1); /*异常结束1*/
}/*推算M、N两个“中点”的坐标*/
m.x = (a.x+c.x)/2;
m.y = (a.y+c.y)/2;
n.x = (b.x+d.x)/2;
n.y = (b.y+d.y)/2;/*这里计算BA与CD延长后的交点坐标*/
{
double cdx,cdy,abx,aby,dax,day, del,,dv;
cdx = c.x-d.x;
cdy = c.y-d.y;
abx = a.x-b.x;
aby = a.y-b.y;
dax = d.x-a.x;
day = d.y-a.y;
del = cdx*aby-cdy*abx;
= dax*aby-day*abx;
dv = cdx*day-cdy*dax;
/= del;
dv /= del;
r.x = d.x-*cdx;
r.y = d.y-*cdy;
}/*这里计算四边形ABCD的面积*/
abcd = area_of_triangle(a,b,c) + area_of_triangle(a,d,c);
/*补充检测四边形,看它的凹凸性*/
abcd1= area_of_triangle(a,b,d) + area_of_triangle(b,d,c);
if(fabs(abcd1-abcd)>EPS)
{
fprintf(stderr,"凹四边形!不行!\n\n");
exit(2); /*异常结束2*/
}/*这里计算△RMN的面积*/
rmn = area_of_triangle(r,m,n);/*这里是二者相除,看它是否非常接近于4.00*/
printf("as a result\nABCD:RMN=%4.2f:%4.2f=%4.2f\n"
,abcd,rmn,abcd/rmn);}/*运行情况举例
input (x,y) of point A:0,0
input (x,y) of point B:2,0
input (x,y) of point C:1.5,1.5
input (x,y) of point D:0.5,1
as a result
ABCD:RMN=1.87:0.47=4.00
Press any key to continue
*/

参考于: http://bbs.bccn.net/thread-75642-2-1.html

⑤ 求用c语言写一个判断凹凸四边形的代码。

你可以参考下面的方法:

.判断一个封闭图形是凹集还是凸集
语法:result=convex(Point *p,int n);
参数:
*p: 封闭曲线顶点数组
n: 封闭曲线顶点个数
返回值: 1:凸集;-1:凹集;0:曲线不符合要求无法计算
注意:
默认曲线为简单曲线:无交叉、无圈
源程序:

typedef struct {
double x,y;
} Point;
int convex(Point *p,int n)
{
int i,j,k;
int flag = 0;
double z;
if (n < 3)
return(0);
for (i=0;i<n;i++) {
j = (i + 1) % n;
k = (i + 2) % n;
z = (p[j].x - p[i].x) * (p[k].y - p[j].y);
z -= (p[j].y - p[i].y) * (p[k].x - p[j].x);
if (z < 0)
flag |= 1;
else if (z > 0)
flag |= 2;
if (flag == 3)
return -1; //CONCAVE
}
if (flag != 0)
return 1; //CONVEX
else
return 0;
}