码迷,mamicode.com
首页 > 其他好文 > 详细

uva 11437

时间:2015-08-01 10:04:58      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

题意:给出一个三角形的三点,然后取三边的三等分点和相对的顶点连线,问围起来的三角形的面积。
题解:把CF、AD、BE三个向量先求出来,然后两两取交点,最后用叉积求面积,最后要四舍五入。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double PI = acos(-1);
const double eps = 1e-9;

struct Point {
    double x, y;
    Point(double x = 0, double y = 0): x(x), y(y) {}
};
typedef Point Vector;

double Sqr(double x) {
    return x * x;
}
double dcmp(double x) {
    if (fabs(x) < eps)
        return 0;
    return x < 0 ? -1 : 1;
}
Vector operator + (const Point& A, const Point& B) {
    return Vector(A.x + B.x, A.y + B.y);
}
Vector operator - (const Point& A, const Point& B) {
    return Vector(A.x - B.x, A.y - B.y);
}
Vector operator * (const Point& A, double a) {
    return Vector(A.x * a, A.y * a);
}
Vector operator / (const Point& A, double a) {
    return Vector(A.x / a, A.y / a);
}
double Cross(const Vector& A, const Vector& B) {
    return A.x * B.y - A.y * B.x;
}
double Dot(const Vector& A, const Vector& B) {
    return A.x * B.x + A.y * B.y;
}
double Length(const Vector& A) {
    return sqrt(Dot(A, A));
}
bool operator < (const Point& A, const Point& B) {
    return A.x < B.x || (A.x == B.x && A.y < B.y);
}
bool operator == (const Point& A, const Point& B) {
    return A.x == B.x && A.y == B.y;
}
//得到两直线交点 
Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) {
    Point u = P - Q;
    double t = Cross(w, u) / Cross(v, w);
    return P + v * t;
}
Point A, B, C, D, E, F;

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        scanf("%lf%lf%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y, &C.x, &C.y);
        Vector CF = Vector((B - C) + (A - B) * 2.0 / 3);
        Vector BE = Vector((A - B) + (C - A) * 2.0 / 3);
        Vector AD = Vector((C - A) + (B - C) * 2.0 / 3);
        Point P = GetLineIntersection(B, BE, A, AD);
        Point R = GetLineIntersection(C, CF, A, AD);
        Point Q = GetLineIntersection(B, BE, C, CF);
        double res = fabs(Cross(Q - P, R - P));
        printf("%lld\n", (long long)(res / 2.0 + 0.5));
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

uva 11437

标签:

原文地址:http://blog.csdn.net/hyczms/article/details/47183629

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!