【问题标题】:Gift Wrapping Algorithm not working properly礼品包装算法无法正常工作
【发布时间】:2011-12-07 00:29:19
【问题描述】:

我正在尝试在 VB6 中用 java 实现这个礼品包装算法 (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 之间的任何差异,这就是原因。

我知道这很多,但我们将不胜感激。

谢谢!!!!

【问题讨论】:

    标签: java algorithm vb6 convex-hull


    【解决方案1】:

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

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

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

    这个:

    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 字节)数据类型了。

    EDIT1:

    我所有的数组都是从 1 开始的。所以如果你发现 1 和 0 这就是原因。

    您是否知道除非您明确声明,否则您的 redim 保留不会保留您的下限 1?所以`ReDim Preserve xPoints2(contourcount2)'分配了一个“zeroeth”槽。如果您想从 1 开始该数组,可以使用“ReDim Preserve xPoints2(1 to contourcount2)”。

    EDIT2:

    在你的 Do 循环之外,你有 Current = Min

    在你的 Do 循环中你有下一个

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

    这意味着在次迭代中,最小的是 1。

    接下来你有一个总是从 1 开始的 For 循环:

    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
    

    Current 仍然是 1,只要您的点在计算 Min 时不在 pos 1 处,那么您将立即满足 Loop 条件并退出。

    【讨论】:

    • tcarvin,contourCount 是全局声明的。我尝试明确声明每个变量,但没有看到改变结果。
    • 我知道明确声明每个变量不会改变您的行为,但这是一个最佳实践,因此值得包括在内。
    猜你喜欢
    • 2012-04-18
    • 2023-03-21
    • 2015-11-25
    • 2021-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多