-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.cpp
78 lines (60 loc) · 1.61 KB
/
template.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
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
#include "template.h"
#include <algorithm>
using namespace byteimage;
Template::Template() : img(nullptr), width(0), nextIndex(0) { }
void Template::setImage(const ByteImage& img) {
this->img = &img;
rewind();
}
static bool comparePt(const Pt& a, const Pt& b) {return a.r * a.r + a.c * a.c < b.r * b.r + b.c * b.c;}
Template Template::makeBox(int radius) {
Template T = makeSerialBox(radius);
std::stable_sort(T.points.begin(), T.points.end(), comparePt);
return T;
}
Template Template::makeSerialBox(int radius) {
Template T;
Pt p;
for (p.r = -radius; p.r <= radius; p.r++)
for (p.c = -radius; p.c <= radius; p.c++)
T.points.push_back(p);
T.width = 2 * radius + 1;
return T;
}
Template Template::makeCircle(int radius) {
Template T = makeSerialCircle(radius);
std::stable_sort(T.points.begin(), T.points.end(), comparePt);
return T;
}
Template Template::makeSerialCircle(int radius) {
Template T;
Pt p;
for (p.r = -radius; p.r <= radius; p.r++)
for (p.c = -radius; p.c <= radius; p.c++)
if (p.r * p.r + p.c * p.c <= radius * radius)
T.points.push_back(p);
T.width = 2 * radius + 1;
return T;
}
void Template::getCenter(int& r, int& c) {
r = center.r;
c = center.c;
}
void Template::centerAt(int r, int c) {
center = Pt(r, c);
rewind();
}
void Template::rewind() {
nextIndex = 0;
}
bool Template::next(int&r, int& c) {
for (; nextIndex < points.size(); nextIndex++) {
r = center.r + points[nextIndex].r;
c = center.c + points[nextIndex].c;
if (!img || img->inBounds(r, c)) {
nextIndex++;
return 1;
}
}
return 0;
}