您的位置:首页 > 其它

UVA 11437 Triangle Fun 几何基础

2016-05-13 17:45 471 查看
点击打开题目链接

几何基础

#include <cstdio>
#include <cmath>
#include <iostream>
using namespace std;

struct Point //定义点
{
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {} //构造函数,方便代码编写
};

typedef Point Vector; //向量是点的一个别名
//重载 +,-,* 运算符
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);
}
//叉积,两向量组成的三角形的有向面积的两倍
double Cross(Vector A, Vector B)
{
return A.x * B.y - A.y * B.x;
}
//直线交点,调用前确保两条直线P+tv和Q+tw有唯一交点。当且仅当Cross(v,w)非0
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;
}
//三角形有向面积的两倍
double Area2(Point A, Point B, Point C)
{
return Cross(B - A, C - A);
}

Point getD(Point A, Point B, Point C)
{
Point D = B + (C - B) / 3; //点D
Vector AD = D - A; //线段AD

Point E = C + (A - C) / 3; //点E
Vector BE = E - B; //线段BE

return GetLineIntersection(A, AD, B, BE); //交点P
}

Point read_point()
{
Point p;
scanf("%lf%lf", &p.x, &p.y);
return p;
}

int main()
{
int T;
Point A, B, C, P, Q, R;
scanf("%d", &T);
while (T--)
{
A = read_point();
B = read_point();
C = read_point();
P = getD(A, B, C);
Q = getD(B, C, A);
R = getD(C, A, B);
double ans = fabs(Area2(P, Q, R)) / 2;
printf("%.0lf\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm 几何