2011-10-13 3 views
0

이 선물 포장 알고리즘 (yoshihitoyagi!)을 VB6에서 java로 구현하려고합니다. 나는 이것을 제대로했다고 확신하지만, 어떤 이유로 그것이 작동하지 않을 것이다. 돌려 주어지는 배열은 1 요소만을가집니다. 나는 누군가가 한 번 (새로운 눈의 집합)을 들여다 볼 수 있었고, 내가 무언가를 뻔뻔스럽게 놓치고 있는지를 알려주기를 바랬다. 당신이 1과 0의 이유는 그 사이에 어떤 차이를 볼 그래서 경우Gift Wrapping 알고리즘이 제대로 작동하지 않습니다.

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부터 시작됩니다

여기 내 코드입니다.

나는 이것이 많은 것을 이해하지만 어떤 도움을 주시면 감사하겠습니다.

고맙습니다 !!!!

답변

2

표시되지 않는 클래스/모듈 범위에있을 수있는 다른 변수가 있는지 모르겠으므로 말하기 어렵지만 사용하지 않은 변수가있을 수 있습니다.

  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 

내가 긴에 그들 모두를 변경주의 . VB6에서 더 이상 정수 (2 바이트) 데이터 형식을 사용할 이유가 거의 없습니다.

EDIT1 : 당신이 1과 0의 이유는 그 사이에 어떤 차이를 볼 그래서 경우

내 모든 배열이 1부터 시작하고있다.

당신이 명시 적으로 언급하지 않는 한 당신의 redim preserves는 1의 낮은 경계를 유지하지 않는다는 것을 알고 계십니까? 그래서 'ReDim Preserve xPoints2 (contourcount2)'는 "제로"슬롯을 할당합니다. 당신이 1

EDIT2에 해당 배열을 시작하려는 경우 'ReDim을 (contourcount2 1) xPoints2 보존'사용할 수 있습니다 : 당신의 마 루프 외부

을 당신은 당신의 할 일 내부 Current = Min

다음이 루프는

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

이이 모든 반복 최소가 1 에 의미합니다.작은이 항상 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)에 있지이되도록이기 때문에 다음 즉시 루프 조건을 만족하는 것 출구.

+0

tcarvin, contourCount는 전역 적으로 선언됩니다. 각 변수를 명시 적으로 선언했지만 결과를 변경하지 못했습니다. – gberg927

+2

각 변수를 명시 적으로 선언해도 행동을 바꿀 수는 없다는 것을 알았지 만 포함할만한 가치가있는 최상의 방법입니다. – tcarvin

+0

가져 오기, 편집 2 확인 – tcarvin

관련 문제