dance 发表于 2006-5-14 22:35:00

[求助]大侠:请教VB调用自定义函数的值传递问题

<P style="WORD-BREAK: break-all; LINE-HEIGHT: 12pt"><FONT face=宋体></FONT>&nbsp;</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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '</FONT><FONT face=宋体>点的x 坐标值</FONT><BR><FONT face=宋体>y As Single&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '</FONT><FONT face=宋体>点的y 坐标值</FONT><BR><FONT face=宋体>q As Single&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '</FONT><FONT face=宋体>与水平线的夹角</FONT><BR><FONT face=宋体>End Type<BR>Dim p0 As pointinfo<BR>Dim p(0 To 200) As pointinfo&nbsp;&nbsp;&nbsp; '</FONT><FONT face=宋体>点集</FONT><BR><FONT face=宋体>Dim pl(0 To 200) As pointinfo&nbsp;&nbsp; '</FONT><FONT face=宋体>求出起始点</FONT><BR><FONT face=宋体>Dim ps(0 To 200) As pointinfo&nbsp;&nbsp; '</FONT><FONT face=宋体>排序后的点集</FONT><BR><FONT face=宋体>Dim pi(0 To 200) As pointinfo&nbsp;&nbsp; '</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>&nbsp;p(0).x = x<BR>&nbsp;p(0).y = y<BR>&nbsp;Circle (x, y), 100, RGB(255, 0, 255)<BR>Else '</FONT><FONT face=宋体>输入点集</FONT><BR><FONT face=宋体>&nbsp;p(n).x = x<BR>&nbsp;p(n).y = y<BR>&nbsp;Circle (x, y), 10, RGB(255, 255, 0)<BR>End If<BR>n = n + 1<BR>x0 = x<BR>y0 = y<BR>&nbsp;<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>&nbsp; term = p(l)<BR>&nbsp; k = 0<BR>&nbsp; r = u - 0<BR>&nbsp; Do While k &lt;= r<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f = Int((k + r) / 2)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If term.y &lt; pl(f).y Or (term.y = pl(f).y And (Abs(term.x) &gt; Abs(pl(f).x))) Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r = f - 1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; k = f + 1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<BR>&nbsp; Loop<BR>&nbsp; For j = u - 1 To k Step -1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pl(j + 1) = pl(j)<BR>&nbsp; Next j<BR>&nbsp; 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>

xinghesnak 发表于 2006-5-15 09:20:00

<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&nbsp;n - 1<BR>&nbsp;&nbsp; If p(i).y &lt; term.y Or (p(i).y = term.y And p(i).x &gt; term.x) Then&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; term.x = p(i).x: term.y = p(i).y</P>
<P>&nbsp;&nbsp;&nbsp; end if<BR>Next<BR>End Sub</P>

xinghesnak 发表于 2006-5-15 09:43:00

<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>&nbsp;&nbsp;&nbsp; For j = 1 To n - i<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If p(j).y &lt; p(j - 1).y Or (p(j).y = p(j - 1).y And p(j).x &gt; p(j - 1).x) Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; term.x = p(j - 1).x: term.y = p(j - 1).y<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p(j - 1).x = p(j).x: p(j - 1).y = p(j).y<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p(j).x = term.x: p(j).y = term.y<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<BR>&nbsp;&nbsp;&nbsp; Next<BR>Next<BR>End Sub</P>

dance 发表于 2006-5-15 22:17:00

[求助]

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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '点的x 坐标值<BR>y As Single&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '点的y 坐标值<BR>q As Single&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '与水平线的夹角<BR>End Type<BR>Dim p0 As pointinfo<BR>Dim p(0 To 200) As pointinfo&nbsp;&nbsp;&nbsp; '点集<BR>Dim pl(0 To 200) As pointinfo&nbsp;&nbsp; '求出起始点<BR>Dim ps(0 To 200) As pointinfo&nbsp;&nbsp; '排序后的点集<BR>Dim pi(0 To 200) As pointinfo&nbsp;&nbsp; '极点<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>&nbsp;p(0).x = x<BR>&nbsp;p(0).y = y<BR>&nbsp;Circle (x, y), 100, RGB(255, 0, 255)<BR>Else '输入点集<BR>&nbsp;p(n).x = x<BR>&nbsp;p(n).y = y<BR>&nbsp;Circle (x, y), 10, RGB(255, 255, 0)<BR>End If<BR><FONT color=#ff0000>n = n + 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '我后改写的部分</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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '将p()排序y有小至大。<BR>Dim term As pointinfo<BR>For u = 1 To n - 1<BR>&nbsp;&nbsp;&nbsp; For j = 1 To n - u<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If p(j).y &lt; p(j - 1).y Or (p(j).y = p(j - 1).y And p(j).x &lt; p(j - 1).x) Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; term.x = p(j - 1).x: term.y = p(j - 1).y<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p(j - 1).x = p(j).x: p(j - 1).y = p(j).y<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p(j).x = term.x: p(j).y = term.y<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<BR>&nbsp;&nbsp;&nbsp; Next j<BR>Next u</P>
<P><FONT color=#0909f7>For i = 1 To n - 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '我计算角度的程序<BR>If p(i).y - p(0).y = 0 Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If p(i).x - p(0).x = 0 Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p(i).q = 90<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ElseIf p(i).x - p(0).x &gt; 0 Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p(i).q = 180 / pi * Atn((p(i).y - p(0).y) / (p(i).x - p(0).x))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p(i).q = 180 / pi * Atn((p(i).y - p(0).y) / (p(i).x - p(0).x)) + 180<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<BR>ElseIf p(i).y - p(0).y &lt; 0 Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If p(i).x - p(0).x = 0 Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p(i).q = 270<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ElseIf p(i).x - p(0).x &gt; 0 Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p(i).q = 360 + 180 / pi * Atn((p(i).y - p(0).y) / (p(i).x - p(0).x))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p(i).q = 180 + 180 / pi * Atn((p(i).y - p(0).y) / (p(i).x - p(0).x))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<BR>End If<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>Next i</FONT></P>
<P>End Sub<BR></P>

xinghesnak 发表于 2006-5-16 09:19:00

<P>楼上的,你的程序可。。。。。。</P>
<P>If p(i).y - p(0).y = 0 Then 这句话是不是你的笔误啊,应该写成</P>
<P>If p(i).y - p(0).y&gt; =0 Then ??</P>

dance 发表于 2006-5-17 15:38:00

[求助]

xinghesnak发表于2006-5-16 9:19:00static/image/common/back.gif
楼上的,你的程序可。。。。。。


<P>呵呵,你是不是觉得俺的程序有点啰嗦</P>
<P>不好意思,总是边着编着出纰漏。</P>

dance 发表于 2006-5-17 15:43:00

[求助]

<P>我现在请问下,怎么清除一个自定义数组的一个值?</P>
<P>下面是后续程序</P>
<P>Private Sub Command3_Click()<BR>Dim u As Integer, j As Integer&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '将p()排序q有小至大逆时针排序。<BR>Dim term As pointinfo<BR>For u = 1 To n - 1<BR>&nbsp;&nbsp;&nbsp; For j = 1 To n - u<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If p(j).q &lt; p(j - 1).q Or (p(j).q = p(j - 1).q And p(j).y &lt; p(j - 1).y) Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; term = p(j - 1)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p(j - 1) = p(j)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p(j) = term<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<BR>&nbsp;&nbsp;&nbsp; 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>&nbsp;If s &gt; 0 Then&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '点在pv 数组尾的左侧<BR>&nbsp;&nbsp;&nbsp; pi(m + 1) = p(d)<BR>&nbsp;&nbsp;&nbsp; m = m + 1<BR>&nbsp;Else:<BR>&nbsp;&nbsp;&nbsp; pi(m) = p(d)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '点在pv 数组尾的右边侧<BR>&nbsp;&nbsp;&nbsp; m = m - 1<BR>&nbsp;End If<BR>&nbsp;&nbsp;&nbsp;&nbsp; m = m + 1<BR>&nbsp;&nbsp;&nbsp;&nbsp; <BR>Next d<BR>If s = sarea(pi(m - 1), pi(m), p(0)) &lt; 0 Then<BR>&nbsp;&nbsp; pi(m).x = "": pi(m).y = "": pi(m).q = ""&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT color=#f70909>‘我想在这个位置清除pi(m)。但怎么设都不行<BR></FONT>&nbsp;&nbsp; m = m - 1<BR>End If<BR>&nbsp;&nbsp; <BR>DrawWidth = 2</P>
<P>For j = 0 To n - 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '输出极边<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>

dance 发表于 2006-5-17 15:46:00

<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>

xinghesnak 发表于 2006-5-17 16:26:00

清楚P(m)是什么意思?除掉这个数还是清除里面的数据?

dance 发表于 2006-5-17 20:34:00

[求助]

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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '点的x 坐标值<BR>y As Single&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '点的y 坐标值<BR>q As Single&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '与水平线的夹角<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
查看完整版本: [求助]大侠:请教VB调用自定义函数的值传递问题