UVA 11437 Triangle Fun 几何基础

来源:互联网 发布:苹果电脑系统删除软件 编辑:程序博客网 时间:2024/05/28 11:50

点击打开题目链接

几何基础

#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)非0Point 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;}


0 0
原创粉丝点击