2016-11-21 43 views
1

我試圖做一個PShape SVG multipy。我想每當一個變量(我從CSV文件導入的)發生變化時創建一個新形狀。我嘗試使用for,但它並不尊重我給它的變量範圍,它只是創建儘可能多的SVG。基本上我想要做的是,如果變量表示在X狂暴之間有21個數據,那麼在一個和另一個之間的固定距離內繪製21個SVG副本。pshape處理多個

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()中設置一次而不是一秒多次在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() { 


}