礼品包装算法无法正常工作

时间:2011-10-13 14:18:47

标签: java algorithm vb6 convex-hull

我正在尝试在java中用VB6实现这个礼品包装算法(yoshihitoyagi!)。我很确定我已经做好了,但由于某种原因,它不会起作用。返回的数组只有1个元素。我希望有人能看一眼(一双新的眼睛)让我知道我是否会错过一些东西。

这是我的代码:

Function small(ByVal Current As Integer, ByVal smallest As Integer, ByVal i As Integer) As Boolean
Dim xa, ya, xb, yb, val As Integer

xa = xPoints(smallest) - xPoints(Current)
xb = xPoints(i) - xPoints(Current)
ya = yPoints(smallest) - yPoints(Current)
yb = yPoints(i) - yPoints(Current)

val = xa * yb - xb * ya

If val > 0 Then
    small = True
ElseIf val < 0 Then
    small = False
Else
    If (xa * xb + ya * yb) < 0 Then
        small = False
    Else
        If (xa * xa + ya * ya) > (xb * xb + yb * yb) Then
            small = True
        Else
            small = False
        End If
    End If
End If

End Function

Sub CreateContours1()
Dim Min, i, num, smallest, Current, contourcount2 As Integer
Dim xPoints2(), yPoints2() As Long

'Find leftmost lowest point
Min = 1
For i = 1 To contourCount
    If yPoints(i) = yPoints(Min) Then
        If xPoints(i) < xPoints(Min) Then
            Min = i
        End If
    ElseIf yPoints(i) < yPoints(Min) Then
        Min = i
    End If
Next

Debug.Print "Min: " & Min
Current = Min
num = 1

Do
    contourcount2 = contourcount2 + 1
    ReDim Preserve xPoints2(contourcount2)
    ReDim Preserve yPoints2(contourcount2)
    xPoints2(num) = xPoints(Current)
    yPoints2(num) = yPoints(Current)
    Debug.Print "num: " & num & ", current: " & Current & "(" & xPoints(Current) & ", " & yPoints(Current) & ")"
    num = num + 1
    smallest = 1
    If smallest = Current Then
        smallest = 1
    End If

    For i = 1 To contourCount
        If (Current = i) Or (smallest = i) Then
            GoTo continue_loop
        End If
        If small(Current, smallest, i) Then
            smallest = i
        End If
    Next
    Current = smallest
continue_loop:
Loop While Current <> Min

End Sub

我的所有数组都从1开始。所以,如果你看到1和0之间存在任何差异,那就是原因。

我知道这很多,但任何帮助都会受到如此赞赏。

感谢!!!!

1 个答案:

答案 0 :(得分:2)

很难说,因为我不知道是否有其他变量可能在类/模块范围内未显示,但您可能会使用一些未声明的变量。

  1. 使用Option Explicit,查看是否出现任何编译错误。特别是contourCount似乎没有声明。

  2. 您需要明确声明每个变量类型。

  3. 此:

    Dim Min, i, num, smallest, Current, contourcount2 As Integer 
    Dim xPoints2(), yPoints2() As Long 
    

    真的是这样:

    Dim Min As Variant, i As Variant, num As Variant, smallest As Variant, Current As Variant, contourcount2 As Integer 
    Dim xPoints2() As Variant, yPoints2() As Long 
    

    所以你应该改为:

    Dim Min As Long, i As Long, num As Long, smallest As Long, Current As Long, contourcount2 As Long 
    Dim xPoints2() As Long, yPoints2() As Long 
    

    另请注意,我将它们全部更改为Long。几乎没有理由在VB6中使用Integer(2字节)数据类型。

    <强> EDIT1:

      

    我的所有数组都从1开始。所以如果你看到之间有任何差异   这是1和0的原因。

    你是否知道你的redim保留不会保留你的1的下限,除非你明确说明它?所以`ReDim Preserve xPoints2(contourcount2)'分配一个“零”槽。如果要在1处启动该数组,可以使用“ReDim Preserve xPoints2(1到contourcount2)”。

    <强> EDIT2:

    在Do循环之外,您有Current = Min

    接下来你的Do Loop中有

    smallest = 1     
    If smallest = Current Then         
       smallest = 1     
    End If 
    

    这意味着每个迭代最小值为1.

    接下来,你的For循环始终从1开始:

    For i = 1 To contourCount         
        If (Current = i) Or (smallest = i) Then
            GoTo continue_loop         
        End If
        'the rest ommited because you never get here
    Next 
    

    请注意,小始终 1,因此您始终分支。

    最后你的分支是这样的:

    continue_loop: 
    Loop While Current <> Min
    

    当前仍为1并且只要你的点数是这样的,当计算Min时它不在pos 1那么你将立即满足Loop条件并退出。