-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.cpp
51 lines (51 loc) · 1.77 KB
/
grid.cpp
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
class Vec {
public:
ll dx, dy;
Vec(ll dx, ll dy) : dx(dx), dy(dy) {}
Vec(ll x1, ll y1, ll x2, ll y2) : dx(x2 - x1), dy(y2 - y1) {}
Vec(const pair<ll, ll>& c1, const pair<ll, ll>& c2)
: Vec(c1.first, c1.second, c2.first, c2.second) {}
Vec modify(const complex<ll>& c) {
return Vec(dx * c.real() - dy * c.imag(), dx * c.imag() + dy * c.real());
}
Vec operator-() const { return Vec(-dx, -dy); }
};
pair<ll, ll> operator+(const pair<ll, ll>& lhs, const Vec& rhs) {
return {lhs.first + rhs.dx, lhs.second + rhs.dy};
}
pair<ll, ll> operator+(const Vec& lhs, const pair<ll, ll>& rhs) {
return rhs + lhs;
}
pair<ll, ll> operator*(ll lhs, const Vec& rhs) {
return {lhs * rhs.dx, lhs * rhs.dy};
}
pair<ll, ll> operator*(const Vec& lhs, ll rhs) { return rhs * lhs; }
Vec operator*(const complex<ll>& lhs, Vec& rhs) { return rhs.modify(lhs); }
Vec operator*(Vec& lhs, const complex<ll>& rhs) { return rhs * lhs; }
template <class T>
class Grid {
public:
ll xmin, xmax, ymin, ymax;
vector<vector<T>> grid;
T buf;
Grid(ll xmin, ll xmax, ll ymin, ll ymax, T value)
: xmin(xmin),
xmax(xmax),
ymin(ymin),
ymax(ymax),
grid(xmax - xmin, vector<T>(ymax - ymin, value)) {}
Grid(ll xmin, ll xmax, ll ymin, ll ymax)
: Grid(xmin, xmax, ymin, ymax, T()) {}
Grid(ll xmax, ll ymax, T value) : Grid(0, xmax, 0, ymax, value) {}
Grid(ll xmax, ll ymax) : Grid(0, xmax, 0, ymax, T()) {}
typename vector<T>::reference operator()(ll x, ll y) {
return grid[x - xmin][y - ymin];
}
typename vector<T>::reference operator()(const pair<ll, ll>& c) {
return (*this)(c.first, c.second);
}
bool isin(ll x, ll y) {
return x >= xmin && x < xmax && y >= ymin && y < ymax;
}
bool isin(const pair<ll, ll>& c) { return isin(c.first, c.second); }
};