diff --git a/libs/cg/include/psemek/cg/area.hpp b/libs/cg/include/psemek/cg/area.hpp new file mode 100644 index 00000000..a4d19f41 --- /dev/null +++ b/libs/cg/include/psemek/cg/area.hpp @@ -0,0 +1,43 @@ +#pragma once + +#include +#include + +namespace psemek::cg +{ + + template + auto polygon_area(Iterator begin, Iterator end) + { + using scalar_type = std::remove_cvref_t; + scalar_type result{}; + + using point_type = std::remove_cvref_t; + auto origin = point_type::zero(); + + for (auto it = begin, prev = std::prev(end); it != end; prev = it++) + { + result += geom::volume(origin, *prev, *it); + } + + return result / scalar_type{2}; + } + + template + auto polygon_inertia(Iterator begin, Iterator end, Point const & origin) + { + using scalar_type = std::remove_cvref_t; + scalar_type result{}; + + for (auto it = begin, prev = std::prev(end); it != end; prev = it++) + { + auto const v0 = *prev - origin; + auto const v1 = *it - origin; + result += (v1[1] - v0[1]) * (v0[0] * v0[0] * v0[0] + v0[0] * v0[0] * v1[0] + v0[0] * v1[0] * v1[0] + v1[0] * v1[0] * v1[0]); + result -= (v1[0] - v0[0]) * (v0[1] * v0[1] * v0[1] + v0[1] * v0[1] * v1[1] + v0[1] * v1[1] * v1[1] + v1[1] * v1[1] * v1[1]); + } + + return result / scalar_type{12}; + } + +}