processing由两个基本函数构成,setup和draw函数
窗口
窗口
size();
背景
background()
帧速率
frameRate( );
循环
loop
基本形状
点
point(x1, y1);
基本形状
线
line(x1, y1, x2, y2);
三角形
triangle(x1, y1, x2, y2, x3, y3);
四边形
quad(x1, y1, x2, y2, x3, y3, x4, y4);
矩形
rect(x, y, width, height);
圆
ellipse(x, y, width, height);
扇形
arc(x, y, width, start, stop);
start QUARTER_PI PI HALF_PI
自定义图形
自定义图形
beginShape();
vertex(x, y);
vertex(x, y);
endShape(CLOSE);
笔触
平滑
smooth() nosmooth()
画笔粗细
strokeWeight()
画笔属性
线线之间的连接模式 strokeJoin(ROUND/BEVEL)
线 strokeCap(ROUND/SQUARE)
颜色
颜色
fill(r, g, b, alpha);
noFill();
stroke();
noStroke();
图片
导入图片
PImage img;
img = loadImage(“地址”);
image(img,0,0)
保存图片
save("a.jpg");
saveFrame("a.gif");
字体
字符
textSize( );
textAlign(CENTER);
text(key, x, y);
图片滤镜
2020年4月8日 13:54 周三
高斯模糊 BLUR(level)
level指定模糊程度
色调分离 POSTERIZE(level)
限制每个色彩通道的颜色,由level 参数指定颜色数量
阈值化 THRESHOLD(level)
按照像素的值与level 定义的阈值的大小关系,将每个像素设为黑或白色
灰度化 GRAY(level)
将图像色彩转为灰度模式
膨胀 DILATE()
增加明亮区域,由level 参数指定增加的层度
电脑
鼠标变量获取
mouseX, mouseY pmouseX, pmouseY
点击
mousePressed true/false
3D图形
void setup(){
size(500,500,P3D);
}
void draw(){
//translate(width / 2, height / 2);
//sphere(100);
//ambientLight(255,0,0, 0,width/2,0);
//translate(width/2,height/2);
//sphere(100);
//pointLight(255,0,0, 35,40,0);
//translate(width/2,height/2);
//sphere(100);
translate(width/2,height/2);
float thetaX = map(mouseY,0,width,-PI,PI);
float thetaY = map(mouseX,0,width,-PI,PI);
rotateX(thetaX);
rotateY(thetaY);
sphere(100);
}
球世界
ParticleSystem ps;
void setup() {
fullScreen(P3D);
colorMode (RGB, 256);
ps = new ParticleSystem();
}
void draw() {
background(0);
ps.run();
}
class ParticleSystem{
final int MAX_CNT = 200;
ArrayList<Particle> particles;
float thetaX,thetaY;
ParticleSystem(){
particles = new ArrayList();
}
void run(){
addParticle();
update();
display();
}
void addParticle(){
if(particles.size() >= MAX_CNT)
return;
particles.add(new Particle());
}
void update(){
for(int i = particles.size() - 1; i >= 0; i--){
Particle p = particles.get(i);
if(p.dead()){
particles.remove(p);
continue;
}
p.update();
}
thetaX += (mouseX - thetaX) * 0.04;
thetaY += (mouseY - thetaY) * 0.04;
}
void display() {
translate(width / 2, height / 2);
ambientLight (30, 20, 80);
pointLight (255, 0, 0, 200.0, 0.0, 200.0);
pointLight (255, 0, 0, -200.0, 0.0, -200.0);
pointLight (0, 255, 0, 0.0, 200.0, 200.0);
pointLight (0, 255, 0, 0.0, -200.0, -200.0);
rotateX(thetaY * 0.01);
rotateY(thetaX * 0.01);
for (Particle p : particles) {
p.display();
}
}
}
class Particle {
PVector velocity, acceleration, location;
float lifespan, lifeRate;
float size;
Particle() {
float x = random(-width/2, width/2);
float y = height / 2 + 40;
float z = random (-1000.0, 1000.0);
location = new PVector(x, y, z);
acceleration = new PVector();
velocity = new PVector(0, random(-20, -1), 0);
size = random(120);
lifeRate = -velocity.y;
lifespan = y;
}
boolean dead() {
if (lifespan < 0)return true;
return false;
}
void update() {
lifespan -= lifeRate;
velocity.add(acceleration);
location.add(velocity);
}
void display() {
pushMatrix();
noStroke ();
translate(location.x, location.y, location.z);
sphere(size);
popMatrix();
}
}