' 打开文件并读取数据显示超出文件尾
Private Sub cmdCalc_Click()
Dim s As String, n As Integer, i As Integer
Dim x1() As Double, y1() As Double, z1() As Double
Dim x2() As Double, y2() As Double, z2() As Double
Dim A() As Double, At() As Double, Naa() As Double, W() As Double, L() As Double
Dim x(1 To 4) As Double
Dim Ex As Double, k3 As Double
Dim du As Integer, fen As Integer
' 打开文件并读取数据
Open txtFileName.Text For Input As #1
Line Input #1, s
n = Val(s)
Debug.Print "Number of points:" & n
If n <= 0 Then
MsgBox "Invalid number of points."
Exit Sub
End If
ReDim x1(n), y1(n), z1(n), x2(n), y2(n), z2(n)
For i = 1 To n
Debug.Print "Number of points:" & n
If n <= 0 Then
MsgBox "Invalid number of points."
Exit Sub
End If
Input #1, x1(i), y1(i), z1(i), x2(i), y2(i), z2(i)
Next i
Close #1
' 初始化矩阵
ReDim A(1 To 2 * n, 1 To 4)
ReDim L(1 To 2 * n)
ReDim At(1 To 4, 1 To 2 * n)
ReDim Naa(1 To 4, 1 To 4)
ReDim W(1 To 4)
' 填充矩阵 A 和 L
For i = 1 To n
If i > UBound(x1) Or i > UBound(y1) Or i > UBound(z1) Or i > UBound(x2) Or i > UBound(y2) Or i > UBound(z2) Then
MsgBox "Array index out of bounds."
Exit Sub
End If
A(2 * i - 1, 1) = 1: A(2 * i - 1, 2) = 0: A(2 * i - 1, 3) = x1(i): A(2 * i - 1, 4) = y1(i)
A(2 * i, 1) = 0: A(2 * i, 2) = 1: A(2 * i, 3) = -z1(i): A(2 * i, 4) = x1(i)
L(2 * i - 1) = x2(i): L(2 * i) = y2(i)
Next i
If UBound(A, 1) <> 2 * n Or UBound(A, 2) <> 4 Then
MsgBox "Matrix A dimensions are incorrect."
Exit Sub
End If
If UBound(At, 1) <> 4 Or UBound(At, 2) <> 2 * n Then
MsgBox "Matrix At dimensions are incorrect."
Exit Sub
End If
If UBound(Naa, 1) <> 4 Or UBound(Naa, 2) <> 4 Then
MsgBox "Matrix Naa dimensions are incorrect."
Exit Sub
End If
If UBound(W, 1) <> 4 Then
MsgBox "Matrix W dimensions are incorrect."
Exit Sub
End If
If UBound(L, 1) <> 2 * n Then
MsgBox "Matrix L dimensions are incorrect."
Exit Sub
End If
' 矩阵运算
MatrixTrans A, At
Matrix_Multy Naa, At, A
Matrix_Multy W, At, L
MajorInColGuass Naa, W, x
' 分离旋转和尺度参数
If Abs(x(3)) < 0.00000001 Then
If x(4) > 0 Then
Ex = pi / 2
Else
Ex = pi * 3 / 2
End If
Else
Ex = Atn(x(4) / x(3))
If x(3) < 0 And x(4) > 0 Then
Ex = pi - Ex
ElseIf x(3) < 0 And x(4) < 0 Then
Ex = pi + Ex
ElseIf x(3) > 0 And x(4) < 0 Then
Ex = pi * 2 + Ex
End If
End If
k3 = x(3) / Cos(Ex)
' 将转换参数写入相应文本框
txtK3 = Str(k3 - 1)
Ex = Ex * 180 / pi
du = Int(Ex): Ex = (Ex - du) * 60
fen = Int(Ex): Ex = (Ex - fen) * 60
Ex = Val(Format(Ex, "0.00"))
Ex = du + fen / 100# + Ex / 10000
txtEx.Text = Str(Ex)
textEy.Text = Str(Ey)
txtEz.Text = Str(Ez)
txtdX3.Text = Str(x(1))
txtdY3.Text = Str(x(2))
txtdZ3.Text = Str(x(3))
End Sub