【计算几何】点与三角形关系

本文介绍了一种计算几何中的经典问题:如何判断一个点是否位于一个给定的三角形内部,包括边界上的情况。通过计算三角形各部分面积之和与整体面积的比较,实现点与三角形关系的快速判断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【计算几何】点与三角形关系

时间限制: 1 Sec 内存限制: 128 MB

题目描述

已知一个平面坐标系中三角形三个点 A、B、CA、B、CABC 的坐标,判断另外一个点 DDD 是否在三角形内(点在三角形边上也认为在三角形内)

输入

输入共四行,每行两个数,前三行表示 A、B、CA、B、CABC 的坐标,第四行为 DDD 的坐标。

输出

输出一个字符串,in表示点D在三角形 ABCABCABC 内,out表示点 DDD 在三角形 ABCABCABC 外。

样例输入

1.0 2.0
7.0 1.0
7.0 5.0
5.0 3.0

样例输出

in

思路

判断三角形DAB、DBC、DCA面积之和与三角形ABC面积的关系,若相等在里面,否则,在外面。

#pragma GCC optimize(3,"Ofast","inline")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <algorithm>
#include <unordered_map>
#include <vector>
using namespace std;
#define ls (rt<<1)
#define rs (rt<<1|1)
typedef long long ll;
template <typename T>
inline void read(T &x) {
    x = 0;
    static int p;
    p = 1;
    static char c;
    c = getchar();
    while (!isdigit(c)) {
        if (c == '-')p = -1;
        c = getchar();
    }
    while (isdigit(c)) {
        x = (x << 1) + (x << 3) + (c - 48);
        c = getchar();
    }
    x *= p;
}
template <typename T>
inline void print(T x) {
    if (x<0) {
        x = -x;
        putchar('-');
    }
    static int cnt;
    static int a[50];
    cnt = 0;
    do {
        a[++cnt] = x % 10;
        x /= 10;
    } while (x);
    for (int i = cnt; i >= 1; i--)putchar(a[i] + '0');
    puts("");
}
const double eps=1e-6;
const int mod = 1e9+9;
const int inf = 0x3f3f3f3f;
const int maxn = 4e5+10;
struct Point {
    double x, y;

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

    Point operator-(const Point &oth) const {
        return Point(x - oth.x, y - oth.y);
    }

}a,b,c,d;
double dot(const Point &a, const Point &b) {
    return a.x * b.y - a.y * b.x;
}
inline void work() {
    scanf("%lf%lf", &a.x, &a.y);
    scanf("%lf%lf", &b.x, &b.y);
    scanf("%lf%lf", &c.x, &c.y);
    scanf("%lf%lf", &d.x, &d.y);
    double s1 = fabs(dot(b - d, a - d));
    double s2 = fabs(dot(a - d, c - d));
    double s3 = fabs(dot(c - d, b - d));
    double s = fabs(dot(b - a, c - a));
    if (fabs(s1 + s2 + s3 - s) < eps) puts("in"); else puts("out");
}
int main() {
    //freopen("1.txt","r",stdin);
    int T = 1;
    //read(T);
    while (T--) {
        work();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值