33 lines
916 B
C++
33 lines
916 B
C++
#pragma once
|
|
#include "vde/core/point.h"
|
|
#include <vector>
|
|
|
|
namespace vde::core {
|
|
|
|
class Polygon2D {
|
|
public:
|
|
Polygon2D() = default;
|
|
explicit Polygon2D(std::vector<Point2D> vertices) : vertices_(std::move(vertices)) {}
|
|
|
|
[[nodiscard]] const std::vector<Point2D>& vertices() const { return vertices_; }
|
|
[[nodiscard]] size_t size() const { return vertices_.size(); }
|
|
[[nodiscard]] bool empty() const { return vertices_.empty(); }
|
|
|
|
/// Signed area (positive = CCW)
|
|
[[nodiscard]] double signed_area() const;
|
|
|
|
/// Absolute area
|
|
[[nodiscard]] double area() const { return std::abs(signed_area()); }
|
|
|
|
/// Point-in-polygon test (ray casting)
|
|
[[nodiscard]] bool contains(const Point2D& p) const;
|
|
|
|
/// Is the polygon counter-clockwise?
|
|
[[nodiscard]] bool is_ccw() const { return signed_area() > 0; }
|
|
|
|
private:
|
|
std::vector<Point2D> vertices_;
|
|
};
|
|
|
|
} // namespace vde::core
|