u02/tests: Fix edge case for invalid points

filtered
Simon Bruder 2023-05-13 22:06:37 +02:00
parent 8f784ccf3e
commit 085b8fcdb9
1 changed files with 10 additions and 4 deletions

View File

@ -438,16 +438,22 @@ TEST_CASE("Barycentric coordinates (prop: Σ = 1)") {
int x2 = GENERATE(take(2, random(-100, 100)));
int y2 = GENERATE(take(2, random(-100, 100)));
// If all points are on a straight line, the property does not hold.
// Checking this is equivalent to checking if the area of the triangle is 0.
if ((x0 - x1) * (y0 - y2) - (y0 - y1) * (x0 - x2) == 0) {
// see above
std::cerr << "Points do not form reasonable triangle, skipping"
<< std::endl;
return;
}
int x = GENERATE(take(5, random(-100, 100)));
int y = GENERATE(take(5, random(-100, 100)));
float b1, b2, b3;
std::tie(b1, b2, b3) = barycentric_coordinates(x0, y0, x1, y1, x2, y2, x, y);
// If all points are on a straight line, the property does not hold
if (!(x0 == x1 && x1 == x2) && !(y0 == y1 && y1 == y2)) {
REQUIRE_THAT(b1 + b2 + b3, WithinAbs(1.0, 0.01));
}
REQUIRE_THAT(b1 + b2 + b3, WithinAbs(1.0, 0.01));
}
TEST_CASE("Sort triangle points") {