本文主要是介绍第0章 随机游走——《processing》学习,自己完成实验的代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.第一个的尝试:
完整代码如下:
//主函数代码
Walker[] w;int total = 0;void setup() {frameRate(30);size(600, 400);w = new Walker[20];for (int i = 0; i < w.length; i++) {w[i] = new Walker();}
}void draw() {background(255);int o = int(map(mouseX,mouseY,width,1,8));//int oy = int(map(mouseY,mouseX,width,1,8));//八音度调节noiseDetail(o,0.3);//调整由Perlin噪声功能产生的细节的特征和水平。// noiseDetail(oy,0.3);if (frameCount % 30 == 0) {total = total + 1;if (total > w.length-1) {total = w.length-1;}}for (int i = 0; i < total; i++) {w[i].walk();w[i].display();}
}//调用的Walker类
class Walker {PVector position;PVector noff;Walker() {position = new PVector(width/2, height/2);noff = new PVector(random(1000),random(1000));}void display() {//frameRate(10);strokeWeight(2);int r = int(random(255));int g = int(random(255));int b = int(random(255));// int a = int(random(255));//fill(127);fill(r,g,b);stroke(0);position.x=constrain(position.x,mouseX,width);position.y=constrain(position.y,mouseY,height);ellipse(position.x, position.y, 48, 48);}// Randomly move up, down, left, right, or stay in one placevoid walk() {position.x = map(noise(noff.x),0,1,0,width);position.y = map(noise(noff.y),0,1,0,height);noff.x += 0.01;noff.y += 0.01;}
}
2.第二种尝试:
//主函数
void setup() {size(640,360);// Create a walker objectw = new Walker[10];for (int i = 0; i < w.length; i++) {w[i] = new Walker();}//w = new Walker();background(255);
}void draw() {if (frameCount % 30 == 0) {total = total + 1;if (total > w.length-1) {total = w.length-1;}}for (int i = 0; i < total; i++) {w[i].step();w[i].render();}
}//调用的Walker类
class Walker {int x, y;Walker() {x = width/2;y = height/2;}void render() {
//设置颜色float r = randomGaussian();float g = randomGaussian();float b = randomGaussian();float sd = 100; float mean = 100;r = constrain((r * sd) + mean,0,255);//repeat for g & bsd = 20; mean = 200;g = constrain((g * sd) + mean,0,255);sd = 50; mean = 0;b = constrain((b * sd) + mean,0,255);//get more gaussian numbers, this time for positionfloat xloc = randomGaussian();float yloc = randomGaussian();sd = width/10;mean = width/2;xloc = ( xloc * sd ) + mean;yloc = ( yloc * sd ) + mean;
////* int r = int(random(255));int g = int(random(255));int b = int(random(255));//int a = int(random(255));*/stroke(255);strokeWeight(10);point(x, y);noFill();stroke(123,g,255);strokeWeight(0.5);ellipse(x, y, r, r);/* stroke(r,120,255);rectMode(CENTER);rect(x-9, y-9, r/2, r/2);//正方形小块的运动*/}// Randomly move up, down, left, right, or stay in one placevoid step() {float r = random(1);// A 50% of moving towards the mouseif (r < 0.5) { int xdir = (mouseX-x);int ydir = (mouseY-y);if (xdir != 0) {xdir /= abs(xdir);} if (ydir != 0) {ydir /= abs(ydir);}x += xdir;y += ydir;} else {int xdir = int(random(-2, 2));int ydir = int(random(-2, 2));println(xdir);x += xdir;y += ydir;}x = constrain(x, 0, width-1);y = constrain(y, 0, height-1);}
}
加上这一段就会出现rect,代码的稍微改动就会出现不一样的景象:
这篇关于第0章 随机游走——《processing》学习,自己完成实验的代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!