2016-11-21 8 views
1

私はPShape SVG multipyを作成しようとしています。変数(CSVファイルからインポートしている変数)が変更されるたびに新しいシェイプが作成されます。私はforを使ってみましたが、私が与えている可変範囲を尊重していません。望むだけ多くのSVGを作成します。基本的に私がやろうとしていることは、変数がXの怒りの間に21のデータがあることを示している場合、SVGのコピーを固定距離で1つと他のものの間に21個コピーすることです。pshape multiple multiple

Table table; 

PShape tipi2; 
PShape tipi3; 


void setup() { 

    size (1875, 871); 
    table = loadTable("WHO.csv", "header"); 
    tipi2 = loadShape("tipi-02.svg"); 


} 


void draw() { 

    background(0); 


    for (TableRow row : table.rows()) { 

    int hale = row.getInt("Healthy life expectancy (HALE) at birth (years) both sexes"); 


    } 
    tipi2.disableStyle(); 


noStroke(); 

for(int i = 0 ;i<=1800;i=i+33){ 


pushMatrix(); 

    translate(0,89.5); 

     if(hale > 40 && hale < 60){ 

shape(tipi2,i,0); 

popMatrix(); 
} 

} 
+0

あなたは読みやすくするために、あなたのインデントをクリーンアップすることはできますか? –

+0

@LauraFlorez私たちがテストしやすくするために.svg(コードスニペットとして)と.csv(リンクとして)ファイルを投稿できますか? –

答えて

1

あなたの現在のコードで改善することができたカップルの事物事のカップルがあります。

  • hale変数の可視性(または範囲)が唯一のこのループ内にある:for (TableRow row : table.rows()) {
  • 描画スタイル(noStroke()/ disableStyle()など)はあまり変更されないので、setup()では1回だけ設定できます。draw()
  • forループを移動することができますfr OM 0 1800からfor (TableRow row : table.rows()) {ループ内で、それは非常に効率的ではないかもしれません:

は、ここで私が何を意味するかです:

Table table; 

PShape tipi2; 
PShape tipi3; 


void setup() { 

    size (1875, 871); 
    table = loadTable("WHO.csv", "header"); 
    tipi2 = loadShape("tipi-02.svg"); 

    //this styles could be set once in setup, rather than multiple times in draw(); 
    tipi2.disableStyle(); 
    noStroke(); 

    background(0); 


    for (TableRow row : table.rows()) { 

    int hale = row.getInt("Healthy life expectancy (HALE) at birth (years) both sexes"); 

    for (int i = 0; i<=1800; i=i+33) { 

     pushMatrix(); 

     translate(0, 89.5); 
     //hale is visible within this scope, but not outside the for loop 
     if (hale > 40 && hale < 60) { 

     shape(tipi2, i, 0); 

     } 
     //popMatrix(); should be called the same amount of times as pushMatrix 
     popMatrix(); 
    } 

    } 
} 


void draw() { 


}