Mon, 16 May 2022 01:40:49 +0300
work on circle tool
186 | 1 | #include "circletool.h" |
2 | ||
3 | CircleTool::CircleTool(Document *document) : | |
4 | AbstractDrawTool{document} | |
5 | { | |
6 | } | |
7 | ||
8 | QString CircleTool::name() const | |
9 | { | |
10 | return tr("Circle"); | |
11 | } | |
12 | ||
13 | QString CircleTool::toolTip() const | |
14 | { | |
15 | return tr("Draw circular primitives like circles or discs"); | |
16 | } | |
17 | ||
187 | 18 | std::vector<glm::vec3> circle(int divisions) |
19 | { | |
20 | std::vector<glm::vec3> points; | |
21 | points.reserve(divisions + 1); | |
22 | for (int i = 0; i <= divisions; ++i) { | |
23 | float ang = i * 2.0f * glm::pi<float>() / divisions; | |
24 | points.push_back({std::sin(ang), std::cos(ang), 0.0f}); | |
25 | } | |
26 | return points; | |
27 | } | |
28 | ||
186 | 29 | void CircleTool::overpaint(Canvas *canvas, QPainter *painter) const |
30 | { | |
31 | if (this->previewPolygon.size() >= 2) | |
32 | { | |
187 | 33 | for (int i : {0, 1}) { |
34 | canvas->drawWorldPoint(painter, this->previewPolygon[i]); | |
35 | } | |
36 | painter->setPen(QPen{Qt::green, 2, Qt::DashLine, Qt::RoundCap, Qt::MiterJoin}); | |
37 | canvas->drawWorldPolyline(painter, {this->previewPolygon[0], this->previewPolygon[1]}); | |
38 | const float size = glm::distance(this->previewPolygon[1], this->previewPolygon[0]); | |
39 | glm::mat4 matrix = size * canvas->getGridMatrix(); | |
40 | matrix[3] = {this->previewPolygon[0], 1}; | |
41 | std::vector<glm::vec3> points = circle(16); | |
42 | for (std::size_t i = 0; i < points.size(); ++i) { | |
43 | points[i] = matrix * glm::vec4{points[i], 1.0f}; | |
44 | } | |
45 | painter->setPen(QPen{Qt::black, 2, Qt::DashLine, Qt::RoundCap, Qt::MiterJoin}); | |
46 | canvas->drawWorldPolyline(painter, points); | |
186 | 47 | } |
48 | } | |
49 | ||
50 | QString CircleTool::iconName() const | |
51 | { | |
52 | return ":/icons/linetype-circularprimitive.png"; | |
53 | } | |
54 | ||
55 | void CircleTool::closeShape() | |
56 | { | |
57 | ||
58 | } |