文章目录
Chap 0 随机游走——perlin噪声生成水波
0.1 最终效果
0.2 知识点
1.Perlin噪声同时生成x坐标和y坐标。
2.map()函数将(0,1)范围映射到范围(200,300);
3.对于Perlin噪声,在这里,我们将x坐标跨度 xoff 设为0.05,y坐标跨度 yoff设为0.01;
4.通过beginshape()和 endShape(CLOSE)将像素点画成一个图形(即下部分的水波);
5.水的颜色变化,我们也可以采用Perlin噪声生成。为了更像水,在此,我们只将RGB颜色的R分量用噪声生成,GB颜色不变,使整体色调保持水绿色。map()函数将(0,1)范围映射到范围(0,255);
colorcc = map(noise(xoff, yoff), 0, 1, 0,255);
fill(colorcc,200,200);
参考《代码本色》Chap0 引言——06Perlin噪声
一维Perlin噪声示意图
二维Perlin噪声
map()函数的映射关系
0.3 具体代码
// 二维Perlin噪声
float yoff = 0.0;
float colorcc =0;//颜色
void setup() {
size(640, 360);
}
void draw() {
background(237);
//fill(100, 149 ,237);
beginShape();
float xoff = 0;
for (float x = 0; x <= width; x += 10) {
float y = map(noise(xoff, yoff), 0, 1, 200,300);
vertex(x, y);
xoff += 0.05;
}
yoff += 0.01;
vertex(width, height);
vertex(0, height);
endShape(CLOSE);
colorcc = map(noise(xoff, yoff), 0, 1, 0,255);
fill(colorcc,200,200);
}
Chap01 向量
1.1最终效果
参考《代码本色》Chap01向量
1.2知识点
物理
1.新位置 = 原位置 + 速度;
2.新速度 = 原速度 + 加速度;
3.速度、加速度具有大小和方向,可以用向量表示。
processing
1.限制范围的函数limit(),constrain()。
2.PVector类的一些操作:
1.3代码
1.随机生成10个Mover类(粉色圆)和10个Mover2类(蓝色正方形);
2.Mover类一直跟随鼠标移动,在鼠标所在点的一定范围内活动;
3.Mover类在距离鼠标所在点一定范围内受到加速度的作用,超过范围不受加速度作用。
主程序代码:
Mover[] movers = new Mover[10];
Mover2[] movers2 = new Mover2[10];
void setup(){
size(500,500);
background(255);
for(int i=0;i<movers.length;i++){
movers[i]=new Mover();
}
for(int i=0;i<movers2.length;i++){
movers2[i]=new Mover2();
}
}
void draw(){
fill(255);
rect(0,0,width,height);
for (int i = 0; i < movers.length; i++) {
movers[i].update();
movers[i].display();
movers[i].checkboarder();
movers2[i].update();
movers2[i].display();
movers2[i].checkboarder();
}
}
Mover类:
1.变量:位置,速度,加速度,最高速度;构造函数: Mover() 。
2.void update()函数:计算加速度,然后更新位置和速度。
3.void display()函数:用粉色圆表示, fill(255, 174, 185);
ellipse(position.x,position.y,10,10);
4.void checkboarder()函数:确定边界 。
class Mover {
PVector position;
PVector velocity;
PVector acceleration;
// The Mover's maximum speed
float topspeed;
Mover() {
// Start in the center
position = new PVector(random(width),random(height));
velocity = new PVector(random(1), random(1));
topspeed = 5;
}
void update() {
// Compute a vector that points from position to mouse
PVector mouse = new PVector(mouseX,mouseY);
acceleration = PVector.sub(mouse,position);
// Set magnitude of acceleration
//acceleration.setMag(0.2);
acceleration.normalize();
acceleration.mult(0.2);
// Velocity changes according to acceleration
velocity.add(acceleration);
// Limit the velocity by topspeed
velocity.limit(topspeed);
// position changes by velocity
position.add(velocity);
}
void display() {
stroke(0);
strokeWeight(1);
fill(255, 174, 185);
ellipse(position.x,position.y,10,10);
}
void checkboarder() {
if ((position.x > width) || (position.x < 0)) {
velocity.x = velocity.x * -