2016-11-21 58 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() { 


}