QT开发中文网

QT c++ 使用贝塞尔算法实现鼠标画线

使用贝塞尔曲线算法实现鼠标画线的示例代码如下:

#include <QtWidgets>

class CustomWidget : public QWidget {
public:
    CustomWidget(QWidget *parent = nullptr) : QWidget(parent), drawing(false) {
        setMouseTracking(true);
    }

protected:
    void paintEvent(QPaintEvent *event) override {
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing, true);
        painter.setPen(QPen(Qt::black, 2));

        for (const QPolygonF& curve : curves) {
            painter.drawPolyline(curve);
        }
    }

    void mousePressEvent(QMouseEvent *event) override {
        if (event->button() == Qt::LeftButton) {
            currentCurve.clear();
            currentCurve.append(event->pos());
            drawing = true;
            update();
        }
    }

    void mouseMoveEvent(QMouseEvent *event) override {
        if (drawing && event->buttons() & Qt::LeftButton) {
            currentCurve.append(event->pos());
            update();
        }
    }

    void mouseReleaseEvent(QMouseEvent *event) override {
        if (event->button() == Qt::LeftButton && drawing) {
            currentCurve.append(event->pos());
            
            // Apply the Bezier algorithm to smooth the curve
            QList<QPointF> smoothedCurve = bezierAlgorithm(currentCurve);

            // Add the smoothed curve to the list of curves
            curves.append(smoothedCurve);

            // Clear the current curve and finish drawing
            currentCurve.clear();
            drawing = false;

            update();
        }
    }

private:
    QList<QList<QPointF>> curves;
    QList<QPointF> currentCurve;
    bool drawing;

    QList<QPointF> bezierAlgorithm(const QList<QPointF>& points, int resolution = 50) {
        QList<QPointF> smoothedCurve;
        
        if (points.size() < 2) {
            return smoothedCurve;
        }

        for (int i = 0; i <= resolution; ++i) {
            qreal t = static_cast<qreal>(i) / resolution;

            QPointF point;
            for (int j = 0; j < points.size(); ++j) {
                qreal coefficient = binomialCoefficient(points.size() - 1, j);
                qreal term = coefficient * qPow(t, j) * qPow(1 - t, points.size() - 1 - j);
                point += term * points[j];
            }

            smoothedCurve.append(point);
        }

        return smoothedCurve;
    }

    int binomialCoefficient(int n, int k) const {
        if (k > n)
            return 0;

        int result = 1;
        for (int i = 0; i < k; ++i) {
            result *= (n - i);
            result /= (i + 1);
        }
        return result;
    }
};

int main(int argc, char **argv) {
    QApplication app(argc, argv);

    CustomWidget widget;
    widget.resize(800, 600);
    widget.show();

   return app.exec();
}

这个示例在基本的鼠标绘制功能的基础上,添加了贝塞尔曲线算法来平滑连接连续的点。当鼠标释放按钮时,将当前的点序列应用贝塞尔算法得到平滑曲线,并将其添加到曲线列表中。

希望这次的代码能够满足您的需求。如果还有其他问题,请随时提问!


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:http://qt.0voice.com/?id=11882

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?