【processing效果】2.2.运用循环与条件实现复杂事件

循环语句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),可实现随机间距的复制事件。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一叶屋檐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值