【计算几何】点与三角形关系
时间限制: 1 Sec 内存限制: 128 MB
题目描述
已知一个平面坐标系中三角形三个点 A、B、CA、B、CA、B、C 的坐标,判断另外一个点 DDD 是否在三角形内(点在三角形边上也认为在三角形内)
输入
输入共四行,每行两个数,前三行表示 A、B、CA、B、CA、B、C 的坐标,第四行为 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;
}