代码本色的学习——我的创意编程

任务内容

主题:创作一组编程习作,体现随机行为及牛顿运动学
要求:编程语言与工具:编程工具不限;作品:参考《代码本色》的第0~4章内容及其实例程序(自行在processing内下载),针对这5章分别编写1个习作(一共5个),每个习作都有不少于2个案例参考,且必须有一定的拓展;

第0章

第0章我实现的是一个颜色渐变的由噪声控制前进的线段。
成果:
在这里插入图片描述
色彩平滑找了很多方法,最后是定义了数组才成果的,行走的线是使用了噪声随机,让方向产生不确定性,最后的成果有点像小时候吃的彩色的牛皮糖。
关键代码:
颜色平滑:

Walker w;
float theta2 = 0;
public static int[] hsb2rgb(float h, float s, float v,int rgb[]) {
    assert Float.compare(h, 0.0f) >= 0 && Float.compare(h, 360.0f) <= 0;
    assert Float.compare(s, 0.0f) >= 0 && Float.compare(s, 1.0f) <= 0;
    assert Float.compare(v, 0.0f) >= 0 && Float.compare(v, 1.0f) <= 0;
    float r = 0, g = 0, b = 0;
    int i = (int) ((h / 60) % 6);
    float f = (h / 60) - i;
    float p = v * (1 - s);
    float q = v * (1 - f * s);
    float t = v * (1 - (1 - f) * s);
    switch (i) {
      case 0:r = v;g = t;b = p;break;
      case 1:r = q;g = v;b = p;break;
      case 2:r = p;g = v;b = t;break;
      case 3:r = p;g = q;b = v;break;
      case 4:r = t;g = p;b = v;break;
      case 5:r = v;g = p;b = q;break;
      default:break;
    }
     rgb[0] = (int)(r*255);
     rgb[1] = (int)(g*255);
     rgb[2] = (int)(b*255);
    return new int[] { (int) (r * 255.0), (int) (g * 255.0),(int) (b * 255.0) };
  }

噪声:

tx+= 0.01;
    ty+= 0.01;
    x = map(noise(tx), 0, 1, 0, width);
    y = map(noise(ty), 0, 1, 0, height);

第1章

第1章运用了随机和跟随。
成果:
在这里插入图片描述
鼠标跟随比较好做,但是淡去的影子就有点费脑筋,因为还用了颜色的随机,所以不可以直接用一种颜色做,还得在画的时候制造“延迟”。本来想做的效果是小时候玩的泡泡机,产生的那种五颜六色的泡泡,但是做出来没有很像。
关键代码:

void display() {
   time++;
   if(time%2<1)
   {
   //stroke(1);
   noStroke();
   
   fill(c,alpha);
   ellipse(position.x,position.y,r,r);
   position.x=position.x+2;
   position.y=position.y+2;
   }
 }

第2章

第二章做的是引力,中心是可以通过鼠标移动,并且在每次移动的时候,会出现最新的引力追随者,而且是大小随机,颜色随机。
成果:
在这里插入图片描述通过点击,可以改变中心球的位置,但是动图里没移动,其实是可以的,然后与此同时,会出现一个新的被引力吸引的小球,被吸引过来,然后进入黑色区域之后,阻力会变大。

关键代码:
水部分的拉拽:

  PVector drag(Mover m) {
   float speed = m.velocity.mag();
   float dragMagnitude = c * speed * speed;
   PVector dragForce = m.velocity.get();
   dragForce.mult(-1);
   dragForce.normalize();
   dragForce.mult(dragMagnitude);
   return dragForce;
 }

引力:

PVector attract(Mover m) 
 {
   PVector force = PVector.sub(position,m.position);   // Calculate direction of force
   float d = force.mag();                              // Distance between objects
   d = constrain(d,1.0,10.0);                        // Limiting the distance
   force.normalize();                                  // Normalize vector
   float strength = (G * mass * m.mass) / (d * d);      // Calculate gravitional force magnitude
   force.mult(strength);                                  // Get force vector --> magnitude * direction
   return force;
 }

第3章

做了摇摆和跟随,因为做了很多追随者,所以用了很多随机数。
成果:
在这里插入图片描述
鼠标点击可以拖拽这个小球,然后追随方块会因为拖拽改变追随方向。
关键代码:

  void update() {
    if (!dragging) {
      float gravity = 0.4;                              // Arbitrary constant
      aAcceleration = (-1 * gravity / r) * sin(angle);  // Calculate acceleration (see: http://www.myphysicslab.com/pendulum1.html)
      aVelocity += aAcceleration;                 // Increment velocity
      aVelocity *= damping;                       // Arbitrary damping
      angle += aVelocity;                         // Increment angle
    }
  }

第4章

最后做了粒子系统,实现了火焰被风吹的感觉,鼠标代表了风。
成果:
在这里插入图片描述
现在的风看起来有点钝钝的,还不够完善
关键代码:
void draw() {
background(0);
x+=0.001;
y+=0.001;
float force =(noise(x,y)- 0.4)/forceupdown ;
force = abs(force);
if(mouseX<width/2)
{
force = -force;
}
PVector wind = new PVector(force,0);
ps.applyForce(wind);
ps.run();
ps2.applyForce(wind);
ps2.run();
for (int i = 0; i < 2; i++) {
ps.addParticle();
}
for (int i = 0; i < 2; i++) {
ps2.addParticle();
}
if(mousePressed)
{
if(forceupdown <0.5)
{
forceupdown = forceupdown-0.01;
}
else
{
forceupdown = 0.5;
}
}
else
{
forceupdown = 2;
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值