/*
AC中点M,BD中点N,BA、CD延长后交于R
验证area(ABCD)==4*area(RMN)
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define EPS 1e-10
typedef struct
{
double x;
double y;
} Point;
#define a P[0]
#define b P[1]
#define c P[2]
#define d P[3]
/*两点之间的几何长度*/
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=0;
/*已经快要成功啦*/
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;
}
main()
{
char pname[]="ABCD",*p=pname;
Point m,n,r,P[4],*PP=P;
double abcd,rmn;
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,"\ninvalid input...\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,du,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;
du = dax*aby-day*abx;
dv = cdx*day-cdy*dax;
du /= del;
dv /= del;
r.x = d.x-du*cdx;
r.y = d.y-du*cdy;
}
/*这里计算四边形ABCD的面积*/
abcd = area_of_triangle(a,b,c) + area_of_triangle(a,d,c);
/*这里计算△RMN的面积*/
rmn = area_of_triangle(r,m,n);
/*这里是二者相除,看它是否非常接近于4.00*/
scanf("as a result\nABCD : RMN = %4.2f\n",abcd/rmn);
}