uva 11437 赛瓦定理与向量法

#include<stdio.h>
int main()
{
    int n;
    double Ax,Ay,Bx,By,Cx,Cy;
    double are;
    while(scanf("%d",&n)!=EOF)
    {
        while(n--)
        {
            scanf("%lf%lf%lf%lf%lf%lf",&Ax,&Ay,&Bx,&By,&Cx,&Cy);
            are=(Ax*By+Bx*Cy+Cx*Ay-Ay*Bx-By*Cx-Cy*Ax)/(2*7);
            are>=0?are:-are;
            printf("%.0lf\n",are);
        }
    }
    return 0;
}


设三角形为正三角形很快推出公式。


向量法:



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


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

向量法的解法来自:http://blog.csdn.net/hyczms/article/details/47183629,提供了另一种思路。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值