comp-geometry

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Luzhiled/comp-geometry

:warning: src/integer-geometry/unit-test/unit-test-twice_area.cpp

Depends on

Code

#include <cassert>
#include <algorithm>

#include "../alias.hpp"
#include "../polygon.hpp"
#include "../twice_area.hpp"

int main() {
  using intgeometry2d::i32;
  using intgeometry2d::polygon;
  using LP32 = intgeometry2d::lattice_point<i32>;
  using intgeometry2d::twice_signed_area;
  using intgeometry2d::twice_unsigned_area;

  polygon< i32 > poly({
    LP32(4, 2),
    LP32(4, 4),
    LP32(2, 4),
    LP32(2, 2)
  });
  assert(twice_signed_area(poly) == 4 * 2);

  polygon< i32 > rpoly = poly;
  reverse(rpoly.begin(), rpoly.end());
  assert(twice_signed_area(rpoly) == -4 * 2);

  assert(twice_unsigned_area(poly) == twice_unsigned_area(rpoly));
}
#line 1 "src/integer-geometry/unit-test/unit-test-twice_area.cpp"
#include <cassert>
#include <algorithm>

#line 2 "src/integer-geometry/alias.hpp"

#include <cstddef>
#include <cstdint>

namespace intgeometry2d {
  using isize = std::ptrdiff_t;
  using usize = std::size_t;

  using i32 = std::int_fast32_t;
  using i64 = std::int_fast64_t;
  using u32 = std::uint_fast32_t;
  using u64 = std::uint_fast64_t;
} // intgeometry2d
#line 2 "src/integer-geometry/polygon.hpp"

#include <vector>

#line 2 "src/integer-geometry/point.hpp"

#line 4 "src/integer-geometry/point.hpp"

namespace intgeometry2d {

  template< typename Z >
  class lattice_point {
    Z x_, y_;

  public:
    lattice_point() {}
    lattice_point(Z x_, Z y_) : x_(x_), y_(y_) {}
    
    Z x() const { return x_; }
    Z y() const { return y_; }

    bool operator==(const lattice_point &p) const { return x_ == p.x_ and y_ == p.y_; }
    bool operator!=(const lattice_point &p) const { return x_ != p.x_ or  y_ != p.y_; }

    lattice_point operator+(lattice_point p) { return lattice_point(x_ + p.x_, y_ + p.y_); }
    lattice_point operator-(lattice_point p) { return lattice_point(x_ - p.x_, y_ - p.y_); }

    Z norm() const { return x_ * x_ + y_ * y_; }
  };

} // intgeometry2d
#line 6 "src/integer-geometry/polygon.hpp"

namespace intgeometry2d {

  template< typename Z >
  using polygon = std::vector< lattice_point<Z> >;

} // intgeometry2d
#line 2 "src/integer-geometry/twice_area.hpp"

#line 2 "src/integer-geometry/det.hpp"

#line 4 "src/integer-geometry/det.hpp"

namespace intgeometry2d {

  template< typename Z >
  Z det(const lattice_point<Z> &a, const lattice_point<Z> &b) {
    return a.x() * b.y() - a.y() * b.x();
  }

} // intgeometry2d
#line 2 "src/integer-geometry/nidx.hpp"

#line 4 "src/integer-geometry/nidx.hpp"

namespace intgeometry2d {
  inline usize nidx(usize idx, usize size) {
    return idx + 1 == size ? 0 : idx + 1;
  }
} // intgeometry2d
#line 7 "src/integer-geometry/twice_area.hpp"

namespace intgeometry2d {
  
  template< typename Z >
  Z twice_signed_area(const polygon<Z> &poly) {
    Z s = Z(0);
    for (usize i = 0; i < poly.size(); i++) {
      s += det(poly[i], poly[nidx(i, poly.size())]);
    }
    return s;
  }

  template< typename Z >
  Z twice_unsigned_area(const polygon<Z> &poly) {
    return abs(twice_signed_area(poly));
  }

} // intgeometry2d
#line 7 "src/integer-geometry/unit-test/unit-test-twice_area.cpp"

int main() {
  using intgeometry2d::i32;
  using intgeometry2d::polygon;
  using LP32 = intgeometry2d::lattice_point<i32>;
  using intgeometry2d::twice_signed_area;
  using intgeometry2d::twice_unsigned_area;

  polygon< i32 > poly({
    LP32(4, 2),
    LP32(4, 4),
    LP32(2, 4),
    LP32(2, 2)
  });
  assert(twice_signed_area(poly) == 4 * 2);

  polygon< i32 > rpoly = poly;
  reverse(rpoly.begin(), rpoly.end());
  assert(twice_signed_area(rpoly) == -4 * 2);

  assert(twice_unsigned_area(poly) == twice_unsigned_area(rpoly));
}
Back to top page