Bezier 曲线

P = ( 1 − t ) P 0 + t P 1 P = (1-t) P0 + t P1 P=(1t)P0+tP1

#include <QWidget>
#include <QApplication>
#include <QPainter>
#include <QPointF>
#include <QPainterPath>

QPointF calculateBezierPoint(float t, const QPointF& P0, const QPointF& P1) {
    return (1 - t) * P0 + t * P1;
}

void LinearBezierCurve(QPainter* painter, const QPointF& P0, const QPointF& P1) {
    float tStep = 0.1f;
    QPainterPath bezierPath;

    QColor curveColor(0, 0, 255);  // 蓝色
    painter->setPen(curveColor);

    for (float t = 0.0f; t <= 1.0f; t += tStep) {
        QPointF P = calculateBezierPoint(t, P0, P1);
        if (t == 0.0f) {
            bezierPath.moveTo(P0);
        } else {
            bezierPath.lineTo(P);
        }
        bezierPath.lineTo(P);
    }
    painter->drawPath(bezierPath);
}

class MyWidget : public QWidget {
public:
    MyWidget(QWidget* parent = nullptr) : QWidget(parent) {
        setFixedSize(800, 600);
    }

protected:
    void paintEvent(QPaintEvent* event) override {
        Q_UNUSED(event);

        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing, true);
        QPoint P0(100, 100);
        QPoint P1(500, 500);
        LinearBezierCurve(&painter, P0,P1);

    }
};


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

    MyWidget widget;
    widget.show();

    return app.exec();
}


二次


void DrawPolygon(QPainter* painter, const QPointF& P0, const QPointF& P1, const QPointF& P2)
{
    QColor polygonColor(0, 0, 255);  // 蓝色
    painter->setPen(polygonColor);
    painter->drawLine(P0, P1);
    painter->drawLine(P1, P2);

}



QPointF calculateQuadraticBezierPoint(float t, const QPointF& P0, const QPointF& P1, const QPointF& P2) {
    float u = 1 - t;
    return u * u * P0 + 2 * u * t * P1 + t * t * P2;
}

void QuadraticBezierCurve(QPainter* painter, const QPointF& P0, const QPointF& P1, const QPointF& P2) {
    float tStep = 0.1f;
    QPainterPath bezierPath;
    QColor curveColor(255, 0, 0);           // 红色

    painter->setPen(curveColor);
    bezierPath.moveTo(P0);
    for (float t = 0.0f; t <= 1.0f; t += tStep) {
        QPointF P = calculateQuadraticBezierPoint(t, P0, P1, P2);
        bezierPath.lineTo(P);
        }
    painter->drawPath(bezierPath);
}

在这里插入图片描述

三次


QPointF calculateCubicBezierPoint(float t, const QPointF& P0, const QPointF& P1, const QPointF& P2, const QPointF& P3) {
    float u = 1 - t;
    float b0 = u * u * u;
    float b1 = 3 * u * u * t;
    float b2 = 3 * u * t * t;
    float b3 = t * t * t;
    return b0 * P0 + b1 * P1 + b2 * P2 + b3 * P3;
}

void CubicBezierCurve(QPainter* painter, const QPointF& P0, const QPointF& P1, const QPointF& P2, const QPointF& P3) {
    float tStep = 0.01f;
    QPainterPath bezierPath;
    QColor curveColor(255, 0, 0);  // 红色

    painter->setPen(curveColor);
    bezierPath.moveTo(P0);

    for (float t = tStep; t <= 1.0f; t += tStep) {
        QPointF P = calculateCubicBezierPoint(t, P0, P1, P2, P3);
        bezierPath.lineTo(P);
    }

    painter->drawPath(bezierPath);
}

在这里插入图片描述

  • 14
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Bezier曲线是一种数学曲线,它可以用来创建光滑的曲线Bezier曲线算法是通过一系列控制点来定义曲线的形状。以下是Bezier曲线算法的基本步骤: 1.定义控制点:定义一组控制点,这些点将决定曲线的形状。 2.计算Bezier曲线上的点:通过递归地计算相邻线段的同等比例点处的连线,再取同等比例的点再连线,一直取到最后那条线段的同等比例处,该点就是Bezier曲线上的点。 3.绘制Bezier曲线:将计算出的Bezier曲线上的点连接起来,就可以绘制出Bezier曲线。 下面是一个使用Python实现的例子: ```python import matplotlib.pyplot as plt import numpy as np def bezier_curve(control_points, num=1000): t = np.linspace(0, 1, num=num) n = len(control_points) result = [] for i in range(num): point = np.zeros((2,)) for j in range(n): point += control_points[j] * bernstein_poly(j, n-1, t[i]) result.append(point) return result def bernstein_poly(i, n, t): return comb(n, i) * t**i * (1-t)**(n-i) def comb(n, i): return np.math.factorial(n) / (np.math.factorial(i) * np.math.factorial(n-i)) control_points = np.array([[0, 0], [1, 2], [3, 1], [4, 3]]) curve_points = bezier_curve(control_points) plt.plot(control_points[:,0], control_points[:,1], 'ro--') plt.plot([p[0] for p in curve_points], [p[1] for p in curve_points], 'b-') plt.show() ``` 该例子中,我们定义了四个控制点,然后使用`bezier_curve`函数计算出Bezier曲线上的点,最后使用Matplotlib库绘制出Bezier曲线。你可以根据自己的需要修改控制点的坐标来创建不同形状的Bezier曲线

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值