-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
71 lines (62 loc) · 1.82 KB
/
main.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
#include "RectPacker.h"
#include <QtGui>
#include <vector>
#include <assert.h>
class Visualizer : public QWidget
{
public:
Visualizer(QWidget* parent = 0)
: QWidget(parent), pack(1280, 720)
{
resize(1280, 720);
randomize();
}
protected:
void paintEvent(QPaintEvent*)
{
const QStringList colors = QColor::colorNames();
const QStringList::const_iterator colorEnd = colors.end();
QStringList::const_iterator color = colors.begin();
QPainter p(this);
std::vector<Rect>::const_iterator rect = rects.begin();
const std::vector<Rect>::const_iterator end = rects.end();
while (rect != end) {
if (color == colorEnd)
color = colors.begin();
qDebug() << "filling" << rect->x << rect->y << rect->width() << rect->height();
p.fillRect(rect->x, rect->y, rect->width(), rect->height(), QColor(*color));
++color;
++rect;
}
}
private:
void randomize()
{
srandom(time(0));
enum { RectCount = 600,
MinWidth = 10, MaxWidth = 50,
MinHeight = 20, MaxHeight = 80 };
bool ok;
for (int i = 0; i < RectCount; ++i) {
const int w = std::max<int>(random() % MaxWidth, MinWidth);
const int h = std::max<int>(random() % MaxHeight, MinHeight);
Rect r = pack.insert(w, h, &ok);
if (!ok) {
qWarning("Unable to fit size %dx%d", w, h);
return;
}
assert(r.width() == w && r.height() == h);
rects.push_back(r);
}
}
private:
RectPacker pack;
std::vector<Rect> rects;
};
int main(int argc, char** argv)
{
QApplication app(argc, argv);
Visualizer vis;
vis.show();
return app.exec();
}