-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrush.cpp
38 lines (34 loc) · 1.25 KB
/
brush.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
#include "brush.h"
#include <cstdlib> // For random number generation in spray brush
void Brush::draw(QPainter &painter, const QPoint &pos, int size, const QColor &color, Type type) {
switch (type) {
case Normal:
drawNormal(painter, pos, size, color);
break;
case Spray:
drawSpray(painter, pos, size, color);
break;
case Square:
drawSquare(painter, pos, size, color);
break;
}
}
void Brush::drawNormal(QPainter &painter, const QPoint &pos, int size, const QColor &color) {
painter.setPen(QPen(color, size, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter.drawPoint(pos);
}
void Brush::drawSpray(QPainter &painter, const QPoint &pos, int size, const QColor &color) {
painter.setPen(QPen(color, 1));
int radius = size / 2;
for (int i = 0; i < 100; ++i) {
int dx = (rand() % (2 * radius)) - radius;
int dy = (rand() % (2 * radius)) - radius;
if (dx * dx + dy * dy <= radius * radius) {
painter.drawPoint(pos + QPoint(dx, dy));
}
}
}
void Brush::drawSquare(QPainter &painter, const QPoint &pos, int size, const QColor &color) {
painter.setBrush(QBrush(color));
painter.drawRect(pos.x() - size / 2, pos.y() - size / 2, size, size);
}