循环语句while实现大数量重复事件,并嵌套条件语句if实现复杂效果。其次,for循环语句,同上while效果相同。
小球变间距复制
input:
//小球不同间距的复制
float x = 0;
void setup(){
size(640,360);
}
void draw(){
background(0);
x = 0;
while (x < width){ //while循环语句,注意exit退出条件
if (mouseX < 1){ //通过if实现鼠标控制条件变化
x = x + 1;
}else{
x = x + mouseX;
}
fill(100);
stroke(255);
ellipse(x,180,18,18);
}
}
output:
小球变间距复制
线条变间距复制
input:
//线条不同间距复制案例
float x = 0;
float y = 0;
void setup(){
size(640,360);
}
void draw(){
background(0);
stroke(255);
strokeWeight(2); //设定直线粗细或图形外框宽度的函数
x = 100;
while (x < width){ //while循环语句
line(x,0,x,height);
if (mouseX < 1){ //通过if实现鼠标控制条件变化
x = x + 1;
}else{
x = x + mouseX;
}
}
}
output:
线条变间距复制
线条随机间距复制
input:
//循环语句实现大数量重复事件,见下线条复制事件
float x = 0;
float y = 0;
float spacing = 20;
void setup(){
size(500,400);
}
void draw(){
background(0);
spacing = spacing + random(-5,5); //线条随机间距复制事件
stroke(255);
strokeWeight(2); //设定直线粗细或图形外框宽度的函数
x = 0;
while (x < width){ //while循环语句
line(x,0,x,height);
x = x + spacing;
}
for (float y = 0;y < height;y = y + spacing){ //for循环语句,同上while效果相同
line(0,y,width,y);
}
}
output:
线条随机间距复制
注:for循环语句,同上while效果相同。其次,void draw()添加spacing = spacing + random(-2,2),可实现随机间距的复制事件。