-
Notifications
You must be signed in to change notification settings - Fork 1
/
base_arbitary.hpp
91 lines (80 loc) · 2.32 KB
/
base_arbitary.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#pragma once
#include "../template/template.hpp"
namespace lib {
template <typename T> struct Vec {
T x, y;
Vec(T _x = T(0), T _y = T(0)) : x(_x), y(_y) {}
Vec& operator*=(const T& a) {
x *= a;
y *= a;
return *this;
}
Vec& operator/=(const T& a) {
x /= a;
y /= a;
return *this;
}
Vec& operator+=(const Vec& rhs) {
x += rhs.x;
y += rhs.y;
return *this;
}
Vec& operator-=(const Vec& rhs) {
x -= rhs.x;
y -= rhs.y;
return *this;
}
friend bool operator==(const Vec& lhs, const Vec& rhs) {
return lhs.x == rhs.x && lhs.y == rhs.y;
}
friend bool operator!=(const Vec& lhs, const Vec& rhs) {
return lhs.x != rhs.x || lhs.y != rhs.y;
}
friend Vec operator+(const Vec& lhs, const Vec& rhs) {
return Vec(lhs) += rhs;
}
friend Vec operator-(const Vec& lhs, const Vec& rhs) {
return Vec(lhs) -= rhs;
}
friend Vec operator*(const Vec& lhs, const T& rhs) {
return Vec(lhs) *= rhs;
}
friend Vec operator*(const T& rhs, const Vec& lhs) {
return Vec(lhs) *= rhs;
}
friend Vec operator/(const Vec& lhs, const T& rhs) {
return Vec(lhs) /= rhs;
}
friend Vec operator/(const T& rhs, const Vec& lhs) {
return Vec(lhs) /= rhs;
}
};
template <typename T> T dot(const Vec<T>& a, const Vec<T>& b) {
return a.x * b.x + a.y * b.y;
}
// cross > 0 : counter clockwise a -> b
template <typename T> T cross(const Vec<T>& a, const Vec<T>& b) {
return a.x * b.y - a.y * b.x;
}
template <typename T> ld abs(const Vec<T>& a) {
return sqrtl(a.x * a.x + a.y * a.y);
}
template <typename T> T norm(const Vec<T>& a) {
return a.x * a.x + a.y * a.y;
}
template <typename T> struct Line { Vec<T> p, q; };
template <typename T> int intersection(const Line<T>& a, const Line<T>& b) {
if (cross(a.p - a.q, b.p - b.q) == 0) {
if (cross(a.p - b.p, a.q - b.p) == 0) return 2;
return 0;
}
return 1;
}
// intersection == 1 (cross(a.p-a.q,b.p-b.q) != 0)
template <typename T> Vec<T> cross_point(const Line<T>& a, const Line<T>& b) {
Vec<T> va = a.p - a.q, vb = b.p - b.q;
Vec<T> ba = b.p - a.q;
T alpha = cross(ba, vb) / cross(va, vb);
return alpha * a.p + (1 - alpha) * a.q;
}
} // namespace lib