明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3700|回复: 11

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

  [复制链接]
发表于 2006-5-14 22:35:00 | 显示全部楼层 |阅读模式

 

我在编一个求点集凸包的程序

采点集的程序部分已经搞定

想把点集中y最近小且距x最远的点找到,就用二分法排了序,但数组p()的值纵传不到数组pl()上。请在帮忙看下:

Option Explicit
Private Type pointinfo
x As Single                    '
点的x 坐标值
y As Single                    '点的y 坐标值
q As Single                    '与水平线的夹角
End Type
Dim p0 As pointinfo
Dim p(0 To 200) As pointinfo    '
点集
Dim pl(0 To 200) As pointinfo   '求出起始点
Dim ps(0 To 200) As pointinfo   '排序后的点集
Dim pi(0 To 200) As pointinfo   '极点
Dim n, m, d As Integer
Dim x0, y0 As Single
Dim l, e As Integer

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
DrawWidth = 4
If n = 0 Then
 p(0).x = x
 p(0).y = y
 Circle (x, y), 100, RGB(255, 0, 255)
Else '
输入点集
 p(n).x = x
 p(n).y = y
 Circle (x, y), 10, RGB(255, 255, 0)
End If
n = n + 1
x0 = x
y0 = y
 
End Sub

Private Sub Command2_Click()
Dim i As Integer
Dim q As Single
p(n).x = x0
p(n).y = y0
Const pi = 3.14
Dim u, j As Integer
Dim k, r, f As Integer
Dim term As pointinfo
For u = 0 To n
  term = p(l)
  k = 0
  r = u - 0
  Do While k <= r
           f = Int((k + r) / 2)
          If term.y < pl(f).y Or (term.y = pl(f).y And (Abs(term.x) > Abs(pl(f).x))) Then
             r = f - 1
          Else
             k = f + 1
          End If
  Loop
  For j = u - 1 To k Step -1
      pl(j + 1) = pl(j)
  Next j
  pl(k) = term
Next u

End Sub

发表于 2006-5-15 09:20:00 | 显示全部楼层

呵呵!昨天公司休息,没有电脑能上网啊。。。。

dance,不怕你见笑,二分法我不明白。。。。要是我做,如果只是简单得到Y最

小X最大的数组,在command2的点击事件中这么写:

Private Sub Command2_Click()
Dim i As Integer
Dim term As pointinfo
term.x = p(0).x: term.y = p(0).y

For i = 1 To n - 1
   If p(i).y < term.y Or (p(i).y = term.y And p(i).x > term.x) Then 

        term.x = p(i).x: term.y = p(i).y

    end if
Next
End Sub

发表于 2006-5-15 09:43:00 | 显示全部楼层

如果是排序,我这样写的:

Private Sub Command2_Click()
Dim i As Integer, j As Integer
Dim term As pointinfo
For i = 1 To n - 1
    For j = 1 To n - i
        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
            term.x = p(j - 1).x: term.y = p(j - 1).y
            p(j - 1).x = p(j).x: p(j - 1).y = p(j).y
            p(j).x = term.x: p(j).y = term.y
        End If
    Next
Next
End Sub

 楼主| 发表于 2006-5-15 22:17:00 | 显示全部楼层

[求助]

xinghesnak发表于2006-5-15 9:43:00 如果是排序,我这样写的: Private Sub Command2_Click()Dim i As Integer, j As IntegerDim term As pointinfoFor i = 1 To n ...

呵呵,这个方法很奏效.谢谢.

为了从p(0)开始计点,我把form-click()部分给改写了下。

但是我在往下编写求其他各点与排序后的极植点p(0)所在水平线的角度p(i).q时,又出现了值传递问题。p(i).q的值总是0,而不是程序的计算值。下面是我的程序:

Option Explicit
Private Type pointinfo
x As Single                    '点的x 坐标值
y As Single                    '点的y 坐标值
q As Single                    '与水平线的夹角
End Type
Dim p0 As pointinfo
Dim p(0 To 200) As pointinfo    '点集
Dim pl(0 To 200) As pointinfo   '求出起始点
Dim ps(0 To 200) As pointinfo   '排序后的点集
Dim pi(0 To 200) As pointinfo   '极点
Dim n, m, d As Integer
Dim x0, y0 As Single
Dim l, e As Integer

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
DrawWidth = 4
If n = 0 Then
 p(0).x = x
 p(0).y = y
 Circle (x, y), 100, RGB(255, 0, 255)
Else '输入点集
 p(n).x = x
 p(n).y = y
 Circle (x, y), 10, RGB(255, 255, 0)
End If
n = n + 1       '我后改写的部分

End Sub


Private Sub Command2_Click()
Dim i As Integer
Dim q As Single
Const pi = 3.14

Dim u As Integer, j As Integer                           '将p()排序y有小至大。
Dim term As pointinfo
For u = 1 To n - 1
    For j = 1 To n - u
        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
            term.x = p(j - 1).x: term.y = p(j - 1).y
            p(j - 1).x = p(j).x: p(j - 1).y = p(j).y
            p(j).x = term.x: p(j).y = term.y
        End If
    Next j
Next u

For i = 1 To n - 1                                         '我计算角度的程序
If p(i).y - p(0).y = 0 Then
        If p(i).x - p(0).x = 0 Then
           p(i).q = 90
        ElseIf p(i).x - p(0).x > 0 Then
          p(i).q = 180 / pi * Atn((p(i).y - p(0).y) / (p(i).x - p(0).x))
        Else
            p(i).q = 180 / pi * Atn((p(i).y - p(0).y) / (p(i).x - p(0).x)) + 180
        End If
ElseIf p(i).y - p(0).y < 0 Then
        If p(i).x - p(0).x = 0 Then
           p(i).q = 270
        ElseIf p(i).x - p(0).x > 0 Then
          p(i).q = 360 + 180 / pi * Atn((p(i).y - p(0).y) / (p(i).x - p(0).x))
        Else
          p(i).q = 180 + 180 / pi * Atn((p(i).y - p(0).y) / (p(i).x - p(0).x))
        End If
End If
       
Next i

End Sub

发表于 2006-5-16 09:19:00 | 显示全部楼层

楼上的,你的程序可。。。。。。

If p(i).y - p(0).y = 0 Then 这句话是不是你的笔误啊,应该写成

If p(i).y - p(0).y> =0 Then ??

 楼主| 发表于 2006-5-17 15:38:00 | 显示全部楼层

[求助]

xinghesnak发表于2006-5-16 9:19:00 楼上的,你的程序可。。。。。。

呵呵,你是不是觉得俺的程序有点啰嗦

不好意思,总是边着编着出纰漏。

 楼主| 发表于 2006-5-17 15:43:00 | 显示全部楼层

[求助]

我现在请问下,怎么清除一个自定义数组的一个值?

下面是后续程序

Private Sub Command3_Click()
Dim u As Integer, j As Integer                           '将p()排序q有小至大逆时针排序。
Dim term As pointinfo
For u = 1 To n - 1
    For j = 1 To n - u
        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
            term = p(j - 1)
            p(j - 1) = p(j)
            p(j) = term
        End If
    Next j
Next u

End Sub

Private Sub Command4_Click()
'找极点
Dim i, j As Integer
Dim s As Single
Dim inpv As Integer
pi(0) = p(0)
pi(1) = p(1)
m = 1
For d = 2 To n - 1

s = sarea(pi(m - 1), pi(m), p(d))
 If s > 0 Then                 '点在pv 数组尾的左侧
    pi(m + 1) = p(d)
    m = m + 1
 Else:
    pi(m) = p(d)               '点在pv 数组尾的右边侧
    m = m - 1
 End If
     m = m + 1
    
Next d
If s = sarea(pi(m - 1), pi(m), p(0)) < 0 Then
   pi(m).x = "": pi(m).y = "": pi(m).q = ""          ‘我想在这个位置清除pi(m)。但怎么设都不行
   m = m - 1
End If
  
DrawWidth = 2

For j = 0 To n - 1          '输出极边
Line (pi(j).x, pi(j).y)-(pi(j + 1).x, pi(j + 1).y)
Next j

Line (pi(n).x, pi(n).y)-(pi(0).x, pi(0).y)

End Sub

 楼主| 发表于 2006-5-17 15:46:00 | 显示全部楼层

抱歉,刚刚忘打自定义函数了

Private Function sarea(x As pointinfo, y As pointinfo, z As pointinfo) As Single
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)
End Function

发表于 2006-5-17 16:26:00 | 显示全部楼层
清楚P(m)是什么意思?除掉这个数还是清除里面的数据?
 楼主| 发表于 2006-5-17 20:34:00 | 显示全部楼层

[求助]

xinghesnak发表于2006-5-17 16:26:00清楚P(m)是什么意思?除掉这个数还是清除里面的数据?

Private Type pointinfo
x As Single                    '点的x 坐标值
y As Single                    '点的y 坐标值
q As Single                    '与水平线的夹角
End Type

Dim p(0 To 200) As pointinfo

p(m)为其中的一个值

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 14:25 , Processed in 0.204059 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表