QT绘制B样条曲线
² 貝塞爾曲線
貝塞爾曲線是通過一組多邊折線的各頂點(diǎn)來定義。在各頂點(diǎn)中,曲線經(jīng)過第一點(diǎn)和最后一點(diǎn),其余各點(diǎn)則定義曲線的導(dǎo)數(shù)、階次和形狀。第一條和最后一條則表示曲線起點(diǎn)和終點(diǎn)的切線方向。
² B樣條曲線
針對貝塞爾曲線存在的一些缺點(diǎn),數(shù)學(xué)家們提出了B樣條方法,在保留貝塞爾全部優(yōu)點(diǎn)的同時,克服可貝塞爾方法的弱點(diǎn)。
1) 二次B樣條曲線
2) 三次B樣條曲線
QT中的QPainter提供了繪制貝塞爾曲線的相關(guān)API:
void QPainterPath::quadTo(const QPointF &c, const QPointF &endPoint)
void QPainterPath::cubicTo(const QPointF &c1, const QPointF &c2, const QPointF &endPoint)
void QPainter::drawPath(const QPainterPath &path)
Widget.h
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
///////////////////////////////////////////////////////////// ///@fileWidget.h ///@brief繪制B樣條曲線Widget類 /// ///通過鼠標(biāo)點(diǎn)擊來繪制B樣條曲線 ///@authorMichaelJoessy ///@date2019-07-02 ///////////////////////////////////////////////////////////// #ifndefWIDGET_H #defineWIDGET_H #include<QWidget> classWidget:publicQWidget public: protected: private: private: #endif//WIDGET_H |
Widget.pp
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
#include"Widget.h" #include<QMouseEvent> #include<QPainter> #include<cmath> Widget::Widget(QWidget*parent) Widget::~Widget() } voidWidget::mousePressEvent(QMouseEvent*event) voidWidget::mouseReleaseEvent(QMouseEvent*event) } voidWidget::mouseDoubleClickEvent(QMouseEvent*event) } voidWidget::mouseMoveEvent(QMouseEvent*event) } voidWidget::paintEvent(QPaintEvent*event) voidWidget::drawSpline() //drawcontrolpoints qrealWidget::N(intk,inti,qrealu) qrealWidget::N1(inti,qrealu) qrealWidget::N2(inti,qrealu) qrealWidget::N3(inti,qrealu) |
上述算法有點(diǎn)簡單,https://github.com/vkorchagin/animated-b-spline提供了比較好的算法例子,值得參考。
給定B樣條曲線的控制點(diǎn)。 這些點(diǎn)在屏幕上移動,從而使樣條動畫。
B樣條通過de Boor算法轉(zhuǎn)換為合成Bezier曲線。 貝塞爾曲線用de Casteljau算法進(jìn)行插值。
特征:
添加和刪除控制點(diǎn)。
通過de Casteljau算法更改插值質(zhì)量。
切換抗鋸齒。
不斷變化的動畫速度。
切換可見點(diǎn)和線。
總結(jié)
- 上一篇: 平板电脑二合一平板电脑二合一是什么意思?
- 下一篇: 做一个国产操作系统到底有多难国产操作系统