The area
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7160 Accepted Submission(s): 5031
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.
Note: The point P1 in the picture is the vertex of the parabola.
![](https://i-blog.csdnimg.cn/blog_migrate/91dc24a066eaa3cddad6393bc62ad561.png)
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 10.000000 0.000000 10.000000 10.000000 1.000000 1.000000 14.000000 8.222222
Sample Output
33.33 40.69HintFor float may be not accurate enough, please use double instead of float.
【题意】就是让你求出抛物线与直线闭合区域的面积;
【题解】可以将它化作许多小三角形,求出面积之和。因为只需两位小数,所以精度还过的去;
程序里包括了一些求解抛物线、直线等的步骤,看不懂可以自己推。
#include<cstdio>
#include<cmath>
double a,b,c,k,p,q,l;
//求抛物线解析式
void parabola(double x1,double y1,double x2,double y2)
{
a=(y2-y1)/(x2*x2-2*x1*x2+x1*x1);
b=-2.0*a*x1;
c=a*x1*x1+y1;
}
//求直线解析式
void straight(double x1,double y1,double x2,double y2)
{
k=(y2-y1)/(x2-x1);
p=y1-k*x1;
if(k!=0)
q=(-p/k)/(sqrt(p*p+(-p/k)*(-p/k)));
else q=1;
}
//求三角形的高;
double yc(double x,double x3,double y3)
{
double kl=a*x*x+b*x+c,h;
l=sqrt((x3-x)*(x3-x)+(y3-kl)*(y3-kl));
h=((kl-k*x)-p)*q;
if(h<0)h=-h;
straight(x3,y3,x,kl);
return h;
}
int main()
{
int t;scanf("%d",&t);
double x1,y1,x2,y2,x3,y3,high,len,area;
for(int i=1;i<=t;i++)
{
area=0;
scanf("%lf%lf",&x1,&y1);
scanf("%lf%lf",&x2,&y2);
scanf("%lf%lf",&x3,&y3);
parabola(x1,y1,x3,y3);
straight(x2,y2,x3,y3);
len=sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
for(double w=x2+0.001;w<x3;w+=0.001)
{
high=yc(w,x3,y3);
area+=high*len/2.0;
len=l;
}
printf("%.2lf\n",area);
}
// for(;;);
return 0;
}