2012-08-24 2 views
0

벡터 파일 (세빌의지도를 그리는 일련의 좌표가있는 .txt)에서 많은 수의 선을 그릴 때 속도를 향상시키기 위해 자체 드로잉 함수를 작성하고 있습니다. 이러한 함수를 빌드하는 픽셀 [int] 메서드를 사용하고 있지만 몇 가지 이유로 프로그램을 설명 할 수 없다는 설명 할 수 없습니다.이 같은 좌표를 입력 할 때 : drawline (y1, x1, y2, x2) 그 방법 : drawline (x1, y1, x2, y2) 논리적 인 설명이 있습니까?사용자 지정 그리기 선 기능, 좌표 방향이 프로그램 속도를 늦추는 이유는 무엇입니까?

.txt 인 코드의 구조는 간단하고 이렇게되면 (전체 .txt 파일이 folder에서 다운로드 할 수 있습니다) : 아래

1188156570;1188156570;37.417595;-5.9971519 
1188156400;1188156400;37.4175115;-5.9970483 
1188156720;1188156720;37.4174588;-5.9969338 
1188156606;1188156606;37.4175833;-5.9966021 
1188156462;1188156462;37.4177174;-5.9960534 
1188156753;1188156753;37.4177413;-5.9958605 
1188156643;1188156643;37.417703;-5.9955831 
1132983943;1132983943;37.4176646;-5.995381 
next 
304791377;304791377;37.3968538;-6.0066269 
1188156644;1188156644;37.3967509;-6.0064412 
1188156521;1188156521;37.3956275;-6.0073602 
next 
1188216699;1188216699;37.4221365;-5.9959761 
693311201;693311201;37.4253619;-5.9951655 
1188216625;1188216625;37.4239123;-5.9924734 
1188216567;1188216567;37.4233085;-5.9916937 
1186512382;1186512382;37.4223597;-5.9910465 
1188216642;1188216642;37.4216335;-5.9927836 
1188216699;1188216699;37.4221365;-5.9959761 
next 

은 처리 1.5.1 스케치의 코드 가능한 한 짧게하고 가능한 한 깔끔하게 만들었습니다. 도와 주셔서 감사합니다! 나는이 깊숙이 파고 및 귀하의 질문에 대한 완전하고 정확한 답변을하지 않습니다 싶어으로

String[] polylines; 
String[] streetArray=new String[0]; 
String[] empty=new String[0]; 
ArrayList vlist=new ArrayList(); 
float panX; float panY; 

void setup() { 
    size(600, 600); 
    polylines=loadStrings("data/MapSeville.txt"); 
    panX=0; panY=0; 
    prepare(); 
} 

void draw() { 
    background(255); 
    PVector pan = pan(); 
    panX=panX+pan.x; 
    panY=panY+pan.y; 

    loadPixels(); 
    for(int i=0;i<vlist.size();i++){ 
    vertexgroup vg= (vertexgroup) vlist.get(i); 
    for(int j=1;j<vg.listcoord.size();j++){ 
     Vertice v2=(Vertice) vg.listcoord.get((j-1)); 
     Vertice v1=(Vertice) vg.listcoord.get(j); 
     float x1=v1.coord.x+panX; 
     float y1=v1.coord.y+panY; 
     float x2=v2.coord.x+panX; 
     float y2=v2.coord.y+panY; 
     drawline(x1, y1, x2, y2);//slow :(comment out this line and enable the next to see how smooth it can go 
     //drawline(y1, x1, y2, x2);//fast! 
    } 
    } 
    updatePixels(); 
} 

////////////////////////////// functions //////////////////////////////////////////////// 

PVector pan(){ 
    PVector p; 
    if (mousePressed){ 
    p = new PVector(mouseX-pmouseX, mouseY-pmouseY); 
    }else{ 
    p=new PVector(0,0); 
    } 
    return p; 
} 

//////////////////// 

void drawline(float x1, float y1, float x2, float y2){ 
    int X1=int(x1); 
    int X2=int(x2); 
    color pink = color(0); 
    if((X2>X1)){ 
     for (int i=0; i<=int(x2-x1); i++){ 
     if (((i+X1)>=0)&&((i+X1)<=width)){ 
      int g=int(y1+((y2-y1)/(x2-x1))*i)*(width) + i + X1; 
      if ((g<width*height)&&(g>=0)){ 
      pixels[g]=pink; 
      } 
     } 
     } 
    }else if((X2<X1)){ 
     for (int i=0; i<=int(x1-x2); i++){ 
     if (((i+X2)>=0)&&((i+X2)<=width)){ 
      int f=int(y2+((y2-y1)/(x2-x1))*i)*(width) + i + X2; 
      if ((f<width*height)&&(f>=0)){ 
      pixels[f]=pink; 
     } 
     } 
    } 
    } 
} 

////////////////////////////// 

void prepare(){ 
    for (int i=1;i<polylines.length;i++) { 
    String[] pts = split(polylines[i], ";"); 

    if (pts.length>3) { 
     streetArray=append(streetArray, polylines[i]); //adds coords strings to the array streetArray 
    } 
    if (pts.length==1) { //this is when the coords of a polyline ends: pts[0]==>"next" 
     vertexgroup vgroup; 
     vgroup=new vertexgroup(create_polyArr(streetArray)); // this function is defined right below 
     vlist.add(vgroup); 
     streetArray=empty; 
    }}} 

    ArrayList create_polyArr(String[] streetpts) { 
    ArrayList arrpts=new ArrayList(); 
    arrpts.clear(); 

    for (int i=0;i<streetpts.length;i++){ //iterates through coords strings of the polyline contained in streetpts 
     String[] pts = split(streetpts[i], ";"); 
     float x=float(pts[3]);//get the x coord 
     float y=float(pts[2]);//get the y coord 
     x=((x+5.95)*15000+width/2);//scale and center coord x 
     y=((-y+37.40)*15000+height/2);//scale and center coord y 
     PVector coord=new PVector(x,y); 
     Vertice govertex=new Vertice(coord); 
     arrpts.add(govertex); 
    } 
    return(arrpts); 
    } 

////////////////////// end functions ////////////////////////////////// 

//////////////////////// classes /////////////////////////////////////// 

class vertexgroup{ 

    ArrayList listcoord; 

    vertexgroup(ArrayList _listcoord){ 
    listcoord=_listcoord; 
    }} 

    ////////// 

    class Vertice{ 

    PVector coord; 

    Vertice(PVector _coord){ 
    coord=_coord; 
    }} 

    ////////////////////// end classes /////////////////////////////////////// 
+0

함수 drawline에서 x2> x1 또는 x1> x2 중 하나이기 때문에 0으로 나누기 상황이 없어야하지만 결코 x1 == x2가 아님을 유의하십시오. – fartagaintuxedo

+0

스케치의 느린 빠른 버전을 저에게 업로드했습니다. 사이트의 차이는 이제 막대한 것입니다. 첫 번째는 전혀 움직이지 않으며 다른 하나는 부드러운 종류. 여기서 확인할 수 있습니다 : [빠른] (http://417i.com/StackOverflow/FastMap) 및 [느린] (http : // 417i.com/StackOverflow/SlowMap) 정말 그 코드를 이해할 수는 없지만 실제로는 같은 코드이지만 하나는 90도 회전했습니다. – fartagaintuxedo

답변

1

불행하게도 나는 많은 시간을 가지고 있지 않지만, 나는 몇 가지 관찰과 수도 제안을 할 도움.

코드가 실제로 나쁘지 않고 브라우저 외부에서 원활하게 실행됩니다. 이가 더 복잡한로 간단한에서 몇 가지 제안이 있습니다 작업 방법에 따라 : size(600,600,P2D) :

  1. 단순히 다른 렌더러를 사용합니다. 나는 ~ 35fps가 ~ JAVA2D 및 ~ 60fps로 나타났습니다. P2D
  2. 픽셀을 삭제하고 항상 다시 그려야합니다. 그림을 그리는 방법에 따라 큰 PGraphics 또는 PImage을 만들고 설정에서 전체지도를 한 번 그 다음 을 draw()에 넣은 다음 mapGraphics.draw(0,0); 또는 mapImage.draw(0,0);을 입력하십시오. 그래도 최대 이미지 크기가 있다고 상상해보십시오. 꽤 비슷한 OpenGL을 사용하여 텍스쳐지도 솔루션을 탐색 할 수 있습니다.
  3. 테스트하지는 않았지만 PShape을 통해 처리 할 때 사용할 수있는 .svg 형식을 사용해 보았습니까?

짧은 설명 : 연속적으로 다시 그리기보다는 다른 렌더러 및/또는 '캐시'를 사용하십시오.

시간이 있고 속도가 다른 곳을 알아 내려면 명령 줄/terminal에서 jvisualvm을 시작하고 CPU 사용 현황 스냅 샷을 비교하십시오.

마지막으로 참고 사항 :.?

나는 두 버전 fill(0);text((int)frameRate+" fps",10,20); 또는 frame.setTitle((int)frameRate+" fps");를 호출하여 동일한 프레임 속도를 (얻고있다

그것은 당신이 단순히 다른 팬 속도/양을 얻을 수 있을까

+0

모든 팁을 주셔서 감사합니다. 귀하의 제안을 테스트 할 시간이 필요하지만 정말 도움이됩니다. – fartagaintuxedo