Tue, 24 May 2022 16:11:10 +0300
more work on circle tool + cleanup
#include "circletool.h" #include "document.h" CircleTool::CircleTool(Document *document) : AbstractDrawTool{document} { } QString CircleTool::name() const { return tr("Circle"); } QString CircleTool::toolTip() const { return tr("Draw circular primitives like circles or discs"); } std::vector<glm::vec3> circle(int divisions) { std::vector<glm::vec3> points; points.reserve(divisions + 1); for (int i = 0; i <= divisions; ++i) { float ang = i * 2.0f * glm::pi<float>() / divisions; points.push_back({std::sin(ang), std::cos(ang), 0.0f}); } return points; } void CircleTool::overpaint(Canvas *canvas, QPainter *painter) const { if (this->previewPolygon.size() >= 2) { for (int i : {0, 1}) { canvas->drawWorldPoint(painter, this->previewPolygon[i]); } painter->setPen(QPen{Qt::green, 2, Qt::DashLine, Qt::RoundCap, Qt::MiterJoin}); canvas->drawWorldPolyline(painter, {this->previewPolygon[0], this->previewPolygon[1]}); const float size = glm::distance(this->previewPolygon[1], this->previewPolygon[0]); glm::mat4 matrix = size * this->baseGridMatrix; matrix[3] = {this->previewPolygon[0], 1}; std::vector<glm::vec3> points = circle(16); for (std::size_t i = 0; i < points.size(); ++i) { points[i] = matrix * glm::vec4{points[i], 1.0f}; } painter->setPen(QPen{Qt::black, 2, Qt::DashLine, Qt::RoundCap, Qt::MiterJoin}); canvas->drawWorldPolyline(painter, points); } if (this->previewPolygon.size() >= 3) { } } QString CircleTool::iconName() const { return ":/icons/linetype-circularprimitive.png"; } void CircleTool::addPoint(const glm::vec3 &pos) { AbstractDrawTool::addPoint(pos); if (this->polygon.size() >= 2) { const glm::mat4& grid = this->document->currentGrid(); const glm::mat4 newGrid = {grid[1], grid[2], grid[0], {this->polygon[0], 1}}; Q_EMIT this->desiredGridChange(newGrid); } } void CircleTool::closeShape() { } void CircleTool::reset() { this->baseGridMatrix = this->document->currentGrid(); }