自定义绘制线功能,为什么坐标方向会减慢程序的速度



在处理过程中,我正在编写自己的绘制线函数,以提高从矢量文件(只有一个.txt文件,其中包含一组绘制seville地图的坐标)绘制大型直线的速度。我正在使用pixel[int]方法来构建这样的函数,但由于某种原因,我无法解释当我输入这样的坐标时,程序运行得非常快:drawline(y1,x1,y2,x2),但当它应该是什么时,速度相当慢:drawlline(x1,y1,x2,y2)对此有合理的解释吗?

.txt代码的结构很简单,如下所示(完整的.txt文件可以从文件夹下载):

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 ///////////////////////////////////////

不幸的是,我没有足够的时间深入研究这个问题,也没有一个完整准确的答案来回答你的问题,但我确实有一些观察和建议可能会有所帮助。

你的代码实际上还不错,在浏览器之外运行得很顺利。根据你想要的工作方式,有一些建议,从简单到复杂:

  1. 只需使用不同的渲染器:size(600,600,P2D)。我注意到JAVA2DP2D的帧速率分别为35fps和60fps
  2. 您一直在清除像素并重新绘制。您可以创建一个大的PGraphics或PImage(取决于您喜欢的绘制方式),并在设置中绘制一次完整的地图,然后在draw()中使用mapGraphics.draw(0,0);mapImage.draw(0,0);。想象一下,有一个最大的图像大小。有点类似,你可以用OpenGL探索一个纹理图谱解决方案,它应该很快
  3. 我还没有测试,但你有没有尝试过使用.svg格式,你可以通过PShape在处理中使用它

长话短说:使用不同的渲染器和/或"缓存"绘图指令,而不是连续重画。

如果您有时间并想弄清楚不同的速度可能来自哪里,您可以从命令行/终端启动jvisualvm,并配置文件/比较CPU使用情况快照。

最后一点:

我得到了相同的帧速率(通过调用两个版本的fill(0);text((int)frameRate+" fps",10,20);frame.setTitle((int)frameRate+" fps");

你会不会只是得到了不同的平移速度/平移量?

相关内容

最新更新