构建此处理代码的更好方法是什么?

时间:2015-04-19 20:57:09

标签: performance structure processing instantiation processing-efficiency

我有这个处理代码可以正常工作,但是对象实例化和case语句有很多重复的代码......在速度/编写代码紧凑性方面有没有更经济的方法呢?

//platonic solids
public Tetrahedron poly0;
public Octahedron poly1;
public Cube poly2;
public Icosahedron poly3;
public Dodecahedron poly4;
//kepler-poinsot solids
public SmallStellatedDodecahedron poly5;
public GreatDodecahedron poly6;
public GreatStellatedDodecahedron poly7;
public GreatIcosahedron poly8;
//archimedean solids
public TruncatedOctahedron poly9;
//versi-regular polyhedra
public Tetrahemihexahedron poly10;
public Cubohemioctahedron poly11;
public Octahemioctahedron poly12;
public SmallDodecahemidodecahedron poly13;
public GreatDodecahemidodecahedron poly14;
public SmallDodecahemicosahedron poly15;
public GreatDodecahemicosahedron poly16;
public SmallIcosihemidodecahedron poly17;
public GreatIcosihemidodecahedron poly18;
//non-regular toroidal solids
public OctagonalIrisToroid poly19;
public float zoom = 0.025;
public int num = 0;
public PFont f;

public void setup(){
  size(1000,1000,OPENGL);
  lights();
  smooth();
  frameRate(30);
  f = createFont("Arial",24,true);
  textFont(f,24);
  textAlign(CENTER);
  //Instantiate each objects
  poly0 = new Tetrahedron();
  poly0.fillColor(int(random(255)), int(random(255)), int(random(255)), 255);
  poly0.strokeColor(int(random(255)), int(random(255)), int(random(255)), 255);

  poly1 = new Octahedron();
  poly1.fillColor(int(random(255)), int(random(255)), int(random(255)), 255);
  poly1.strokeColor(int(random(255)), int(random(255)), int(random(255)), 255);

  poly2 = new Cube();
  poly2.fillColor(int(random(255)), int(random(255)), int(random(255)), 255);
  poly2.strokeColor(int(random(255)), int(random(255)), int(random(255)), 255);

  poly3 = new Icosahedron();
  poly3.fillColor(int(random(255)), int(random(255)), int(random(255)), 255);
  poly3.strokeColor(int(random(255)), int(random(255)), int(random(255)), 255);

  poly4 = new Dodecahedron();
  poly4.fillColor(int(random(255)), int(random(255)), int(random(255)), 255);
  poly4.strokeColor(int(random(255)), int(random(255)), int(random(255)), 255);

  poly5 = new SmallStellatedDodecahedron();
  poly5.fillColor(int(random(255)), int(random(255)), int(random(255)), 255);
  poly5.strokeColor(int(random(255)), int(random(255)), int(random(255)), 255);

  poly6 = new GreatDodecahedron();
  poly6.fillColor(int(random(255)), int(random(255)), int(random(255)), 255);
  poly6.strokeColor(int(random(255)), int(random(255)), int(random(255)), 255);

  poly7 = new GreatStellatedDodecahedron();
  poly7.fillColor(int(random(255)), int(random(255)), int(random(255)), 255);
  poly7.strokeColor(int(random(255)), int(random(255)), int(random(255)), 255);

  poly8 = new GreatIcosahedron();
  poly8.fillColor(int(random(255)), int(random(255)), int(random(255)), 255);
  poly8.strokeColor(int(random(255)), int(random(255)), int(random(255)), 255);

  poly9 = new TruncatedOctahedron();
  poly9.fillColor(int(random(255)), int(random(255)), int(random(255)), 255);
  poly9.strokeColor(int(random(255)), int(random(255)), int(random(255)), 255);

  poly10 = new Tetrahemihexahedron();
  poly10.fillColor(int(random(255)), int(random(255)), int(random(255)), 255);
  poly10.strokeColor(int(random(255)), int(random(255)), int(random(255)), 255);

  poly11 = new Cubohemioctahedron();
  poly11.fillColor(int(random(255)), int(random(255)), int(random(255)), 255);
  poly11.strokeColor(int(random(255)), int(random(255)), int(random(255)), 255);

  poly12 = new Octahemioctahedron();
  poly12.fillColor(int(random(255)), int(random(255)), int(random(255)), 255);
  poly12.strokeColor(int(random(255)), int(random(255)), int(random(255)), 255);

  poly13 = new SmallDodecahemidodecahedron();
  poly13.fillColor(int(random(255)), int(random(255)), int(random(255)), 255);
  poly13.strokeColor(int(random(255)), int(random(255)), int(random(255)), 255);

  poly14 = new GreatDodecahemidodecahedron();
  poly14.fillColor(int(random(255)), int(random(255)), int(random(255)), 255);
  poly14.strokeColor(int(random(255)), int(random(255)), int(random(255)), 255);

  poly15 = new SmallDodecahemicosahedron();
  poly15.fillColor(int(random(255)), int(random(255)), int(random(255)), 255);
  poly15.strokeColor(int(random(255)), int(random(255)), int(random(255)), 255);

  poly16 = new GreatDodecahemicosahedron();
  poly16.fillColor(int(random(255)), int(random(255)), int(random(255)), 255);
  poly16.strokeColor(int(random(255)), int(random(255)), int(random(255)), 255);

  poly17 = new SmallIcosihemidodecahedron();
  poly17.fillColor(int(random(255)), int(random(255)), int(random(255)), 255);
  poly17.strokeColor(int(random(255)), int(random(255)), int(random(255)), 255);

  poly18 = new GreatIcosihemidodecahedron();
  poly18.fillColor(int(random(255)), int(random(255)), int(random(255)), 255);
  poly18.strokeColor(int(random(255)), int(random(255)), int(random(255)), 255);

  poly19 = new OctagonalIrisToroid();
  poly19.fillColor(int(random(255)), int(random(255)), int(random(255)), 255);
  poly19.strokeColor(int(random(255)), int(random(255)), int(random(255)), 255);
} //end setup()

public void draw(){
  background(0);
  //camera(width/2, height/2, 300, width/2, height/2, 0, 0, 1, 0);
  pointLight(200, 200, 200, width/2, height/2, 200);
  ambientLight(102, 102, 102);
  spotLight(51, 102, 126, 80, 20, 40, -1, 0, 0, PI/2, 2);
  translate(width/2, height/2, 0);
  switch(num) {
    case 0:
      if (mousePressed) {
        poly0.rotate(mouseX/100., mouseY/100., 0);
      } else {
        poly0.rotate(radians(frameCount%360),radians(frameCount%360),0);
      }
      poly0.scaleFactor(15000);
      poly0.render();
      text("Tetrahedron",0,400);
      break;
    case 1: 
      if (mousePressed) {
        poly1.rotate(mouseX/100., mouseY/100., 0);
      } else {
        poly1.rotate(radians(frameCount%360),radians(frameCount%360),0);
      }
      poly1.scaleFactor(15000);
      poly1.render();
      text("Octahedron",0,400);
      break;
    case 2: 
      if (mousePressed) {
        poly2.rotate(mouseX/100., mouseY/100., 0);
      } else {
        poly2.rotate(radians(frameCount%360),radians(frameCount%360),0);
      }
      poly2.scaleFactor(14000);
      poly2.render();
      text("Cube",0,400);
      break;
    case 3: 
      if (mousePressed) {
        poly3.rotate(mouseX/100., mouseY/100., 0);
      } else {
        poly3.rotate(radians(frameCount%360),radians(frameCount%360),0);
      }
      poly3.scaleFactor(12000);
      poly3.render();
      text("Icosahedron",0,400);
      break;
    case 4: 
      if (mousePressed) {
        poly4.rotate(mouseX/100., mouseY/100., 0);
      } else {
        poly4.rotate(radians(frameCount%360),radians(frameCount%360),0);
      }
      poly4.scaleFactor(6000);
      poly4.render();
      text("Dodecahedron",0,400);
      break;
    case 5: 
      if (mousePressed) {
        poly5.rotate(mouseX/100., mouseY/100., 0);
      } else {
        poly5.rotate(radians(frameCount%360),radians(frameCount%360),0);
      }
      poly5.scaleFactor(22000);
      poly5.render();
      text("Small Stellated Dodecahedron",0,400);
      break;
    case 6: 
      if (mousePressed) {
        poly6.rotate(mouseX/100., mouseY/100., 0);
      } else {
        poly6.rotate(radians(frameCount%360),radians(frameCount%360),0);
      }
      poly6.scaleFactor(13000);
      poly6.render();
      text("Great Dodecahedron",0,400);
      break;
    case 7: 
      if (mousePressed) {
        poly7.rotate(mouseX/100., mouseY/100., 0);
      } else {
        poly7.rotate(radians(frameCount%360),radians(frameCount%360),0);
      }
      poly7.scaleFactor(25000);
      poly7.render();
      text("Great Stellated Dodecahedron",0,400);
      break;
    case 8: 
      if (mousePressed) {
        poly8.rotate(mouseX/100., mouseY/100., 0);
      } else {
        poly8.scaleFactor(22000);
        poly8.rotate(radians(frameCount%360),radians(frameCount%360),0);
      }
      poly8.render();
      text("Great Icosahedron",0,400);
      break;
    case 9: 
      if (mousePressed) {
        poly9.rotate(mouseX/100., mouseY/100., 0);
      } else {
        poly9.rotate(radians(frameCount%360),radians(frameCount%360),0);
      }
      poly9.render();
      text("Truncated Octahedron",0,400);
      break;
    case 10: 
      if (mousePressed) {
        poly10.rotate(mouseX/100., mouseY/100., 0);
      } else {
        poly10.rotate(radians(frameCount%360),radians(frameCount%360),0);
      }
      poly10.scaleFactor(15000);
      poly10.render();
      text("Tetrahemihexahedron",0,400);
      break;
    case 11: 
      if (mousePressed) {
        poly11.rotate(mouseX/100., mouseY/100., 0);
      } else {
        poly11.rotate(radians(frameCount%360),radians(frameCount%360),0);
      }
      poly11.scaleFactor(10000);
      poly11.render();
      text("Cubohemioctahedron",0,400);
      break;
    case 12: 
      if (mousePressed) {
        poly12.rotate(mouseX/100., mouseY/100., 0);
      } else {
        poly12.rotate(radians(frameCount%360),radians(frameCount%360),0);
      }
      poly12.scaleFactor(10000);
      poly12.render();
      text("Octahemioctahedron",0,400);
      break;
    case 13: 
      if (mousePressed) {
        poly13.rotate(mouseX/100., mouseY/100., 0);
      } else {
        poly13.rotate(radians(frameCount%360),radians(frameCount%360),0);
      }
      poly13.scaleFactor(6000);
      poly13.render();
      text("Small Dodecahemidodecahedron",0,400);
      break;
    case 14: 
      if (mousePressed) {
        poly14.rotate(mouseX/100., mouseY/100., 0);
      } else {
        poly14.rotate(radians(frameCount%360),radians(frameCount%360),0);
      }
      poly14.scaleFactor(20000);
      poly14.render();
      text("Great Dodecahemidodecahedron",0,400);
      break;
    case 15: 
      if (mousePressed) {
        poly15.rotate(mouseX/100., mouseY/100., 0);
      } else {
        poly15.rotate(radians(frameCount%360),radians(frameCount%360),0);
      }
      poly15.scaleFactor(12000);
      poly15.render();
      text("Small Dodecahemicosahedron",0,400);
      break;
    case 16: 
      if (mousePressed) {
        poly16.rotate(mouseX/100., mouseY/100., 0);
      } else {
        poly16.rotate(radians(frameCount%360),radians(frameCount%360),0);
      }
      poly16.scaleFactor(12000);
      poly16.render();
      text("Great Dodecahemicosahedron",0,400);
      break;
    case 17: 
      if (mousePressed) {
        poly17.rotate(mouseX/100., mouseY/100., 0);
      } else {
        poly17.rotate(radians(frameCount%360),radians(frameCount%360),0);
      }
      poly17.scaleFactor(7000);
      poly17.render();
      text("Small Icosihemidodecahedron",0,400);
      break;
    case 18: 
      if (mousePressed) {
        poly18.rotate(mouseX/100., mouseY/100., 0);
      } else {
        poly18.rotate(radians(frameCount%360),radians(frameCount%360),0);
      }
      poly18.scaleFactor(20000);
      poly18.render();
      text("Great Icosihemidodecahedron",0,400);
      break;
    case 19: 
      if (mousePressed) {
        poly19.rotate(mouseX/100., mouseY/100., 0);
      } else {
        poly19.rotate(radians(frameCount%360),radians(frameCount%360),0);
      }
      poly19.scaleFactor(8000);
      poly19.render();
      text("Octagonal Iris Toroid",0,400);
      break;
  }
} //end draw()

public void mouseWheel(MouseEvent e) {
  zoom += map(e.getCount(), -5, 5, 0.01, -0.01);
  zoom = constrain(zoom, 0.001, 0.2);
}

public void keyPressed() {
  if (key == CODED) {
    if (keyCode == LEFT) {
      if (num == 0) {
        num = 19;
      } else {
        num--;
      }
    } 
    if (keyCode == RIGHT) {
      if (num == 19) {
        num = 0;
      } else {
        num++;
      }
    } 
  }
}

更新:在建议之后我做了一些阅读并更新到下面。我创建了一个包含常用方法的Polyhedron抽象类,然后使每种类型的多面体类扩展抽象类。

ArrayList<Polyhedron> polyObjects = new ArrayList<Polyhedron>();
Polyhedron poly;
public float zoom = 0.025;
public int num = 0;
public PFont f;

public void setup(){
  size(1000,1000,OPENGL);
  lights();
  smooth();
  frameRate(30);
  f = createFont("Arial",24,true);
  textFont(f,24);
  textAlign(CENTER);
  //Instantiate each object
  polyObjects.add(new Tetrahedron());
  polyObjects.add(new Octahedron());
  polyObjects.add(new Cube());
  polyObjects.add(new Icosahedron());
  polyObjects.add(new Dodecahedron());
  polyObjects.add(new SmallStellatedDodecahedron());
  polyObjects.add(new GreatDodecahedron());
  polyObjects.add(new GreatStellatedDodecahedron());
  polyObjects.add(new GreatIcosahedron());
  polyObjects.add(new TruncatedOctahedron());
  polyObjects.add(new Tetrahemihexahedron());
  polyObjects.add(new Cubohemioctahedron());
  polyObjects.add(new Octahemioctahedron());
  polyObjects.add(new SmallDodecahemidodecahedron());
  polyObjects.add(new GreatDodecahemidodecahedron());
  polyObjects.add(new SmallDodecahemicosahedron());
  polyObjects.add(new GreatDodecahemicosahedron());
  polyObjects.add(new SmallIcosihemidodecahedron());
  polyObjects.add(new GreatIcosihemidodecahedron());
  polyObjects.add(new OctagonalIrisToroid());

  for (int i = 0; i < polyObjects.size(); i++) {
    poly = polyObjects.get(i);
    poly.fillColor(int(random(255)), int(random(255)), int(random(255)), 255);
    poly.strokeColor(int(random(255)), int(random(255)), int(random(255)),     255);
  }

} //end setup()

public void draw(){
  background(0);
  //camera(width/2, height/2, 300, width/2, height/2, 0, 0, 1, 0);
  pointLight(200, 200, 200, width/2, height/2, 200);
  ambientLight(102, 102, 102);
  spotLight(51, 102, 126, 80, 20, 40, -1, 0, 0, PI/2, 2);
  translate(width/2, height/2, 0);
  poly = polyObjects.get(num);
  if (mousePressed) {
      poly.rotate(mouseX/100., mouseY/100., 0);
    } else {
      poly.rotate(radians(frameCount%360),radians(frameCount%360),0);
    }
  poly.scaleFactor(5000);
  poly.render();
  text(poly.name(),0,400);
} //end draw()

public void mouseWheel(MouseEvent e) {
  zoom += map(e.getCount(), -5, 5, 0.01, -0.01);
  zoom = constrain(zoom, 0.001, 0.2);
}

public void keyPressed() {
  if (key == CODED) {
    if (keyCode == LEFT) {
      if (num == 0) {
        num = 19;
      } else {
        num--;
      }
    } 
    if (keyCode == RIGHT) {
      if (num == 19) {
        num = 0;
      } else {
        num++;
      }
    } 
  }
}

1 个答案:

答案 0 :(得分:0)

使用InheritancePolymorphism。这两个面向对象的编程概念应该可以帮助您删除大量的代码大小(因为您将使用方法/函数而不是代码重复)。一旦你使用了这些,你也可以使用Solid的数组(如果我们要调用那些实体的父类),并进一步简化你的很多代码。使用数组存储text()调用的参数,您甚至不需要切换案例语句。

以上所有内容只会提高您的代码可理解性和可管理性。它不会影响性能(好的,函数调用的开销很小,但在这种情况下,它没有实际意义)。

相关问题