① 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;
}