UVA 11437 - Triangle Fun(计算几何)

这题只要根据题目,利用向量把点一个个求出来计算面积即可

不过据说有一种证明方法可以证明面积是1/7的原三角形

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

int t;

struct Point {
    double x, y;
    Point() {}
    Point(double x, double y) {
        this->x = x;
        this->y = y;
    }
    void read() {
        scanf("%lf%lf", &x, &y);
    }
};

typedef Point Vector;

Vector operator + (Vector A, Vector B) {
    return Vector(A.x + B.x, A.y + B.y);
}

Vector operator - (Vector A, Vector 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;} //叉积
double Area2(Point A, Point B, Point C) {return Cross(B - A, C - A);} //有向面积

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;
}

int main() {
    scanf("%d", &t);
    while (t--) {
        Point A, B, C;
        A.read(); B.read(); C.read();
        Point D, E, F;
        D = (C - B) / 3 + B;
        E = (A - C) / 3 + C;
        F = (B - A) / 3 + A;
        Point R = GetLineIntersection(C, F - C, A, D - A);
        Point P = GetLineIntersection(B, E - B, A, D - A);
        Point Q = GetLineIntersection(C, F - C, B, E - B);
        printf("%d\n", (int)(fabs(Area2(P, Q, R)) / 2 + 0.5));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值