This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "src/Geometry/angle.hpp"
名前 | 概要 |
---|---|
radian_to_degree(r) |
ラジアンの値を度数に変換したものを返す. |
degree_to_radian(d) |
度数の値からラジアンに変換したものを返す. |
normalize_radian(r) |
ラジアンの値を $(-\pi,\pi\rbrack$ の区間に収まるように正規化したものを返す. |
angle(p) |
ベクトル $\boldsymbol{p}$ の偏角を返す. |
angle(p,q) |
ベクトル $\boldsymbol{p}$ からベクトル $\boldsymbol{q}$ への偏角を返す. |
rotate(theta) |
原点を中心に角度 $\theta$ だけ回転する変換を返す. 返り値の型は Affine<K> . |
rotate(p, theta) |
点 $\boldsymbol{p}$ を中心に角度 $\theta$ だけ回転する変換を返す. 返り値の型は Affine<K> . |
rotate90(p) |
点 $\boldsymbol{p}$ を中心に $\pi/2$ だけ回転する変換を返す. 返り値の型は Affine<K> . |
AngleComp<K>
クラスPoint<K>
を偏角の大小で比較する関数オブジェクト.
偏角を $(-\pi,\pi\rbrack$ に正規化した上での大小比較する.
ただし $\boldsymbol{0}=(0,0)$ の偏角は $0$ とする.
K
が整数でも動く
主に偏角ソートで使う.
#pragma once
#include <vector>
#include "src/Geometry/Point.hpp"
namespace geo {
long double radian_to_degree(long double r) { return r * 180.0 / M_PI; }
long double degree_to_radian(long double d) { return d * M_PI / 180.0; }
long double normalize_radian(long double r) { return r= fmod(r + M_PI, 2 * M_PI), r > 0 ? r - M_PI : r + M_PI; }
template <class K> long double angle(const Point<K> &p) { return atan2(p.y, p.x); }
template <class K> long double angle(const Point<K> &p, const Point<K> &q) { return atan2(cross(p, q), dot(p, q)); }
template <class K> Affine<K> rotate(long double theta) {
K c= cos(theta), s= sin(theta);
return {c, -s, s, c, Point<K>{0, 0}};
}
template <class K> Affine<K> rotate(const Point<K> &p, long double theta) {
K c= cos(theta), s= sin(theta);
return {c, -s, s, c, Point<K>{p.x - c * p.x + s * p.y, p.y - s * p.x - c * p.y}};
}
template <class K> Affine<K> rotate90(const Point<K> &p) { return {0, -1, 1, 0, p - !p}; }
// (-PI,PI], counter-clockwise
template <class K> class AngleComp {
using P= Point<K>;
static int quad(const P &p) {
if (int s= sgn(p.y); s) return s;
return sgn(p.x) < 0 ? 2 : 0;
}
public:
bool operator()(const P &p, const P &q) const {
if (int a= quad(p), b= quad(q); a != b) return a < b;
return cross(p, q) > 0;
}
};
}
#line 2 "src/Geometry/angle.hpp"
#include <vector>
#line 2 "src/Geometry/Point.hpp"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <cassert>
#line 2 "src/Internal/long_traits.hpp"
// clang-format off
template<class T>struct make_long{using type= T;};
template<>struct make_long<char>{using type= short;};
template<>struct make_long<unsigned char>{using type= unsigned short;};
template<>struct make_long<short>{using type= int;};
template<>struct make_long<unsigned short>{using type= unsigned;};
template<>struct make_long<int>{using type= long long;};
template<>struct make_long<unsigned>{using type= unsigned long long;};
template<>struct make_long<long long>{using type= __int128_t;};
template<>struct make_long<unsigned long long>{using type= __uint128_t;};
template<>struct make_long<float>{using type= double;};
template<>struct make_long<double>{using type= long double;};
template<class T> using make_long_t= typename make_long<T>::type;
// clang-format on
#line 8 "src/Geometry/Point.hpp"
namespace geo {
using namespace std;
struct Visualizer {
ofstream ofs;
Visualizer(string s= "visualize.txt"): ofs(s) { ofs << fixed << setprecision(10); }
friend Visualizer &operator<<(Visualizer &vis, const string &s) { return vis.ofs << s, vis; }
};
template <class K> int sgn(K x) {
if constexpr (is_floating_point_v<K>) {
static constexpr K EPS= 1e-9;
return x < -EPS ? -1 : x > EPS;
} else return x < 0 ? -1 : x > 0;
}
template <class K> K err_floor(K x) {
K y= floor(x);
if constexpr (is_floating_point_v<K>)
if (K z= y + 1, w= x - z; 0 <= sgn(w) && sgn(w - 1) < 0) return z;
return y;
}
template <class K> K err_ceil(K x) {
K y= ceil(x);
if constexpr (is_floating_point_v<K>)
if (K z= y - 1, w= x - z; 0 < sgn(w + 1) && sgn(w) <= 0) return z;
return y;
}
template <class K> struct Point {
K x, y;
Point(K x= K(), K y= K()): x(x), y(y) {}
Point &operator+=(const Point &p) { return x+= p.x, y+= p.y, *this; }
Point &operator-=(const Point &p) { return x-= p.x, y-= p.y, *this; }
Point &operator*=(K a) { return x*= a, y*= a, *this; }
Point &operator/=(K a) { return x/= a, y/= a, *this; }
Point operator+(const Point &p) const { return {x + p.x, y + p.y}; }
Point operator-(const Point &p) const { return {x - p.x, y - p.y}; }
Point operator*(K a) const { return {x * a, y * a}; }
Point operator/(K a) const { return {x / a, y / a}; }
friend Point operator*(K a, const Point &p) { return {a * p.x, a * p.y}; }
Point operator-() const { return {-x, -y}; }
bool operator<(const Point &p) const {
int s= sgn(x - p.x);
return s ? s < 0 : sgn(y - p.y) < 0;
}
bool operator>(const Point &p) const { return p < *this; }
bool operator<=(const Point &p) const { return !(p < *this); }
bool operator>=(const Point &p) const { return !(*this < p); }
bool operator==(const Point &p) const { return !sgn(x - p.x) && !sgn(y - p.y); }
bool operator!=(const Point &p) const { return sgn(x - p.x) || sgn(y - p.y); }
Point operator!() const { return {-y, x}; } // rotate 90 degree
friend istream &operator>>(istream &is, Point &p) { return is >> p.x >> p.y; }
friend ostream &operator<<(ostream &os, const Point &p) { return os << "(" << p.x << ", " << p.y << ")"; }
friend Visualizer &operator<<(Visualizer &vis, const Point &p) { return vis.ofs << p.x << " " << p.y << "\n", vis; }
};
template <class K> make_long_t<K> dot(const Point<K> &p, const Point<K> &q) { return make_long_t<K>(p.x) * q.x + make_long_t<K>(p.y) * q.y; }
// left turn: > 0, right turn: < 0
template <class K> make_long_t<K> cross(const Point<K> &p, const Point<K> &q) { return make_long_t<K>(p.x) * q.y - make_long_t<K>(p.y) * q.x; }
template <class K> make_long_t<K> norm2(const Point<K> &p) { return dot(p, p); }
template <class K> long double norm(const Point<K> &p) { return sqrt(norm2(p)); }
template <class K> make_long_t<K> dist2(const Point<K> &p, const Point<K> &q) { return norm2(p - q); }
template <class T, class U> long double dist(const T &a, const U &b) { return sqrt(dist2(a, b)); }
enum CCW { COUNTER_CLOCKWISE, CLOCKWISE, ONLINE_BACK, ONLINE_FRONT, ON_SEGMENT };
ostream &operator<<(ostream &os, CCW c) { return os << (c == COUNTER_CLOCKWISE ? "COUNTER_CLOCKWISE" : c == CLOCKWISE ? "CLOCKWISE" : c == ONLINE_BACK ? "ONLINE_BACK" : c == ONLINE_FRONT ? "ONLINE_FRONT" : "ON_SEGMENT"); }
template <class K> CCW ccw(const Point<K> &p0, const Point<K> &p1, const Point<K> &p2) {
Point a= p1 - p0, b= p2 - p0;
int s;
if constexpr (is_floating_point_v<K>) s= sgn(sgn(cross(a, b) / sqrt(norm2(a) * norm2(b))));
else s= sgn(cross(a, b));
if (s) return s > 0 ? COUNTER_CLOCKWISE : CLOCKWISE;
if (K d= dot(a, b); sgn(d) < 0) return ONLINE_BACK;
else return sgn(d - norm2(a)) > 0 ? ONLINE_FRONT : ON_SEGMENT;
}
template <class K> struct Line;
template <class K> struct Segment;
template <class K> class Polygon;
template <class K> struct Convex;
template <class K> struct Affine {
K a00= 1, a01= 0, a10= 0, a11= 1;
Point<K> b;
Point<K> operator()(const Point<K> &p) const { return {a00 * p.x + a01 * p.y + b.x, a10 * p.x + a11 * p.y + b.y}; }
Line<K> operator()(const Line<K> &l);
Segment<K> operator()(const Segment<K> &s);
Polygon<K> operator()(const Polygon<K> &p);
Convex<K> operator()(const Convex<K> &c);
Affine operator*(const Affine &r) const { return {a00 * r.a00 + a01 * r.a10, a00 * r.a01 + a01 * r.a11, a10 * r.a00 + a11 * r.a10, a10 * r.a01 + a11 * r.a11, (*this)(r)}; }
Affine &operator*=(const Affine &r) { return *this= *this * r; }
};
template <class K> Affine<K> translate(const Point<K> &p) { return {1, 0, 0, 1, p}; }
}
#line 4 "src/Geometry/angle.hpp"
namespace geo {
long double radian_to_degree(long double r) { return r * 180.0 / M_PI; }
long double degree_to_radian(long double d) { return d * M_PI / 180.0; }
long double normalize_radian(long double r) { return r= fmod(r + M_PI, 2 * M_PI), r > 0 ? r - M_PI : r + M_PI; }
template <class K> long double angle(const Point<K> &p) { return atan2(p.y, p.x); }
template <class K> long double angle(const Point<K> &p, const Point<K> &q) { return atan2(cross(p, q), dot(p, q)); }
template <class K> Affine<K> rotate(long double theta) {
K c= cos(theta), s= sin(theta);
return {c, -s, s, c, Point<K>{0, 0}};
}
template <class K> Affine<K> rotate(const Point<K> &p, long double theta) {
K c= cos(theta), s= sin(theta);
return {c, -s, s, c, Point<K>{p.x - c * p.x + s * p.y, p.y - s * p.x - c * p.y}};
}
template <class K> Affine<K> rotate90(const Point<K> &p) { return {0, -1, 1, 0, p - !p}; }
// (-PI,PI], counter-clockwise
template <class K> class AngleComp {
using P= Point<K>;
static int quad(const P &p) {
if (int s= sgn(p.y); s) return s;
return sgn(p.x) < 0 ? 2 : 0;
}
public:
bool operator()(const P &p, const P &q) const {
if (int a= quad(p), b= quad(q); a != b) return a < b;
return cross(p, q) > 0;
}
};
}