【Processing】
#Processing五项实验汇总
第0章 随机游走
实验结果:
这个实现的图案就是火箭发射的样子,两个三角形构成火箭的简易形象,然后运用了粒子的效果,实现了尾气的感觉。
相关代码如下:
main:
Walker w;
float g;
ArrayList lightPointsArr = new ArrayList();
PVector gravity = new PVector(g,g);
void setup() {
size(600, 600);
//frameRate(30);
// Create a walker object
w = new Walker();
}
void draw() {
background(0);
// Run the walker object`在这里插入代码片`
for(int i = 0;i< lightPointsArr.size();i++){
lightPoints points = (lightPoints) lightPointsArr.get(i);
points.draw();
}
for (int i = 0; i < prevX/10; i++) {
lightPointsArr.add(new lightPoints());
}
w.step();
w.render();
}
Walker
float x, y;
float tx, ty;
float prevX, prevY;
class Walker {
PVector speed;
PVector acceleration;
float circleR;
Walker() {
tx = 0;
ty = 10000;
x = map(noise(tx), 0, 1, 0, width);
y = map(noise(ty), 0, 1, 0, height);
speed = new PVector(random(-30,30), random(-30, 30));
acceleration = new PVector(0, random(0, 1));
circleR = random(50,100);
}
void render() {
stroke(255);
speed.add(acceleration);//v = v0 + a*t
acceleration.add(gravity);
gravity.mult(0);
fill(255, random(0, 250), 0, random(50, 250));
//ellipse(prevX, prevY, circleR, circleR);
triangle(prevX-20, prevY-20,prevX+20, prevY,prevX, prevY+20);
triangle(prevX-50, prevY-50,prevX+10, prevY-60,prevX-50, prevY+10);
}
// Randomly move according to floating point values
void step() {
prevX = x;
prevY = y;
x = map(noise(tx), 0, 1, 0, width);
y = map(noise(ty), 0, 1, 0, height);
tx += 0.01;
ty += 0.01;
}
}
lightPoints:
class lightPoints{
PVector location;
PVector speed;
PVector acceleration;
float circleR;
lightPoints(){
location = new PVector(prevX, prevY);
speed = new PVector(random(100), random(100));
acceleration = new PVector(0, random(0, 1));
circleR = random(5,15);
}
void draw(){
speed.add(acceleration);//v = v0 + a*t
location.add(speed);//s = s0 + v*t;
acceleration.add(gravity);
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200105204414865.gif) gravity.mult(0);
fill(255, random(0, 250), 0, random(50, 250));
ellipse(location.x, location.y, circleR, circleR);
//triangle(location.x+5, location.y,location.x-5, location.y,location.x, location.y);
}
}
这里主要运用的函数就是random(),由它随机生成的数配合其他的函数构成新的点坐标或者说是小球的颜色。
第1章 向量
实验结果:
通过鼠标实现连续画点,然后画出来的点向着鼠标运动的方向运动,代码如下:
这里的主要应用的原理就是利用好PVector mouse = new PVector(mouseX,mouseY);让生成的的点和已经生成后的点都围绕鼠标做相对应的运动
main:
float g;
ArrayList lightPointsArr = new ArrayList();
PVector gravity = new PVector(g,g);
void setup() {
size(640,640);
}
void draw() {
background(0);
for(int i = 0;i< lightPointsArr.size();i++){
lightPoints points = (lightPoints) lightPointsArr.get(i);
points.draw();
}
for (int i = 0; i < mouseX/10; i++) {
lightPointsArr.add(new lightPoints());
}
}
lightPoints:
class lightPoints{
PVector location;
PVector speed;
PVector acceleration;
float circleR;
float topspeed;
lightPoints(){
location = new PVector(mouseX,mouseY);
speed = new PVector(0,0);
acceleration = new PVector(0, random(0, 1))