Luzhiled's Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub ei1333/library

:heavy_check_mark: geometry/convex_polygon_contains.hpp

Depends on

Verified with

Code

#pragma once

#include <complex>

#include "base.hpp"
#include "point.hpp"
#include "polygon.hpp"

namespace geometry {
int convex_polygon_contains(const Polygon& Q, const Point& p) {
  int N = (int)Q.size();
  Point g = (Q[0] + Q[N / 3] + Q[N * 2 / 3]) / 3.0;
  if (equals(imag(g), imag(p)) && equals(real(g), real(p))) return IN;
  Point gp = p - g;
  int l = 0, r = N;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    Point gl = Q[l] - g;
    Point gm = Q[mid] - g;
    if (cross(gl, gm) > 0) {
      if (cross(gl, gp) >= 0 && cross(gm, gp) <= 0)
        r = mid;
      else
        l = mid;
    } else {
      if (cross(gl, gp) <= 0 && cross(gm, gp) >= 0)
        l = mid;
      else
        r = mid;
    }
  }
  r %= N;
  Real v = cross(Q[l] - p, Q[r] - p);
  return sign(v) == 0 ? ON : sign(v) == -1 ? OUT : IN;
}
}  // namespace geometry
#line 2 "geometry/convex_polygon_contains.hpp"

#include <complex>

#line 2 "geometry/base.hpp"

#include <cmath>

namespace geometry {
using Real = double;
const Real EPS = 1e-8;
const Real PI = std::acos(static_cast<Real>(-1));

enum { OUT, ON, IN };

inline int sign(const Real& r) { return r <= -EPS ? -1 : r >= EPS ? 1 : 0; }

inline bool equals(const Real& a, const Real& b) { return sign(a - b) == 0; }
}  // namespace geometry
#line 2 "geometry/point.hpp"

#line 5 "geometry/point.hpp"
#include <iostream>
#include <vector>

#line 9 "geometry/point.hpp"

namespace geometry {
using Point = std::complex<Real>;

std::istream& operator>>(std::istream& is, Point& p) {
  Real a, b;
  is >> a >> b;
  p = Point(a, b);
  return is;
}

std::ostream& operator<<(std::ostream& os, const Point& p) {
  return os << std::real(p) << " " << std::imag(p);
}

Point operator*(const Point& p, const Real& d) {
  return Point(std::real(p) * d, std::imag(p) * d);
}

// rotate point p counterclockwise by theta rad
Point rotate(Real theta, const Point& p) {
  return Point(std::cos(theta) * std::real(p) - std::sin(theta) * std::imag(p),
               std::sin(theta) * std::real(p) + std::cos(theta) * std::imag(p));
}

Real cross(const Point& a, const Point& b) {
  return std::real(a) * std::imag(b) - std::imag(a) * std::real(b);
}

Real dot(const Point& a, const Point& b) {
  return std::real(a) * std::real(b) + std::imag(a) * std::imag(b);
}

bool compare_x(const Point& a, const Point& b) {
  return equals(std::real(a), std::real(b)) ? std::imag(a) < std::imag(b)
                                            : std::real(a) < std::real(b);
}

bool compare_y(const Point& a, const Point& b) {
  return equals(std::imag(a), std::imag(b)) ? std::real(a) < std::real(b)
                                            : std::imag(a) < std::imag(b);
}

using Points = std::vector<Point>;
}  // namespace geometry
#line 2 "geometry/polygon.hpp"

#line 4 "geometry/polygon.hpp"

#line 6 "geometry/polygon.hpp"

namespace geometry {
using Polygon = std::vector<Point>;
using Polygons = std::vector<Polygon>;
}  // namespace geometry
#line 8 "geometry/convex_polygon_contains.hpp"

namespace geometry {
int convex_polygon_contains(const Polygon& Q, const Point& p) {
  int N = (int)Q.size();
  Point g = (Q[0] + Q[N / 3] + Q[N * 2 / 3]) / 3.0;
  if (equals(imag(g), imag(p)) && equals(real(g), real(p))) return IN;
  Point gp = p - g;
  int l = 0, r = N;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    Point gl = Q[l] - g;
    Point gm = Q[mid] - g;
    if (cross(gl, gm) > 0) {
      if (cross(gl, gp) >= 0 && cross(gm, gp) <= 0)
        r = mid;
      else
        l = mid;
    } else {
      if (cross(gl, gp) <= 0 && cross(gm, gp) >= 0)
        l = mid;
      else
        r = mid;
    }
  }
  r %= N;
  Real v = cross(Q[l] - p, Q[r] - p);
  return sign(v) == 0 ? ON : sign(v) == -1 ? OUT : IN;
}
}  // namespace geometry
Back to top page