Chap0:随机游走与噪声
根据这一章的知识学习到了随机游走类class Walker,并且将Perlin噪声和游走结合做成模型,用Perlin噪声同时生成x坐标和y坐标,再加上一点颜色渐变效果,代码如下:
class Walker{
float x,y;
float tx,ty;
float r,b,g;
float tr,tb,tg;
Walker()
{
tx=0;
ty=10000;
tr=0;
tb=1000;
tg=2000;
}
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()
{
r=map(noise(tr),0,1,100,255);
b=map(noise(tb),0,1,100,255);
g=map(noise(tg),0,1,100,255);
tr+=2;
tb+=2;
tg+=2;
}
}
Walker w;
void setup()
{
size(480,480);
background(135,206,250);
w=new Walker();
}
void draw()
{
w.step();
w.c();
fill(w.r,w.b,w.g);
ellipse(w.x,w.y,30,30);
noStroke();
}
运行结果:
结果做出来有点花里胡哨,如果颜色连贯一点看起来会好看一点,于是改一下:
void c()
{
r=map(noise(tr),0,1,100,255);
b=map(noise(tb),0,1,100,255);
g=map(noise(tg),0,1,100,255);
tr+=0.01;
tb+=0.01;
tg+=0.01;
}
就连贯了许多:
Chap1:向量
欧几里得向量(Euclidean vector,以希腊数学家欧几里 得的名字命名,也称作几何向量)它的定义是:一个既有大小又有方向的几何对象。
向量通常被绘制为一个带箭头的线段,线段的长度代表向量的大小,箭头所指的方向就是向量的方向。
先试了一个简单的,给上面随机运动的小球加上了随机向量,得到如下效果:
代码如下:
class Mover
{
PVector location1,location2;
PVector v1,v2;
PVector acceleration1,acceleration2;
Mover()
{
location1=new PVector(random(width),random(height));
v1=new PVector(random(-2,2),random(-2,2));
acceleration1=PVector.random2D();
location2=new PVector(random(width),random(height));
v2=new PVector(random(-2,2),random(-2,2));
acceleration2=PVector.random2D();
}
void update()
{
PVector dir1=PVector.sub(location2,location1);
PVector dir2=PVector.sub(location1,location2);
dir1.normalize();
dir1.mult(0.5);
acceleration1=dir1;
v1.add(acceleration1);
v1.limit(20);
location1.add(v1);
dir2.normalize();
dir2.mult(0.5);
acceleration2=dir2;
v2.add(acceleration2);
v2.limit(20);
location2.add(v2);
}
void display()
{
stroke(0);
fill(255);
ellipse(location1.x,location1.y,1,1);
ellipse(location2.x,location2.y,1,1);
}
void checkEdges()
{
if(location1.x>width)
{
location1.x=0;
}
else if(location1.x<0)
{
location1.x=width;
}
if(location1.y>height)
{
location1.y=0;
}
else if(location1.y<0)
{
location1.y=height;
}
}
}
Mover mover;
void setup()
{
mover=new Mover();
background(255);
size(500,500);
smooth();
}
void draw()
{
mover.update();
mover.checkEdges();
mover.display();
}
这一章里面我学习了向量的加减乘除以及加速度,在1.11的启发下,我想实现一个类似烟花炸开的效果,在open processing上面参考了一下之后实现了如下效果:
鼠标点击就可以实现炸开的效果,各个粒子的加速度由大到小,像是一盘颜料洒出来的感觉,大致的想法是将爆炸范围控制在一个圆内,然后设置鼠标点击,从中央炸开,具体代码如下:
final int PARTICLE_COUNT = 1000;
Particle[] particles = new Particle[PARTICLE_COUNT];
PVector direction = PVector.random2D();
PVector point;
PVector center;
void setup()
{
size(800, 800);
center = new PVector(width/