65 lines
1.8 KiB
C++
65 lines
1.8 KiB
C++
#ifndef POLYGON_H
|
|
#define POLYGON_H
|
|
|
|
#include "fastmath.hpp"
|
|
struct polygon {
|
|
|
|
vec3 points[3];
|
|
decimal delta[9];
|
|
bool skip = false;
|
|
|
|
decimal bounding[4]; // min x, max x, min y, max y
|
|
|
|
polygon(const vec3 &v1, const vec3 &v2, const vec3 &v3)
|
|
: points{v1, v2, v3}, delta{} {}
|
|
polygon() : points{}, delta{} {}
|
|
|
|
void calcDelta() {
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
int n = (i + 1) % 3;
|
|
|
|
delta[i * 3] = points[i].y() - points[n].y();
|
|
delta[i * 3 + 1] = points[n].x() - points[i].x();
|
|
delta[i * 3 + 2] =
|
|
points[i].x() * points[n].y() - points[i].y() * points[n].x();
|
|
}
|
|
bounding[0] = points[0].x();
|
|
bounding[1] = points[0].x();
|
|
bounding[2] = points[0].y();
|
|
bounding[3] = points[0].y();
|
|
for (int i = 1; i < 3; i++) {
|
|
if (bounding[0] > points[i].x())
|
|
bounding[0] = points[i].x();
|
|
if (bounding[1] < points[i].x())
|
|
bounding[1] = points[i].x();
|
|
if (bounding[2] > points[i].y())
|
|
bounding[2] = points[i].y();
|
|
if (bounding[3] < points[i].y())
|
|
bounding[3] = points[i].y();
|
|
}
|
|
}
|
|
|
|
const bool contains(const vec3 &p) {
|
|
// if (skip)
|
|
// return false;
|
|
for (int i = 0; i < 3; i++) {
|
|
vec3 d = p;
|
|
if ((((d.x().i >> SHIFT_AMOUNT) * delta[i * 3].i +
|
|
(d.y().i >> SHIFT_AMOUNT) * delta[i * 3 + 1].i +
|
|
delta[i * 3 + 2].i) >>
|
|
SHIFT_AMOUNT) > 0)
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
friend std::ostream &operator<<(std::ostream &os, const polygon &p) {
|
|
for (int i = 0; i < 3; i++) {
|
|
os << p.points[i];
|
|
}
|
|
return os;
|
|
}
|
|
};
|
|
|
|
#endif
|