创意编程
实验要求
参考《代码本色》0-4章节,针对每个章节,编写5个习作。要求每个习作至少参考2个案例且必须有一定拓展。
本次作业按照规定要求认真学习书上相关内容,并运行浏览学习了每一段给出的范例代码。每一个习作都至少参考2个案例并且结合自己的想法加以拓展。
代码本色 第0章 引言
在这一章中介绍了随机和噪声,在书中介绍的原理我们看的很清楚,在随机游走的过程中有两种情况一个是有五种可能行走,一个是九种可能行走:
通过参考随机游走和噪声函数的学习我制作了一个可以随机游走且利用noise函数来实现颜色的变化:
class Walker{
float x,y;
float tx,ty;
float r,b,g;
float tr,tb,tg;
Walker()
{
tx=0;
ty=1000;
tr=0;
tb=200;
tg=200;
}
void step()
{
x=map(noise(tx),0,1,0,width);
y=map(noise(ty),0,1,0,height);
tx+=0.01;
ty+=0.01;
}
void c() //color
{
r=map(noise(tr),0,1,0,255);
b=map(noise(tb),0,1,0,255);
g=map(noise(tg),0,1,0,255);
tr+=0.05;
tb+=0.05;
tg+=0.05;
}
}
Walker w;
void setup()
{
size(500,500);
w=new Walker();
}
void draw()
{
w.step();
w.c();
fill(w.r,w.b,w.g);
ellipse(w.x,w.y,30,30);
}
代码本色 第1章 向量
通过对这一章的processing的学习,学到了一个很有用的东西就是PVector的使用,他是一个作为一种数据结果其存储,有利于我们对仿真的实现,再加上同上上一章节对于随机的学习我实现了一个出现任意小球,在到达一个边界以后就会到另一边:
int ball=20;
class Mover
{
PVector[] location=new PVector[ball];
PVector[] v=new PVector[ball];
PVector[] acceleration=new PVector[ball];
Mover()
{
for(int i=0;i<ball;i++)
{
location[i]=new PVector(random(width),random(height));
v[i]=new PVector(random(-2,2),random(-2,2));
acceleration[i]=PVector.random2D();
}
}
void update()
{
for(int i=0;i<ball;i++)
{
PVector dir=new PVector(0,0);
for(int j=0;j<ball;j++)
{
if(i==j)
{
}
else
{
PVector tempdir=PVector.sub(location[j],location[i]);
dir.add(tempdir);
}
}
dir.normalize();
dir.mult(0.5);
acceleration[i]=dir;
v[i].add(acceleration[i]);
v[i].limit(10);
}
for(int i=0;i<ball;i++)
{
location[i].add(v[i]);
}
}
void display()
{
stroke(0);
fill(175);
for(int i=0;i<ball;i++)
{ellipse(location[i].x,location[i].y,16,16);
}
}
void checkEdges()
{
for(int i=0;i<ball;i++)
{
if(location[i].x>width)
{
location[i].x=0;
}
else if(location[i].x<0)
{
location[i].x=width;
}
if(location[i].y>height)
{
location[i].y=0;
}
else if(location[i].y<0)
{
location[i].y=height;
}
}
}
}
Mover mover;
void setup()
{
size(480,480);
mover=new Mover();
smooth();
}
void draw()
{
fill(255,25);
rect(0,0,480,480);