Mon, 16 May 2022 01:40:49 +0300
work on circle tool
#include "circletool.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 * canvas->getGridMatrix(); 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); } } QString CircleTool::iconName() const { return ":/icons/linetype-circularprimitive.png"; } void CircleTool::closeShape() { }