[求助]大侠:请教VB调用自定义函数的值传递问题
<P style="WORD-BREAK: break-all; LINE-HEIGHT: 12pt"><FONT face=宋体></FONT> </P><P style="WORD-BREAK: break-all; LINE-HEIGHT: 12pt"><FONT face=宋体>我在编一个求点集凸包的程序<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt">采点集的程序部分已经搞定<o:p></o:p></P>
<P style="WORD-BREAK: break-all; LINE-HEIGHT: 12pt"><FONT face=宋体>想把点集中y最近小且距x最远的点找到,就用二分法排了序,但数组p()的值纵传不到数组pl()上。请在帮忙看下:<o:p></o:p></FONT></P>
<P style="WORD-BREAK: break-all; LINE-HEIGHT: 12pt"><FONT face=宋体>Option Explicit<BR>Private Type pointinfo<BR>x As Single '</FONT><FONT face=宋体>点的x 坐标值</FONT><BR><FONT face=宋体>y As Single '</FONT><FONT face=宋体>点的y 坐标值</FONT><BR><FONT face=宋体>q As Single '</FONT><FONT face=宋体>与水平线的夹角</FONT><BR><FONT face=宋体>End Type<BR>Dim p0 As pointinfo<BR>Dim p(0 To 200) As pointinfo '</FONT><FONT face=宋体>点集</FONT><BR><FONT face=宋体>Dim pl(0 To 200) As pointinfo '</FONT><FONT face=宋体>求出起始点</FONT><BR><FONT face=宋体>Dim ps(0 To 200) As pointinfo '</FONT><FONT face=宋体>排序后的点集</FONT><BR><FONT face=宋体>Dim pi(0 To 200) As pointinfo '</FONT><FONT face=宋体>极点</FONT><BR><FONT face=宋体>Dim n, m, d As Integer<BR>Dim x0, y0 As Single<BR>Dim l, e As Integer<o:p></o:p></FONT></P>
<P style="WORD-BREAK: break-all; LINE-HEIGHT: 12pt"><FONT face=宋体>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 '</FONT><FONT face=宋体>输入点集</FONT><BR><FONT face=宋体> 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<o:p></o:p></FONT></P>
<P style="WORD-BREAK: break-all; LINE-HEIGHT: 12pt"><FONT face=宋体>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<o:p></o:p></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">End Sub</FONT></P> <P>呵呵!昨天公司休息,没有电脑能上网啊。。。。</P>
<P>dance,不怕你见笑,二分法我不明白。。。。要是我做,如果只是简单得到Y最</P>
<P>小X最大的数组,在command2的点击事件中这么写:</P>
<P>Private Sub Command2_Click()<BR>Dim i As Integer<BR>Dim term As pointinfo<BR>term.x = p(0).x: term.y = p(0).y</P>
<P>For i = 1 To n - 1<BR> If p(i).y < term.y Or (p(i).y = term.y And p(i).x > term.x) Then </P>
<P> term.x = p(i).x: term.y = p(i).y</P>
<P> end if<BR>Next<BR>End Sub</P> <P>如果是排序,我这样写的:</P>
<P>Private Sub Command2_Click()<BR>Dim i As Integer, j As Integer<BR>Dim term As pointinfo<BR>For i = 1 To n - 1<BR> For j = 1 To n - i<BR> If p(j).y < p(j - 1).y Or (p(j).y = p(j - 1).y And p(j).x > p(j - 1).x) Then<BR> term.x = p(j - 1).x: term.y = p(j - 1).y<BR> p(j - 1).x = p(j).x: p(j - 1).y = p(j).y<BR> p(j).x = term.x: p(j).y = term.y<BR> End If<BR> Next<BR>Next<BR>End Sub</P>
[求助]
xinghesnak发表于2006-5-15 9:43:00static/image/common/back.gif如果是排序,我这样写的:
Private Sub Command2_Click()Dim i As Integer, j As IntegerDim term As pointinfoFor i = 1 To n ...
<P>呵呵,这个方法很奏效.谢谢.</P>
<P>为了从p(0)开始计点,我把form-click()部分给改写了下。</P>
<P>但是我在往下编写求其他各点与排序后的极植点p(0)所在水平线的角度p(i).q时,又出现了值传递问题。p(i).q的值总是0,而不是程序的计算值。下面是我的程序:</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><FONT color=#ff0000>n = n + 1 '我后改写的部分</FONT></P>
<P>End Sub<BR></P>
<P><BR>Private Sub Command2_Click()<BR>Dim i As Integer<BR>Dim q As Single<BR>Const pi = 3.14</P>
<P>Dim u As Integer, j As Integer '将p()排序y有小至大。<BR>Dim term As pointinfo<BR>For u = 1 To n - 1<BR> For j = 1 To n - u<BR> If p(j).y < p(j - 1).y Or (p(j).y = p(j - 1).y And p(j).x < p(j - 1).x) Then<BR> term.x = p(j - 1).x: term.y = p(j - 1).y<BR> p(j - 1).x = p(j).x: p(j - 1).y = p(j).y<BR> p(j).x = term.x: p(j).y = term.y<BR> End If<BR> Next j<BR>Next u</P>
<P><FONT color=#0909f7>For i = 1 To n - 1 '我计算角度的程序<BR>If p(i).y - p(0).y = 0 Then<BR> If p(i).x - p(0).x = 0 Then<BR> p(i).q = 90<BR> ElseIf p(i).x - p(0).x > 0 Then<BR> p(i).q = 180 / pi * Atn((p(i).y - p(0).y) / (p(i).x - p(0).x))<BR> Else<BR> p(i).q = 180 / pi * Atn((p(i).y - p(0).y) / (p(i).x - p(0).x)) + 180<BR> End If<BR>ElseIf p(i).y - p(0).y < 0 Then<BR> If p(i).x - p(0).x = 0 Then<BR> p(i).q = 270<BR> ElseIf p(i).x - p(0).x > 0 Then<BR> p(i).q = 360 + 180 / pi * Atn((p(i).y - p(0).y) / (p(i).x - p(0).x))<BR> Else<BR> p(i).q = 180 + 180 / pi * Atn((p(i).y - p(0).y) / (p(i).x - p(0).x))<BR> End If<BR>End If<BR> <BR>Next i</FONT></P>
<P>End Sub<BR></P> <P>楼上的,你的程序可。。。。。。</P>
<P>If p(i).y - p(0).y = 0 Then 这句话是不是你的笔误啊,应该写成</P>
<P>If p(i).y - p(0).y> =0 Then ??</P>
[求助]
xinghesnak发表于2006-5-16 9:19:00static/image/common/back.gif楼上的,你的程序可。。。。。。
<P>呵呵,你是不是觉得俺的程序有点啰嗦</P>
<P>不好意思,总是边着编着出纰漏。</P>
[求助]
<P>我现在请问下,怎么清除一个自定义数组的一个值?</P><P>下面是后续程序</P>
<P>Private Sub Command3_Click()<BR>Dim u As Integer, j As Integer '将p()排序q有小至大逆时针排序。<BR>Dim term As pointinfo<BR>For u = 1 To n - 1<BR> For j = 1 To n - u<BR> If p(j).q < p(j - 1).q Or (p(j).q = p(j - 1).q And p(j).y < p(j - 1).y) Then<BR> term = p(j - 1)<BR> p(j - 1) = p(j)<BR> p(j) = term<BR> End If<BR> Next j<BR>Next u</P>
<P>End Sub</P>
<P>Private Sub Command4_Click()<BR>'找极点<BR>Dim i, j As Integer<BR>Dim s As Single<BR>Dim inpv As Integer<BR>pi(0) = p(0)<BR>pi(1) = p(1)<BR>m = 1<BR>For d = 2 To n - 1</P>
<P>s = sarea(pi(m - 1), pi(m), p(d))<BR> If s > 0 Then '点在pv 数组尾的左侧<BR> pi(m + 1) = p(d)<BR> m = m + 1<BR> Else:<BR> pi(m) = p(d) '点在pv 数组尾的右边侧<BR> m = m - 1<BR> End If<BR> m = m + 1<BR> <BR>Next d<BR>If s = sarea(pi(m - 1), pi(m), p(0)) < 0 Then<BR> pi(m).x = "": pi(m).y = "": pi(m).q = "" <FONT color=#f70909>‘我想在这个位置清除pi(m)。但怎么设都不行<BR></FONT> m = m - 1<BR>End If<BR> <BR>DrawWidth = 2</P>
<P>For j = 0 To n - 1 '输出极边<BR>Line (pi(j).x, pi(j).y)-(pi(j + 1).x, pi(j + 1).y)<BR>Next j</P>
<P>Line (pi(n).x, pi(n).y)-(pi(0).x, pi(0).y)</P>
<P>End Sub<BR></P> <P>抱歉,刚刚忘打自定义函数了</P>
<P>Private Function sarea(x As pointinfo, y As pointinfo, z As pointinfo) As Single<BR>sarea = (x.x * y.y - x.y * y.x) + (y.x * z.y - y.y * z.x) + (z.x * x.y - z.y * x.x)<BR>End Function</P> 清楚P(m)是什么意思?除掉这个数还是清除里面的数据?
[求助]
xinghesnak发表于2006-5-17 16:26:00static/image/common/back.gif清楚P(m)是什么意思?除掉这个数还是清除里面的数据?<P><FONT style="BACKGROUND-COLOR: #f3f3f3">Private Type pointinfo<BR>x As Single '点的x 坐标值<BR>y As Single '点的y 坐标值<BR>q As Single '与水平线的夹角<BR>End Type<BR><BR>Dim p(0 To 200) As pointinfo </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #f3f3f3">p(m)为其中的一个值</FONT><BR></P>
页:
[1]
2