这里实现一个二维噪声,画面像一个电视没有信号,要想获取彩色图像,可以把color()函数的参数增加,如color(bright,10,10,100)。
void setup(){
size(400,300);
}
void draw(){
loadPixels();
for (int i = 0;
i
一个聚焦STEAM教育的站点
Processing是一种具有革命前瞻性的新兴计算机语言,它的概念是在电子艺术的环境下介绍程序语言,并将电子艺术的概念介绍给程序设计师。它是 Java 语言的延伸,并支持许多现有的 Java 语言架构,不过在语法 (syntax) 上简易许多,并具有许多贴心及人性化的设计。Processing 可以在 Windows、MAC OS X、MAC OS 9 、Linux 等操作系统上使用。目前最新版本为Processing 3。以 Processing 完成的作品可在个人本机端作用,或以Java Applets 的模式外输至网络上发布。
碰到墙壁就弹开的程序,定义两个速度,分别为x轴和y轴的速度。
float x = 100;
float y = 100;
float xspeed = 1;
float yspeed = 3.3;
int diameter = 20;
void setup(){
size(400,400);
smooth();
background(255);
}
void draw(){
background(255);
x = x+xspeed;
y = y+yspeed;
if((x >width - diameter/2)||(x <diameter/2)){ xspeed = xspeed * -1;
} if((y >height - diameter / 2)||(y <diameter / 2)){
yspeed = yspeed * -1;
}
stroke(0);
fill(175);
ellipse(x,y,diameter,diameter);
}
这里用一个类文件来定义小球的位置和速度。其中的add函数用来实现位置和速度的变化
class PVector{
float x;
float y;
PVector(float x_,float y_){
x = x_;
y = y_;
}
void add(PVector v){
y = y + v.y;
x = x + v.x;
}
}
引用这个类文件,实现代码的简化。但是如果仅仅是实现这个功能,这个代码起始并没有被简化,但是可以在上面的类文件里实现更多的类似add的函数,比如减法,乘法等等。
PVector location;
PVector speed;
int diameter = 20;
void setup(){
size(400,400);
smooth();
background(255);
location = new PVector(100,100);
speed = new PVector(3.3,5);
}
void draw(){
background(255);
location.add(speed);
if((location.x > width - diameter/2)||(location.x height - diameter / 2)||(location.y
在上一篇的类文件中增加一个减法函数——sub
class PVector{
float x;
float y;
PVector(float x_,float y_){
x = x_;
y = y_;
}
void add(PVector v){
y = y + v.y;
x = x + v.x;
}
void sub(PVector v){
x = x - v.x;
y = y - v.y;
}
}
利用减法函数计算屏幕鼠标位置和中心点的距离并实时显示鼠标位置到中心点的线条。
PVector mouse;
PVector center;
void setup(){
size(400,400);
smooth();
}
void draw(){
background(255);
mouse = new PVector(mouseX,mouseY);
center = new PVector(width/2,height/2);
mouse.sub(center);
translate(width/2, height/2);
line(0,0, mouse.x,mouse.y);
}
在向量的类文件中增加一个乘法函数mult(),利用该函数对向量进行乘法运算。
class PVector{
float x;
float y;
PVector(float x_,float y_){
x = x_;
y = y_;
}
void add(PVector v){
y = y + v.y;
x = x + v.x;
}
void sub(PVector v){
x = x - v.x;
y = y - v.y;
}
void mult(float n){
x = x * n;
y = y * n;
}
}
注意,起始这里也可以做除法,把”*”换成”/”即可,或者乘以一个小于1 的数,
void div(float n){
x = x / n;
y = y / n;
}
下面的例子请分别用1.5和0.5演示
PVector mouse;
PVector center;
void setup(){
size(400,400);
smooth();
}
void draw(){
background(255);
mouse = new PVector(mouseX,mouseY);
center = new PVector(width/2,height/2);
mouse.sub(center);
mouse.mult(1.5);
//mouse.mult(0.5);
translate(width/2, height/2);
line(0,0, mouse.x,mouse.y);
}