我正在尝试实现这个礼品包装算法(yoshihitoyagi!)在Java中完成,在VB6中完成。我很肯定我做得很好,但由于某些原因,这是行不通的。返回的数组只有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之间有什么不同,这就是为什么。
我知道这是很多,但任何帮助都会非常感谢。
谢谢!!!啊!

最佳答案

很难说,因为我不知道类/模块范围内是否还有其他变量未显示,但您可能使用了一些未声明的变量。
使用option explicit并查看是否显示任何编译错误。尤其是contourCount似乎没有声明。
您需要明确地声明每个变量类型。
这:

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中几乎没有理由再使用整数(2字节)数据类型。
编辑1:
我所有的阵列都从1开始所以如果你看到
1和0就是原因。
您是否知道,除非显式声明,否则您的redim保留不会保留1的下限所以“ReDim Preserve xPoints2(contourcount2)”分配一个“零”槽如果要在1开始数组,可以使用“redim preserve xpoints2(1 to contourcount2)”。
编辑2:
在您的Do循环之外,您有Current = Min
接下来在你的do循环中
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,只要你的点是这样的,当计算分钟,它不是在POS 1,然后你会立即满足循环条件和退出。

08-27 12:25