转载请注明出处:忆梦http://blog.csdn.net/yimeng2013/article/details/17329917
计算几何入门题,熟悉使用模板。
题意:求出三角形PQR的面积。
方法一: 几何证明面积关系求解:http://blog.csdn.net/freezhanacmore/article/details/8171942
方法二: 利用叉积直接求面积:
#include<cstdio>
#include<cmath>
//定义点
struct Point
{
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
};
typedef Point Vector; //Vector 为 Point的别名
//向量+向量=向量 点+向量=点
Vector operator + (Vector A, Vector B) {return Vector(A.x+B.x, A.y+B.y);}
//点-点=向量
Vector operator - (Point A, Point B) {return Vector(A.x-B.x, A.y-B.y);}
//向量*数=向量
Vector operator * (Vector A, double p) {return Vector(A.x*p, A.y*p);}
//向量/数=向量
Vector operator / (Vector A, double p) {return Vector(A.x/p, A.y/p);}
bool operator < (const Point & a, const Point & b)
{
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
//点积:两者长度乘积在乘上夹角余弦 XaXb + YaYb
double Dot(Vector A, Vector B)
{
return A.x*B.x + A.y*B.y;
}
double Length(Vector A)
{
return sqrt(Dot(A, A));
}
double Angle(Vector A, Vector B)
{
return acos(Dot(A, B) / Length(A) / Length(B));
}
//叉积:两向量v和w的叉积等于v和w组成的三角形的有向面积的两倍 XaYb - XbYa
double Cross(Vector A, Vector B)
{
return A.x*B.y - A.y*B.x;
}
double Area2(Point A, Point B, Point C)
{
return Cross(B-A, C-A);
}
//直线一般用参数表示 P = P0 + tv
//如何已知直线上两点A,B ;则方向向量为B-A,所以参数方程为A+(B-A)t
//两直线交点
Point GetLineIntersection(Point P, Vector v, Point Q, Vector w)
{
Vector u = P - Q;
double t = Cross(w, u) / Cross(v, w);
return P+v*t;
}
Point read_point()
{
Point temp;
scanf("%lf %lf", &temp.x, &temp.y);
return temp;
}
Point GetD(Point A, Point B)
{
return (B-A)/3.0 + A;
}
int main ()
{
int T;
scanf("%d", &T);
while(T--)
{
Point A, B, C, D, E, F, P, Q, R;
A = read_point();
B = read_point();
C = read_point();
D = GetD(B, C);
E = GetD(C, A);
F = GetD(A, B);
P = GetLineIntersection(A, D-A, B, E-B);
R = GetLineIntersection(A, D-A, C, F-C);
Q = GetLineIntersection(B, E-B, C, F-C);
double ans = Area2(P, R, Q) / 2.0;
printf("%.0lf\n", fabs(ans));
}
return 0;
}