230 likes | 511 Views
贝塞尔曲线 B 样条曲线 程序. Option Explicit Private Function NNum(n As Integer) As Long Dim i As Integer, r As Long If n = 0 Or n = 1 Then NNum = 1 Exit Function End If r = 1 For i = 2 To n r = r * i Next. NNum = r End Function Private Function CNum(i As Integer, n As Integer) As Long
E N D
Option Explicit Private Function NNum(n As Integer) As Long Dim i As Integer, r As Long If n = 0 Or n = 1 Then NNum = 1 Exit Function End If r = 1 For i = 2 To n r = r * i Next
NNum = r End Function Private Function CNum(i As Integer, n As Integer) As Long CNum = NNum(n) / (NNum(i) * NNum(n - i)) End Private Function BensteinB(t As Single, i As Integer, n As Integer) As Single If i = 0 Then BensteinB = (1 - t) ^ n Exit Function End If
If i = n Then BensteinB = t ^ n Exit Function End If BensteinB = CNum(i, n) * t ^ i * (1 - t) ^ (n - i) End Function Private Function BSplineB(t As Single, k As Integer, n As Integer) As Single Dim j As Integer, r As Single r = 0
For j = 0 To n - k r = r + (-1) ^ j * CNum(j, n + 1) * (t + n - k - j) ^ n Next BSplineB = r / NNum(n) End Function Private Function BZline(t As Single, x() As Single) As Single Dim i As Integer, r As Single, n As Integer n = UBound(x) r = 0
For i = 0 To n r = r + BensteinB(t, i, n) * x(i) Next BZline = r End Function Private Function BSPline(t As Single, x() As Single) As Single Dim i As Integer, r As Single, n As Integer n = UBound(x) r = 0
For i = 0 To n r = r + BSplineB(t, i, n) * x(i) Next BSPline = r End Function Private Sub DBZline(x() As Single, y() As Single) Dim t As Single Dim x1 As Single, y1 As Single, x0 As Single, y0 As Single t = 0
x0 = BZline(t, x) y0 = BZline(t, y) For t = 0 To 1 Step 0.01 x1 = BZline(t, x) y1 = BZline(t, y) Pic.Line (x0, y0)-(x1, y1), RGB(255, 0, 0) x0 = x1 y0 = y1 Next End Sub
Private Sub DSPline(x() As Single, y() As Single) Dim t As Single Dim x1 As Single, y1 As Single, x0 As Single, y0 As Single t = 0 x0 = BSPline(t, x) y0 = BSPline(t, y)
For t = 0 To 1 Step 0.01 x1 = BSPline(t, x) y1 = BSPline(t, y) Pic.Line (x0, y0)-(x1, y1), RGB(255, 0, 0) x0 = x1 y0 = y1 Next End Sub
Private Sub cmdBezier_Click(index As Integer) Dim x(5) As Single, y(5) As Single, x1(2) As Single, y1(2) As Single Dim i As Integer, j As Integer Pic.Cls x(0) = 100: x(1) = 2000: x(2) = 3000: x(3) = 4000: x(4) = 2000: x(5) = 1000 y(0) = 200: y(1) = 3000: y(2) = 300: y(3) = 4000: y(4) = 5000: y(5) = 3000 For i = 0 To UBound(x) - 1 Pic.Line (x(i), y(i))-(x(i + 1), y(i + 1)) Next
If index = 0 Then DBZline x, y Else For i = 0 To UBound(x) - UBound(x1) For j = 0 To UBound(x1) x1(j) = x(i + j) y1(j) = y(i + j) Next DSPline x1, y1 Next End If End Sub
贝塞尔曲面 给定(m+1)(n+1)个空间点列bi,j(i=0,1,2,…,n;j=0,1,2,…,n)后,可以定义mn次贝塞尔曲面如下式所示:
1.双一次贝塞尔曲面 当m=n=1时,有 B0,1(t)=1-t, B1,1(t)=t 所以
2.双二次贝塞尔曲面当m=n=2时,有 B0,2(t)=(1-t)2, B1,2(t)=2t(1-t), B2,2(t)=t2 所以有
3.双二次贝塞尔曲面当m=n=3时,有 B0,3(t)=(1-t)3, B1,3(t)=3t(1-t)2, B2,3(t)= 3 (1-t)2t B3,3(t)= t3 所以有
给定(m+1)(n+1)个空间点列bi,j(i=0,1,2,…,n;j=0,1,2,…,n)后,可以定义mn次B样条曲面如下式所示:给定(m+1)(n+1)个空间点列bi,j(i=0,1,2,…,n;j=0,1,2,…,n)后,可以定义mn次B样条曲面如下式所示:
1.最小二乘法 对于一系列的数据点(xi,yi)和所要绘制的曲线y=f(x),用什么样的标准来评价这条曲线是处于较为合理的状态呢?通常用数据点的坐标值与曲线上对应的坐标之差作为评判的标准。 i=f(xi)-yi 常用的评判方法是:使残差的平方和达到最小-最小二乘法。