我正在尝试制作PShape SVG倍数。每次变量(我从CSV文件导入)发生变化时,我都希望创建一个新形状。我尝试使用for
,但它并不尊重我给它的变量范围,它只是创建了所需数量的SVG。基本上我要做的是,如果变量表明X愤怒之间有21个数据,则在一个和另一个之间的固定距离内绘制21个SVG副本。
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 :(得分:1)
有几件事情可以解决当前代码中可以改进的问题:
hale
变量的可见性(或范围)仅在此循环内:for (TableRow row : table.rows()) {
setup()
中设置一次,而不是draw()
中每秒多次设置for (TableRow row : table.rows()) {
循环内将for循环从0移动到1800,但这可能效率不高:这就是我的意思:
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() {
}