/*
AC中点M,BD中点N,BA、CD延长后交于R
验证area(ABCD)==4*area(RMN)
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef struct
{
double x;
double y;
} Point;
#define a P[0]
#define b P[1]
#define c P[2]
#define d P[3]
double area_of_triangle(Point A,Point B,Point C)
{
double area=0;
/* ...... */
return area;
}
int ok(Point *p)
{ /*检测是否平行四边形,若是返回0*/
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*/
printf("as a result\nABCD : RMN = %4.2f\n",abcd/rmn);
}
[此贴子已经被作者于2006-6-29 20:23:27编辑过]