u02: Implement sorting for 3 points
This commit is contained in:
parent
04527ef711
commit
7de6c3cae3
|
@ -36,3 +36,6 @@ Transformation transformation_to_standard_case(int x0, int y0, int x1, int y1);
|
|||
std::tuple<float, float, float> barycentric_coordinates(int x0, int y0, int x1,
|
||||
int y1, int x2, int y2,
|
||||
int xp, int yp);
|
||||
|
||||
// Sorts the points of a triangle to be in ascending order (y0 < y1 < y2).
|
||||
void sort_triangle_points(int &x0, int &y0, int &x1, int &y1, int &x2, int &y2);
|
||||
|
|
|
@ -427,3 +427,28 @@ TEST_CASE("Barycentric coordinates (prop: Σ = 1)") {
|
|||
|
||||
REQUIRE_THAT(b1 + b2 + b3, WithinRel(1.0, 1e-4));
|
||||
}
|
||||
|
||||
TEST_CASE("Sort triangle points") {
|
||||
int x0 = 10, y0 = 60, x1 = 50, y1 = 90, x2 = 40, y2 = 30;
|
||||
sort_triangle_points(x0, y0, x1, y1, x2, y2);
|
||||
REQUIRE(x0 == 40);
|
||||
REQUIRE(y0 == 30);
|
||||
REQUIRE(x1 == 10);
|
||||
REQUIRE(y1 == 60);
|
||||
REQUIRE(x2 == 50);
|
||||
REQUIRE(y2 == 90);
|
||||
}
|
||||
|
||||
TEST_CASE("Sort triangle points (prop: y0 < y1 < y2)") {
|
||||
int x0 = GENERATE(take(3, random(-100, 100)));
|
||||
int y0 = GENERATE(take(3, random(-100, 100)));
|
||||
int x1 = GENERATE(take(3, random(-100, 100)));
|
||||
int y1 = GENERATE(take(3, random(-100, 100)));
|
||||
int x2 = GENERATE(take(3, random(-100, 100)));
|
||||
int y2 = GENERATE(take(3, random(-100, 100)));
|
||||
|
||||
sort_triangle_points(x0, y0, x1, y1, x2, y2);
|
||||
|
||||
REQUIRE(y0 <= y1);
|
||||
REQUIRE(y1 <= y2);
|
||||
}
|
||||
|
|
|
@ -159,3 +159,24 @@ std::tuple<float, float, float> barycentric_coordinates(int x0, int y0, int x1,
|
|||
|
||||
return {b1, b2, b3};
|
||||
}
|
||||
|
||||
void sort_triangle_points(int &x0, int &y0, int &x1, int &y1, int &x2,
|
||||
int &y2) {
|
||||
// Bubble sort is not really ideal in general.
|
||||
// It could be changed to use a more efficient algorithm,
|
||||
// but for only 3 values, it should suffice.
|
||||
// Moreover, implementing sorting on an array/vector of tuples
|
||||
// is probably more overhead.
|
||||
if (y0 > y1) {
|
||||
std::swap(x0, x1);
|
||||
std::swap(y0, y1);
|
||||
}
|
||||
if (y0 > y2) {
|
||||
std::swap(x0, x2);
|
||||
std::swap(y0, y2);
|
||||
}
|
||||
if (y1 > y2) {
|
||||
std::swap(x1, x2);
|
||||
std::swap(y1, y2);
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue