<P>Option Explicit<BR>Private Type POINTAPI<BR>x As Double<BR>y As Double<BR>z As Double<BR>End Type<BR>Dim p() As POINTAPI</P>
<P> </P>
<P> </P>
<P>Private Sub UserForm_Initialize()<BR>ReDim p(0) As POINTAPI<BR>End Sub</P>
<P><BR>Private Sub CommandButton1_Click()<BR> ' 确保文本框的值不为空<BR> Dim item As MSForms.Control<BR> For Each item In UserForm1.Controls<BR> If TypeOf item Is TextBox Then<BR> If Len(item.Text) = 0 Then<BR> MsgBox "请输入定位点!", vbCritical<BR> Exit Sub<BR> End If<BR> End If<BR> Next item<BR> <BR> '存点<BR> If p(0).x <> 0 Then ReDim Preserve p(UBound(p) + 1)</P>
<P> p(UBound(p)).x = Val(TextBox1.Text)<BR> p(UBound(p)).y = Val(TextBox2.Text)<BR> p(UBound(p)).z = Val(TextBox3.Text)</P>
<P> TextBox1.Text = ""<BR> TextBox2.Text = ""<BR> TextBox3.Text = ""<BR> </P>
<P><BR>End Sub</P>
<P> </P>
<P>Private Sub CommandButton2_Click()<BR> ' 确保文本框的值不为空<BR> Dim item As MSForms.Control<BR> For Each item In UserForm1.Controls<BR> If TypeOf item Is TextBox Then<BR> If Len(item.Text) = 0 Then<BR> MsgBox "请输入定位点!", vbCritical<BR> Exit Sub<BR> End If<BR> End If<BR> Next item<BR> <BR> '存点<BR> If p(0).x <> 0 Then ReDim Preserve p(UBound(p) + 1)</P>
<P> p(UBound(p)).x = Val(TextBox1.Text)<BR> p(UBound(p)).y = Val(TextBox2.Text)<BR> p(UBound(p)).z = Val(TextBox3.Text)<BR> <BR> <BR> <BR> ' 绘制直线<BR> Dim i As Integer<BR> Dim FormPnt(0 To 2) As Double<BR> Dim ToPnt(0 To 2) As Double<BR> For i = 1 To UBound(p)<BR> FormPnt(0) = p(i - 1).x<BR> FormPnt(1) = p(i - 1).y<BR> FormPnt(2) = p(i - 1).z<BR> ToPnt(0) = p(i).x<BR> ToPnt(1) = p(i).y<BR> ToPnt(2) = p(i).z<BR> <BR> ThisDrawing.ModelSpace.AddLine FormPnt, ToPnt<BR> <BR> Next<BR> End '结束程序<BR>End Sub</P> <P>其实你的程序总体是对的,要注意两点:</P>
<P>一。程序中这一段</P>
<P>For i = 2 To UBound(p)<BR> FormPnt(0) = p(i - 1).x<BR> FormPnt(1) = p(i - 1).y<BR> FormPnt(2) = p(i - 1).z<BR> ToPnt(0) = p(i).x<BR> ToPnt(1) = p(i).y<BR> ToPnt(2) = p(i).z<BR> <BR> ThisDrawing.ModelSpace.AddLine FormPnt, ToPnt<BR> <BR> Next</P>
<P>让人很费解,为什么i要从2开始递增呢?是不是你想连续画线,那就应该改成这样</P>
<P>For i = 1 To UBound(p) <BR> FormPnt(0) = p(i -1).x<BR> FormPnt(1) = p(i -1).y<BR> FormPnt(2) = p(i -1).z<BR> ToPnt(0) = p(i).x<BR> ToPnt(1) = p(i).y<BR> ToPnt(2) = p(i).z<BR> <BR> ThisDrawing.ModelSpace.AddLine FormPnt, ToPnt<BR> <BR> Next</P>
<P>就是把2改成1就行了,哈哈!</P>
<P>二,我给你的这句话有问题</P>
<P>If p(0).x <> 0 Then ReDim Preserve p(UBound(p) + 1)</P>
<P>如果textbox1控件真的输入0,后面就画不出来了。再增加一个变量,判断是不是第一次输入数值(因为P(0)的xyz都有默认值0)是第一次,就直接</P>
<P>p(UBound(p)).x = Val(TextBox1.Text)<BR> p(UBound(p)).y = Val(TextBox2.Text)<BR> p(UBound(p)).z = Val(TextBox3.Text)</P>
<P>不是就先ReDim Preserve p(UBound(p) + 1),这个改动很重要!</P> <P>呵呵,我的程序搞好了,谢谢各位大虾的帮忙</P>
<P>这是正确的程序,哪位新手想看也能参考</P>
<P>Option Explicit<BR>Private Type POINTAPI<BR>x As Double<BR>y As Double<BR>z As Double<BR>End Type<BR>Dim p() As POINTAPI</P>
<P><BR>Private Sub UserForm_Initialize()<BR>ReDim p(0) As POINTAPI<BR>End Sub</P>
<P><BR>Private Sub CommandButton1_Click()<BR> ' 确保文本框的值不为空<BR> Dim item As MSForms.Control<BR> For Each item In UserForm1.Controls<BR> If TypeOf item Is TextBox Then<BR> If Len(item.Text) = 0 Then<BR> MsgBox "请输入定位点!", vbCritical<BR> Exit Sub<BR> End If<BR> End If<BR> Next item<BR> <BR> TextBox1.SetFocus = 1<BR> <BR> '存点<BR> </P>
<P> p(UBound(p)).x = Val(TextBox1.Text)<BR> p(UBound(p)).y = Val(TextBox2.Text)<BR> p(UBound(p)).z = Val(TextBox3.Text)<BR> <BR> ReDim Preserve p(UBound(p) + 1)</P>
<P> TextBox1.Text = ""<BR> TextBox2.Text = ""<BR> TextBox3.Text = ""<BR> </P>
<P>End Sub</P>
<P> </P>
<P>Private Sub CommandButton2_Click()<BR> ' 确保文本框的值不为空<BR> Dim item As MSForms.Control<BR> For Each item In UserForm1.Controls<BR> If TypeOf item Is TextBox Then<BR> If Len(item.Text) = 0 Then<BR> MsgBox "请输入定位点!", vbCritical<BR> Exit Sub<BR> End If<BR> End If<BR> Next item<BR> <BR> '存点<BR> p(UBound(p)).x = Val(TextBox1.Text)<BR> p(UBound(p)).y = Val(TextBox2.Text)<BR> p(UBound(p)).z = Val(TextBox3.Text)<BR> <BR> <BR> ' 绘制直线<BR> <BR> Dim i As Integer<BR> Dim FormPnt(0 To 2) As Double<BR> Dim ToPnt(0 To 2) As Double<BR> For i = 1 To UBound(p)<BR> FormPnt(0) = p(i - 1).x<BR> FormPnt(1) = p(i - 1).y<BR> FormPnt(2) = p(i - 1).z<BR> ToPnt(0) = p(i).x<BR> ToPnt(1) = p(i).y<BR> ToPnt(2) = p(i).z<BR> <BR> ThisDrawing.ModelSpace.AddLine FormPnt, ToPnt<BR> <BR> Next i<BR> End '结束程序<BR>End Sub</P>
<P> </P>
[求助]能帮我也看看有什么问题吗
xinghesnak发表于2006-5-8 15:32:00static/image/common/back.gif看到最下面的那句
Private Sub Form_Load()ReDim PointData(0) As userDataEnd Sub
了吗?一定要写上,要不就下标越...
<P>我在编一个求点集凸包的程序</P>
<P>采点集的程序部分已经搞定了但到了去极点部分就老提示下标越界</P>
<P>下面是我的程序:</P>
<P>Option Explicit<BR>Private Type pointinfo<BR>x As Single '点的x 坐标值<BR>y As Single '点的y 坐标值<BR>q As Single '与水平线的夹角<BR>End Type<BR>Dim p0 As pointinfo<BR>Dim p(1 To 200) As pointinfo '点集<BR>Dim ps(1 To 200) As pointinfo '排序后的点集<BR>Dim pi(1 To 200) As pointinfo '极点<BR>Dim n, m, d As Integer<BR>Dim x0, y0 As Single<BR>Dim l, e As Integer</P>
<P>Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)<BR>DrawWidth = 4<BR>If n = 0 Then<BR> p0.x = x<BR> p0.y = y<BR> Circle (x, y), 100, RGB(255, 0, 255)<BR>Else '输入点集<BR> p(n).x = x<BR> p(n).y = y<BR> Circle (x, y), 10, RGB(255, 255, 0)<BR>End If<BR>n = n + 1<BR>x0 = x<BR>y0 = y</P>
<P>End Sub</P>
<P><BR>Private Sub Command2_Click()<BR>Dim i As Integer<BR>Dim q As Single<BR>p(n).x = x0<BR>p(n).y = y0<BR>Const pi = 3.14<BR>For i = l To n<BR><B>If p(i).y - p0.y = 0 Then</B> (总是在这句提示下标越界)<BR> If p(i).x - p0.x = 0 Then<BR> p(i).q = 90<BR> ElseIf p(i).x - p0.x > 0 Then<BR> p(i).q = 180 / pi * Atn((p(i).y - p0.y) / (p(i).x - p0.x))<BR> Else<BR> p(i).q = 180 / pi * Atn((p(i).y - p0.y) / (p(i).x - p0.x)) + 180<BR> End If<BR>ElseIf p(i).y - p0.y < 0 Then<BR> If p(i).x - p0.x = 0 Then<BR> p(i).q = 270<BR> ElseIf p(i).x - p0.x > 0 Then<BR> p(i).q = 360 + 180 / pi * Atn((p(i).y - p0.y) / (p(i).x - p0.x))<BR> Else<BR> p(i).q = 180 + 180 / pi * Atn((p(i).y - p0.y) / (p(i).x - p0.x))<BR> End If<BR>End If<BR> <BR>Next i</P>
<P>End Sub</P><BR> <P>楼上的,你的数组下标定义的是1到200,而你的n是0,所以越界了。。定义你的数组时用</P>
<P>Dim p(0 To 200) As pointinfo Dim ps(0 To 200) As pointinfo Dim pi(0 To 200) As pointinfo</P>
<P>就行了。。</P>
[求助]太感谢了
xinghesnak发表于2006-5-13 8:31:00static/image/common/back.gif楼上的,你的数组下标定义的是1到200,而你的n是0,所以越界了。。定义你的数组时用
Dim p(0 To 200) As pointinfo Dim ps(0 To ...
<BR>
<P>呵呵我是新手多谢指教!太感谢了!</P>
<P>但现在又出现新问题了。(注:我又把程序稍改了下,从p(0)开始记录点)</P>
<P>我想把点集中y最近小且距x最远的点找到,就用二分法排了序,但数组p()的值纵传不到数组pl()上。请在帮忙看下:</P>
<P>Option Explicit<BR>Private Type pointinfo<BR>x As Single '点的x 坐标值<BR>y As Single '点的y 坐标值<BR>q As Single '与水平线的夹角<BR>End Type<BR>Dim p0 As pointinfo<BR>Dim p(0 To 200) As pointinfo '点集<BR>Dim pl(0 To 200) As pointinfo '求出起始点<BR>Dim ps(0 To 200) As pointinfo '排序后的点集<BR>Dim pi(0 To 200) As pointinfo '极点<BR>Dim n, m, d As Integer<BR>Dim x0, y0 As Single<BR>Dim l, e As Integer</P>
<P>Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)<BR>DrawWidth = 4<BR>If n = 0 Then<BR> p(0).x = x<BR> p(0).y = y<BR> Circle (x, y), 100, RGB(255, 0, 255)<BR>Else '输入点集<BR> p(n).x = x<BR> p(n).y = y<BR> Circle (x, y), 10, RGB(255, 255, 0)<BR>End If<BR>n = n + 1<BR>x0 = x<BR>y0 = y<BR> <BR>End Sub</P>
<P>Private Sub Command2_Click()<BR>Dim i As Integer<BR>Dim q As Single<BR>p(n).x = x0<BR>p(n).y = y0<BR>Const pi = 3.14<BR>Dim u, j As Integer<BR>Dim k, r, f As Integer<BR>Dim term As pointinfo<BR>For u = 0 To n<BR> term = p(l)<BR> k = 0<BR> r = u - 0<BR> Do While k <= r<BR> f = Int((k + r) / 2)<BR> If term.y < pl(f).y Or (term.y = pl(f).y And (Abs(term.x) > Abs(pl(f).x))) Then<BR> r = f - 1<BR> Else<BR> k = f + 1<BR> End If<BR> Loop<BR> For j = u - 1 To k Step -1<BR> pl(j + 1) = pl(j)<BR> Next j<BR> pl(k) = term<BR>Next u<BR></P>
[求助]大侠今天出门了?
<P>大侠今天出门了?</P><P>有空时,请赐教</P>
<P>多谢</P>
[求助]
dance发表于2006-5-14 22:24:00static/image/common/back.gif回复:(dance)大侠今天出门了?大侠今天出门了?
有空时,请赐教
多谢
<BR>我重创个帖子