蓝桥杯 三角形面积(计算几何叉积)


标题:三角形面积

已知三角形三个顶点在直角坐标系下的坐标分别为:
(2.3, 2.5)
(6.4, 3.1)
(5.1, 7.2)

求该三角形的面积。

注意,要提交的是一个小数形式表示的浮点数。
要求精确到小数后3位,如不足3位,需要补零。

利用叉积计算三角形面积即可。

// #pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#include <set>
#include <cctype>
#include <bitset>
#define IO                       \
    ios::sync_with_stdio(false); \
    // cout.tie(0);
using namespace std;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 1e5 + 10;
const int maxm = 2e5 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);
//int dis[4][2] = {1, 0, 0, -1, 0, 1, -1, 0};
int dis[2][2] = {1, 0, 0, 1};
//int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int cmp(double x)
{
    if (fabs(x) < eps)
        return 0;
    if (x > 0)
        return 1;
    return -1;
}
inline double sqr(double x)
{
    return x * x;
}
struct Point
{
    double x, y;
    Point() {}
    Point(double a, double b) : x(a), y(b) {}
    friend Point operator+(const Point &a, const Point &b)
    {
        return Point(a.x + b.x, a.y + b.y);
    }
    friend Point operator-(const Point &a, const Point &b)
    {
        return Point(a.x - b.x, a.y - b.y);
    }
    friend bool operator==(const Point &a, const Point &b)
    {
        return cmp(a.x - b.x) == 0 && cmp(a.y - b.y) == 0;
    }
    friend Point operator*(const double &a, const Point &b)
    {
        return Point(a * b.x, a * b.y);
    }
    friend Point operator*(const Point &a, const Point &b)
    {
        return Point(a.x * b.x, a.y * b.y);
    }
    friend Point operator/(const Point &a, const double &b)
    {
        return Point(a.x / b, a.y / b);
    }
    double norm()
    {
        return sqrt(sqr(x) + sqr(y));
    }
} a, b, c;

double dist(const Point &a, const Point &b) // ?????
{
    return (a - b).norm();
}
double dot(const Point &a, const Point &b) // ??
{
    return a.x * b.x + a.y * b.y;
}
double det(const Point &a, const Point &b) // ??
{
    return a.x * b.y - a.y * b.x;
}
int main()
{
#ifdef WXY
    freopen("in.txt", "r", stdin);
//	 freopen("out.txt", "w", stdout);
#endif
    IO;
    double x, y;
    cin >> x >> y;
    a = Point(x, y);
    cin >> x >> y;
    b = Point(x, y);
    cin >> x >> y;
    c = Point(x, y);
    double s = det(c - a, c - b) / 2.0;
    printf("%.3lf", s);

    return 0;
}


 

猜你喜欢

转载自blog.csdn.net/qq_44115065/article/details/109098373