Java 礼品包装算法工作不正常
我正试图在VB6中用java实现这个礼品包装算法(!)。我很肯定我做得很好,但由于某种原因,它不会起作用。返回的数组只有1个元素。我希望有人能看看(一双新的眼睛),如果我明显遗漏了什么,让我知道 这是我的密码:Java 礼品包装算法工作不正常,java,algorithm,vb6,convex-hull,Java,Algorithm,Vb6,Convex Hull,我正试图在VB6中用java实现这个礼品包装算法(!)。我很肯定我做得很好,但由于某种原因,它不会起作用。返回的数组只有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 = xPoi
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
Function small(ByVal Current作为整数,ByVal ministen作为整数,ByVal i作为整数)作为布尔值
Dim xa、ya、xb、yb、val为整数
xa=X点(最小)-X点(当前)
xb=X点(i)-X点(当前)
ya=yPoints(最小)-yPoints(当前)
yb=yPoints(i)-yPoints(当前)
val=xa*yb-xb*ya
如果val>0,则
小=真
ElseIf val<0则
小=假
其他的
如果(xa*xb+ya*yb)<0,则
小=假
其他的
如果(xa*xa+ya*ya)>(xb*xb+yb*yb),那么
小=真
其他的
小=假
如果结束
如果结束
如果结束
端函数
子组件1()
Dim Min、i、num、MINIMETER、Current、contourcount2作为整数
Dim xPoints2(),yPoints2()长度相同
'找到最左边的最低点
最小值=1
对于i=1到1计数
如果yPoints(i)=yPoints(Min),则
如果X点(i)
我所有的数组都从1开始。因此,如果你看到1和0之间有任何差异,这就是原因
我知道这是很多,但任何帮助都将不胜感激
谢谢 很难说,因为我不知道类/模块范围中是否有其他变量未显示,但您可能使用了一些未声明的变量
轮廓计数
似乎没有声明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(轮廓计数2)'分配一个“零”插槽。如果要在1处启动该数组,可以使用“ReDim PREVICE xPoints2(1到2)”
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
请注意,small始终是1,因此您总是进行分支
最后,您的分支是:
continue_loop:
Loop While Current <> Min
继续循环:
电流最小时循环
当前值仍然为1,只要您的点在计算最小值时不在位置1,则您将立即满足循环条件并退出。tcarvin,contourCount是全局声明的。我尝试过显式声明每个变量,但它没有改变结果。我知道显式声明每个变量不会改变您的行为,但这是一个最佳实践,因此值得包括在内。
continue_loop:
Loop While Current <> Min