Files
SoftwareRenderer/polygon.hpp

52 lines
1.1 KiB
C++

#ifndef POLYGON_H
#define POLYGON_H
#include "fastmath.hpp"
struct polygon {
vec3 points[3];
vec3 delta[3];
bool skip = false;
polygon(const vec3 &v1, const vec3 &v2, const vec3 &v3)
: points{v1, v2, v3} {}
polygon() : points{} {}
void calcDelta() {
for (int i = 0; i < 3; i++) {
int n = (i + 1) % 3;
vec3 d = points[n] - points[i];
delta[i] = vec3(d.y(), -d.x(), d.z());
if (delta[i].isSmall()) {
skip = true;
return;
}
}
}
bool contains(const vec3 &p) {
if (skip)
return false;
for (int i = 0; i < 3; i++) {
int n = (i + 1) % 3;
vec3 s = p - points[n];
if (s.isSmall())
return false;
if (s * delta[i] <= decimal(0.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