-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrectangle.hpp
More file actions
149 lines (114 loc) · 3.8 KB
/
rectangle.hpp
File metadata and controls
149 lines (114 loc) · 3.8 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Templated code must be put in the header, otherwise the compiler won't know
// what code to generate and give linker errors
#ifndef RECTANGLE_H
#define RECTANGLE_H
#include <algorithm>
#include <concepts>
#include <vector>
#include <poppler-rectangle.h>
template <typename T>
concept numeric = std::integral<T> or std::floating_point<T>;
template <class T>
requires numeric<T>
class srectangle : public poppler::rectangle<T> {
public:
srectangle();
srectangle(T x, T y, T w, T h);
srectangle(const srectangle<T> &r);
srectangle<T> &operator=(const srectangle<T> &r);
srectangle<T> normalized() const;
srectangle padded(int p) const;
};
template <numeric T>
srectangle<T> intersect(const srectangle<T> &a, const srectangle<T> &b);
template <numeric T>
std::vector<srectangle<T>> subtract(const srectangle<T> &a,
const srectangle<T> &b);
template <numeric T> bool is_invalid(const srectangle<T> &p);
template <numeric T>
bool operator==(const srectangle<T> &a, const srectangle<T> &b);
template <numeric T>
bool operator!=(const srectangle<T> &a, const srectangle<T> &b);
template <class T>
requires numeric<T> srectangle<T>::srectangle() : poppler::rectangle<T>() {}
template <class T>
requires numeric<T> srectangle<T>::srectangle(T x, T y, T w, T h)
: poppler::rectangle<T>(x, y, w, h) {}
template <class T>
requires numeric<T> srectangle<T>::srectangle(const srectangle<T> &r) {
this->operator=(r);
}
template <class T>
requires numeric<T> srectangle<T>
&srectangle<T>::operator=(const srectangle<T> &r) {
this->set_left(r.x());
this->set_top(r.y());
this->set_right(r.x() + r.width());
this->set_bottom(r.y() + r.height());
return *this;
}
template <numeric T>
srectangle<T> intersect(const srectangle<T> &a, const srectangle<T> &b) {
T x1 = std::max(a.x(), b.x());
T x2 = std::min(a.x() + a.width(), b.x() + b.width());
T y1 = std::max(a.y(), b.y());
T y2 = std::min(a.y() + a.height(), b.y() + b.height());
return {x1, y1, x2 - x1, y2 - y1};
}
template <numeric T>
std::vector<srectangle<T>> subtract(const srectangle<T> &a,
const srectangle<T> &b) {
if (is_invalid(intersect(a, b)))
return {a};
std::vector<srectangle<T>> d;
if (a.y() < b.y())
d.push_back(srectangle<T>{a.x(), a.y(), a.width(), b.y() - a.y()});
if (a.y() + a.height() > b.y() + b.height())
d.push_back(srectangle<T>{a.x(), b.y() + b.height(), a.width(),
a.y() + a.height() - b.y() + b.height()});
if (a.x() < b.x())
d.push_back(srectangle<T>{a.x(), b.y(), b.x() - a.x(), b.height()});
if (a.x() + a.width() > b.x() + b.width())
d.push_back(srectangle<T>{b.x() + b.width(), b.y(),
a.x() + a.width() - b.x() + b.width(),
b.height()});
return d;
}
template <numeric T> bool is_invalid(const srectangle<T> &p) {
return p.x() < 0 || p.y() < 0 || p.width() < 0 || p.height() < 0;
}
template <numeric T>
bool operator==(const srectangle<T> &a, const srectangle<T> &b) {
return a.x() == b.x() && a.y() == b.y() && a.width() == b.width() &&
a.height() == b.height();
}
template <numeric T>
bool operator!=(const srectangle<T> &a, const srectangle<T> &b) {
return !(a == b);
}
template <class T>
requires numeric<T> srectangle<T> srectangle<T>::normalized()
const {
T nx = this->x();
T ny = this->y();
T nw = this->width();
T nh = this->height();
if (nw < 0) {
nw *= -1;
nx -= nw;
}
if (nh < 0) {
nh *= -1;
ny -= nh;
}
return {nx, ny, nw, nh};
}
template <class T>
requires numeric<T> srectangle<T> srectangle<T>::padded(int p)
const {
return {this->x() - p, this->y() - p, this->width() + 2 * p,
this->height() + 2 * p};
}
using srect = srectangle<int>;
using srectf = srectangle<double>;
#endif