PolyBezier(const POINT *lpPoints,int nCount) 第一个参数lpPoints指向POINT结构数组的首地址,第二个参数指明点数。 eg: CClientDC dc(this); POINT aPoint1[4]={120,100,120,200,250,150,500,40}; POINT aPoint2[4]={120,100,50,350,250,200,500,40}; dc.PolyBezier(aPoint1,4); dc.PolyBezier(aPoint2,4);
我用VB编过,不想用C++再写一遍了,烦。 Option Explicit Private Const MAX As Integer = 20 Private Points(0 To MAX, 1 To 2) As Single Private total As Integer Private border As Boolean Private Function f0(ByVal t As Single) As Single f0 = 1# / 6 * (-t + 1) * (-t + 1) * (-t + 1) End FunctionPrivate Function f1(ByVal t As Single) As Single f1 = 1# / 6 * (3 * t * t * t - 6 * t * t + 4) End Function Private Function f2(ByVal t As Single) As Single f2 = 1# / 6 * (-3 * t * t * t + 3 * t * t + 3 * t + 1) End Function Private Function f3(ByVal t As Single) As Single f3 = 1# / 6 * t * t * t End FunctionPrivate Sub fb(ByVal t As Single, ByRef fs() As Single) fs(0) = f0(t) fs(1) = f1(t) fs(2) = f2(t) fs(3) = f3(t)
End SubPrivate Sub BSpline(ByVal t As Single, ByVal i As Integer, ByRef X As Single, ByRef Y As Single) Dim f(0 To 3) As Single fb t, f Dim j As Integer X = 0 Y = 0 For j = 0 To 3 X = X + f(j) * Points(i + j, 1) Y = Y + f(j) * Points(i + j, 2) Next End SubPrivate Sub BSplineScanning(ByVal sum As Integer) Dim t As Single Dim i As Integer Dim X As Single Dim Y As Single Dim x1 As Single Dim y1 As Single Dim b As Boolean b = False Dim color color = vbBlue If sum < 4 Then Exit Sub For i = 0 To sum - 4 For t = 0 To 1 Step 0.01 BSpline t, i, X, Y If b Then Me.Line (x1, y1)-(X, Y), color Else Me.PSet (X, Y), color b = True End If x1 = X y1 = Y Next If color = vbBlue Then color = vbRed Else color = vbBlue End If Next End SubPrivate Sub Command1_Click() Cls total = 0 End SubPrivate Sub Command2_Click() If border Then Command2.Caption = "border on" Else Command2.Caption = "border off" End If border = Not border End SubPrivate Sub Form_Load() total = 0 border = True End SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) If total < MAX Then Points(total, 1) = X Points(total, 2) = Y total = total + 1 End If Dim i As Integer If border Then For i = 0 To total - 2 Me.Line (Points(i, 1), Points(i, 2))-(Points(i + 1, 1), Points(i + 1, 2)) Next End If BSplineScanning (total) End Sub其实就那么几个公式推来推去的,很简单。这是2D的B样条,可以用鼠标控制选控制点。
第一个参数lpPoints指向POINT结构数组的首地址,第二个参数指明点数。
eg:
CClientDC dc(this);
POINT aPoint1[4]={120,100,120,200,250,150,500,40};
POINT aPoint2[4]={120,100,50,350,250,200,500,40};
dc.PolyBezier(aPoint1,4);
dc.PolyBezier(aPoint2,4);
可能是我没表达清楚。我在做图形学的作业,不能用CDC的函数直接画啦。
B样条是自二阶连续的,贝赛尔要边界条件满足才连续!
[email protected]
Option Explicit
Private Const MAX As Integer = 20
Private Points(0 To MAX, 1 To 2) As Single
Private total As Integer
Private border As Boolean
Private Function f0(ByVal t As Single) As Single
f0 = 1# / 6 * (-t + 1) * (-t + 1) * (-t + 1)
End FunctionPrivate Function f1(ByVal t As Single) As Single
f1 = 1# / 6 * (3 * t * t * t - 6 * t * t + 4)
End Function
Private Function f2(ByVal t As Single) As Single
f2 = 1# / 6 * (-3 * t * t * t + 3 * t * t + 3 * t + 1)
End Function
Private Function f3(ByVal t As Single) As Single
f3 = 1# / 6 * t * t * t
End FunctionPrivate Sub fb(ByVal t As Single, ByRef fs() As Single)
fs(0) = f0(t)
fs(1) = f1(t)
fs(2) = f2(t)
fs(3) = f3(t)
End SubPrivate Sub BSpline(ByVal t As Single, ByVal i As Integer, ByRef X As Single, ByRef Y As Single)
Dim f(0 To 3) As Single
fb t, f
Dim j As Integer
X = 0
Y = 0
For j = 0 To 3
X = X + f(j) * Points(i + j, 1)
Y = Y + f(j) * Points(i + j, 2)
Next
End SubPrivate Sub BSplineScanning(ByVal sum As Integer)
Dim t As Single
Dim i As Integer
Dim X As Single
Dim Y As Single
Dim x1 As Single
Dim y1 As Single
Dim b As Boolean
b = False
Dim color
color = vbBlue
If sum < 4 Then Exit Sub
For i = 0 To sum - 4
For t = 0 To 1 Step 0.01
BSpline t, i, X, Y
If b Then
Me.Line (x1, y1)-(X, Y), color
Else
Me.PSet (X, Y), color
b = True
End If
x1 = X
y1 = Y
Next
If color = vbBlue Then
color = vbRed
Else
color = vbBlue
End If
Next
End SubPrivate Sub Command1_Click()
Cls
total = 0
End SubPrivate Sub Command2_Click()
If border Then
Command2.Caption = "border on"
Else
Command2.Caption = "border off"
End If
border = Not border
End SubPrivate Sub Form_Load()
total = 0
border = True
End SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If total < MAX Then
Points(total, 1) = X
Points(total, 2) = Y
total = total + 1
End If
Dim i As Integer
If border Then
For i = 0 To total - 2
Me.Line (Points(i, 1), Points(i, 2))-(Points(i + 1, 1), Points(i + 1, 2))
Next
End If
BSplineScanning (total)
End Sub其实就那么几个公式推来推去的,很简单。这是2D的B样条,可以用鼠标控制选控制点。
你可以比吧?
[email protected]
看不懂,其实,你只要告诉我怎么样确实参数节点向量T就行了,程序嘛,我的书上有