region: add 'path' query for segments

This commit is contained in:
Danny Robson 2024-11-08 14:36:41 +10:00
parent 0c49eb5845
commit ba7986fce3
2 changed files with 46 additions and 0 deletions

View File

@ -370,6 +370,26 @@ namespace cruft {
rotate90 (cruft::region2<T> obj, int steps);
///////////////////////////////////////////////////////////////////////////
/// Returns the vertices of the region (excluding last->first vertex) in
/// CCW winding.
///
/// It assumes the base position is the min / extent is strictly positive.
template <typename T>
std::array<cruft::point2<T>, 4>
path (cruft::region2<T> const &obj)
{
CHECK (all (obj.e > 0));
return {
obj.p,
obj.p + vector2<T> { 0, obj.e.h },
obj.p + vector2<T> {obj.e.w, obj.e.h },
obj.p + vector2<T> {obj.e.w, 0 },
};
}
///////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
std::ostream& operator<< (std::ostream&, const cruft::region<S,T>&);

View File

@ -286,5 +286,31 @@ main (int, char **)
}
}
// Test that 'path' returns expected values
{
// (-5, 3) (1, 3)
//
//
// (-5,-4)* (1,-4)
//
// Start at the bottom right corner.
cruft::region2i const shape {
cruft::point2i { -5, -4, },
cruft::point2i { 1, 3, },
};
cruft::point2i const expected[] = {
cruft::point2i { -5, -4 },
cruft::point2i { -5, 3 },
cruft::point2i { 1, 3 },
cruft::point2i { 1, -4 },
};
auto const computed = path (shape);
tap.expect (std::ranges::equal (computed, expected), "region2i path");
}
return tap.status ();
}