Skip to content

Commit

Permalink
avoid complicate iterator operation
Browse files Browse the repository at this point in the history
  • Loading branch information
luk036 committed Jan 28, 2024
1 parent 4559aed commit fa6d256
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
11 changes: 6 additions & 5 deletions include/recti/polygon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ namespace recti {
* @return T
*/
constexpr auto signed_area_x2() const -> T {
auto first = this->_vecs.begin();
auto second = std::next(first);
auto itr2 = this->_vecs.begin();
auto itr0 = itr2++;
auto itr1 = itr2++;
auto end = this->_vecs.end();
auto last = std::prev(end);
auto res = first->x() * second->y() - last->x() * std::prev(last)->y();
for (auto itr = second; itr != last; ++itr) {
res = std::move(res) + itr->x() * (std::next(itr)->y() - std::prev(itr)->y());
auto res = itr0->x() * itr1->y() - last->x() * std::prev(last)->y();
for (; itr2 != end; ++itr2, ++itr1, ++itr0) {
res = std::move(res) + itr1->x() * (itr2->y() - itr0->y());
}
return res;
}
Expand Down
11 changes: 5 additions & 6 deletions include/recti/rpolygon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@ namespace recti {
*/
constexpr auto signed_area() const -> T {
assert(this->_vecs.size() >= 1);
auto first = this->_vecs.begin();
auto res = first->x() * first->y();
auto itr0 = first;
for (auto itr = std::next(first); itr != this->_vecs.end(); ++itr) {
res = std::move(res) + itr->x() * (itr->y() - itr0->y());
itr0 = itr;
auto itr1 = this->_vecs.begin();
auto itr0 = itr1++;
auto res = itr0->x() * itr0->y();
for (; itr1 != this->_vecs.end(); ++itr1, ++itr0) {
res = std::move(res) + itr1->x() * (itr1->y() - itr0->y());
}
return res;
// return std::accumulate(++first, this->_vecs.end(), std::move(res),
Expand Down

0 comments on commit fa6d256

Please sign in to comment.