-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhexagon.cpp
More file actions
91 lines (71 loc) · 2.61 KB
/
Copy pathhexagon.cpp
File metadata and controls
91 lines (71 loc) · 2.61 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
#include "hexagon.h"
#include <QPainter>
#include <cmath>
Hexagon::Hexagon(const QPoint& center, int radius) : Polygon(center, QPoint(-1, -1)), m_radius(radius) {
vertices = calculateVertices();
P = 6 * radius;
S = (3 * sqrt(3) / 2) * radius * radius;
}
void Hexagon::scale(double factor) {
if(SF * factor > 10) SF = 10;
else if(SF * factor < 0.1) SF = 0.1;
else SF *= factor;
P = 6 * m_radius * SF;
S = (3 * sqrt(3) / 2) * m_radius * m_radius * SF * SF;
}
void Hexagon::draw(QPainter& painter, const Shape* selectedShape) const {
painter.save();
painter.translate(m_center);
painter.rotate(rotation);
painter.translate(-m_center);
bool pr = false;
if (selectedShape == this && m_radius != 0) {
painter.setPen(Qt::green);
pr = true;
} else {
painter.setPen(Qt::black);
}
painter.setBrush(Qt::white);
QPolygon polygon;
for(const QPoint& vertice: vertices){
polygon << (vertice - m_center) * SF + m_center;
}
painter.drawPolygon(polygon);
if (pr) {
painter.setBrush(Qt::black);
painter.drawEllipse(m_center, 3, 3);
}
painter.restore();
}
QVector<QPoint> Hexagon::calculateVertices() const {
QVector<QPoint> vertices;
const double angleStep = M_PI / 3;
for (int i = 0; i < 6; ++i) {
double angle = angleStep * i;
int x = m_center.x() + m_radius * cos(angle);
int y = m_center.y() + m_radius * sin(angle);
vertices.append(QPoint(x, y));
}
return vertices;
}
void Hexagon::updatePos(QPoint diff) {
for (int i = 0; i < vertices.size(); i++) {
vertices[i] += diff;
}
m_center += diff;
}
void Hexagon::showInformation(QPainter& painter, int height) {
painter.setPen(Qt::white);
QRect rect(20, height - 43, 100, 20);
painter.drawText(rect, Qt::AlignLeft, QString("P: ") + QString::number(P, 'f', 2));
QRect rect2(20, height - 22, 100, 20);
painter.drawText(rect2, Qt::AlignLeft, QString("S: ") + QString::number(S, 'f', 2));
QRect rect3(142, height - 22, 100, 20);
painter.drawText(rect3, Qt::AlignLeft, QString("Угол: ") + QString::number(-this->rotation, 'f', 2));
QRect rect4(118, height - 43, 100, 20);
painter.drawText(rect4, Qt::AlignLeft, QString("Маcштаб: ") + QString::number(SF, 'f', 2));
QRect rect5(250, height - 22, 100, 20);
painter.drawText(rect5, Qt::AlignLeft, QString("ЦМ X: ") + QString::number(m_center.x(), 'f', 2));
QRect rect6(250, height - 43, 100, 20);
painter.drawText(rect6, Qt::AlignLeft, QString("ЦМ Y: ") + QString::number(m_center.y(), 'f', 2));
}