http://acm.hdu.edu.cn/showproblem.php?pid=1071
The area
Problem Description
Ignatius bought a land last week, but he didn't know the area of the land because the land is enclosed by a parabola and a straight line. The picture below shows the area. Now given all the intersectant points shows in the picture, can you tell Ignatius the area of the land?
Note: The point P1 in the picture is the vertex of the parabola.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains three intersectant points which shows in the picture, they are given in the order of P1, P2, P3. Each point is described by two floating-point numbers X and Y(0.0<=X,Y<=1000.0).
Each test case contains three intersectant points which shows in the picture, they are given in the order of P1, P2, P3. Each point is described by two floating-point numbers X and Y(0.0<=X,Y<=1000.0).
Output
For each test case, you should output the area of the land, the result should be rounded to 2 decimal places.
Sample Input
2
5.000000 5.000000
0.000000 0.000000
0.000000 0.000000
10.000000 10.000000
1.000000 1.000000
14.000000 8.222222
Sample Output
33.33
40.69
Hint
For float may be not accurate enough, please use double instead of float.Author
Ignatius.L
我的理解
最好的方法应该用微积分,可高数还没学到,所以就写了个二分法的程序。
把区域面积分成一个大三角形(p1,p2,p3构成的)的面积加上两个弓形(p1,p2构成的和p2,p3构成的)的面积。求弓形面积时,再把他分成一个三角形加两个弓形(求弓形p1,p2时,在p1,p2之间取一点p4(横坐标为p1,p2的中点),面积就等于三角形(p4,p1,p2构成的)加上两个小弓形的面积(p1,p4构成的和p4,p2构成的),一直递归调用函数直到弓形面积小到可以忽略不计。
我的代码
1 #include <stdio.h>
2 #include <math.h>
3 const double e=1e-3; //控制精度
4 double a,h,d;
5 double fx(double x)
6 {
7 return a*(x-h)*(x-h)+d;
8 }
9 double area(double x1,double y1,double x2,double y2,double x3,double y3)
10 {
11 double d1,d2,d3,p,s,x4,y4;
12 d1=sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
13 if (d1<e) return 0; //精度有限
14 d2=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
15 d3=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); //求出3条边长
16 p=(d1+d2+d3)/2;
17 s=sqrt(p*(p-d1)*(p-d2)*(p-d3)); //海伦公式求三角形面积
18 x4=(x1+x2)/2; y4=fx(x4);
19 s+=area(x4,y4,x1,y1,x2,y2);
20 x4=(x1+x3)/2; y4=fx(x4);
21 s+=area(x4,y4,x1,y1,x3,y3); //二分法 求余下的面积
22 return s;
23 }
24 int main()
25 {
26 double x1,y1,x2,y2,x3,y3,s;
27 int n,i;
28 scanf("%d",&n);
29 for (i=0;i<n;i++)
30 {
31 scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3);
32 h=x1; d=y1;
33 a=(y2-d)/(x2-h)/(x2-h); //求二次函数解析式
34 s=area(x1,y1,x2,y2,x3,y3);
35 printf("%.2lf\n",s);
36 }
37 }