-1

我有這個工作正常的處理代碼,但有很多對象實例化和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++; 
     } 
    } 
    } 
} 

UPDATE:建議之後,我做了一些閱讀和更新以下。我創建了一個包含常用方法的多面體抽象類,然後使每種類型的多面體類擴展了抽象類。

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++; 
     } 
    } 
    } 
} 
+0

您可以使用java的[for/each](https://docs.oracle.com/)優化'for(int i = 0; i

+0

謝謝。好得多...... – davidcool

+0

這似乎是一個很好的問題,但它可能更適合[代碼評論](http://codereview.stackexchange.com/),這是一個詢問有關改善工作代碼的網站。在這裏堆棧溢出,這是邊緣線外的話題。 –

回答

0

使用InheritancePolymorphism。這兩個面向對象的編程概念應該幫助你消除大量的代碼量(因爲你將使用方法/函數而不是代碼複製)。一旦你使用了這些,你也可以使用一組Solid(如果我們要調用這些實體的父類),並進一步簡化了大量的代碼。使用數組來存儲你的text()調用的參數,你甚至不需要你的switch case語句。

以上所有內容僅提高了您的代碼可理解性和可管理性。它不會影響性能(好吧,函數調用的開銷很小,但在這種情況下,這沒有實際意義)。

+0

謝謝!我做了一些閱讀,開始有意義。我修改了我的代碼並在上面轉貼了。 – davidcool