2011-12-09 2 views
0

내 프로그램 (내 프로그래밍 언어가 자바)에 문제가 있습니다. 객체의 Douglas-Peucker가 Points 배열이고 더글러스 - Peucker 알고리즘이 있습니다. 이 Point 배열에 직접 작업하고 싶습니다. 여기서 문제가 시작됩니다.void 메서드를 사용한 재귀?

protected Point[] coinImage; 

// 내 생성자

public Peucker(Point [] tab) { 
    coinImage = new Point[tab.length]; 
    for(int i = 0; i < coinImage.length; i++) { 
     coinImage[i] = new Point(tab[i].x, tab[i].y); 
    } 
} 
public Point[] algoDouglasPeucker() { 
    return douglasPeuckerAux(0,coinImage.length - 1); 
} 

public Point[] douglasPeuckerAux(int startIndex, int endIndex) { 
    double dmax = 0; 
    int index = 0; 
    for(int i = startIndex + 1; i < endIndex; i++) { 
     double distance = this.distancePointSegment(this.coinImage[i], this.coinImage[startIndex], this.coinImage[endIndex]); 
     if(distance > dmax) { 
      index = i; 
      dmax = distance; 
     } 
    } *** 
    if(dmax >= this.epsilon) { 
     Point[] recResult1 = douglasPeuckerAux(startIndex,index); 
     Point[] recResult2 = douglasPeuckerAux(index,endIndex); 
     Point [] result = this.unionTabPoint(recResult1, recResult2); 
     return result; 
    } 
    else { 
     return new Point[] { coinImage[0],coinImage[endIndex] }; 
     } 
} 

*** my problem is here : both methods have a specific type of return : array of Point or I want to change this because I want to work directly on my attribut (coinImage). 

이 무효 방법으로 변경하는 방법 :이 더글러스 Peucker 알고리즘? 도와주세요! 죄송합니다 나는 하나의 방법을 잊지 : 나는 또한이 방법의 유형을 변경하려면 :

public Point[] unionTabPoint(Point [] P1,Point [] P2) { 
    Point[] res = new Point[P1.length + P2.length]; 
    for(int i = 0; i < P1.length;i++) { 
     res[i] = new Point(P1[i].x,P1[i].y); 
    } 
    int k = 0; 
    for(int j = P1.length; j < res.length; j++) { 
     res[j] = new Point(P2[k].x,P2[k].y); 
     k++; 
    } 
    return res; 
} 

그녀는 두 개의 배열하지만 특정 순서없이 노조를 반환합니다.

+0

무엇 무효 방법 (PLS 더 명확히하지 않은 경우) 무엇을 찾고있다 희망? –

+0

algoDouglasPeucker, DouglasPeuckerAux 및 unionTabPoint 메소드를 void 메소드로 변경하려고합니다. 나는 물건으로 일하고 싶지만 여기에서는 그렇지 않다. 그러나 void에서 세 가지 방법을 바꾸는 방법은 무엇입니까? 이 중 하나가 재귀이기 때문에 ... – afk

답변

3

잘 무효 재귀 방법에 대한 기본 레이아웃은 다음과 같이이다 : 그것을 호출 된 메소드의의 다음 행을 반환로

int i = 0; 
public void recursive(){ 
    if(i == 6){ 
     return; 
    } 
    i++; 
    recursive(); 
} 

당신은, 방법을 반복 유지할 수 있습니다. 이 경우 복귀는 '}'에 도달하고 완료되면 메소드를 종료합니다.

희망 나는 도움을 : D

+0

여러분 모두에게 감사드립니다! :) – afk

+0

스택 오버 플로우를 원하지 않는 한, 'i> = 6'이 더 좋은 아이디어 일 수 있으며, OP의 문제와 더 밀접하게 관련되어있는 인수로'i '를 전달할 수도 있습니다. – AusCBloke

+0

사실이에요. 그 점을 지적 해 주셔서 감사합니다 : D 조 – Matt

1

자바는 참조에 의해 호출하고 있습니다. 결과의 로컬 인스턴스를 사용하거나 매개 변수 목록 (예 : method(x, y, Point[]))에 사용하여 결과로 메서드를 강제로 호출 할 수 있습니다. 메서드 호출은 무엇입니까? 마찬가지로 :

public void doSome(x,y) { x==0 ? return : doSome(x-1, y-1); } 
0

나는이 당신을 위해 ...

public void douglasPeuckerAux(int startIndex, int endIndex) { 
    ... 
    Point[] newCoinImage = new Point[] { coinImage[0],coinImage[endIndex] }; 
    coinImage = newCoinImage; 
} 

public void unionTabPoint(Point [] P1,Point [] P2) { 
    ... 
    coinImage = res; 
} 
관련 문제