求出r、p、q,然后求叉积,取绝对值再除以2就行了。第一次感受到lrj书上模板的威力,感觉好神奇!
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
struct point
{
double x,y;
point(double x=0,double y=0):x(x),y(y){}
};
point operator+(point a,point b) {return point(a.x+b.x,a.y+b.y);}
point operator-(point a,point b) {return point(a.x-b.x,a.y-b.y);}
point operator*(point a,double p) {return point(a.x*p,a.y*p);}
point operator/(point a,double p) {return point(a.x/p,a.y/p);}
const double eps=1e-10;
int dcmp(double x)
{
if(fabs(x)<eps) return 0;
else return x<0?-1:1;
}
bool operator==(const point& a,const point& b)
{
return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
double cross(point a,point b)
{
return a.x*b.y-a.y*b.x;
}
point getpoint(point p,point v,point q,point w)
{
point u=p-q;
double t=cross(w,u)/cross(v,w);
return p+v*t;
}
int main()
{
freopen("in.txt","r",stdin);
int T;
cin>>T;
while(T--)
{
double x1,y1,x2,y2,x3,y3;
cin>>x1>>y1>>x2>>y2>>x3>>y3;
point a=point(x1,y1);
point b=point(x2,y2);
point c=point(x3,y3);
point e=point((x1+2*x3)/3,(y1+2*y3)/3);
point d=point((x3+2*x2)/3,(y3+2*y2)/3);
point f=point((x2+2*x1)/3,(y2+2*y1)/3);
point r=getpoint(c,f-c,a,d-a);
point p=getpoint(a,d-a,b,e-b);
point q=getpoint(b,e-b,c,f-c);
double area=fabs(cross(p-q,r-q))/2;
printf("%.0f\n",area);
}
return 0;
}