为什么这个处理代码会产生递减轨迹?
Why does this processing code creates diminishing trail?
我正在通过处理学习 java。
代码执行以下操作。
1) 调用安装程序并初始化大小为 700,300 的 window。
2) 在设置中使用for循环初始化了一些点,并给出了随机速度。
3)自动调用draw函数。它是一个循环函数。它被一次又一次地调用。它每次用黑色矩形填充 space 并绘制所有圆圈并更新它们的位置。
4) 由于每次调用 draw() 时 rect() 命令都会清除屏幕,因此它必须只显示一个粒子并且 没有轨迹。 但确实如此。
我偶然发现了其中一个教程,代码如下
Spot[] spots; // Declare array
void setup() {
size(700, 100);
int numSpots = 70; // Number of objects
int dia = width/numSpots; // Calculate diameter
spots = new Spot[numSpots]; // Create array
for (int i = 0; i < spots.length; i++) {
float x = dia/2 + i*dia;
float rate = random(0.1, 2.0);
// Create each object
spots[i] = new Spot(x, 50, dia, rate);
}
noStroke();
}
void draw() {
fill(0, 12);
rect(0, 0, width, height);
fill(255);
for (int i=0; i < spots.length; i++) {
spots[i].move(); // Move each object
spots[i].display(); // Display each object
}
}
class Spot {
float x, y; // X-coordinate, y-coordinate
float diameter; // Diameter of the circle
float speed; // Distance moved each frame
int direction = 1; // Direction of motion (1 is down, -1 is up)
// Constructor
Spot(float xpos, float ypos, float dia, float sp) {
x = xpos;
y = ypos;
diameter = dia;
speed = sp;
}
void move() {
y += (speed * direction);
if ((y > (height - diameter/2)) || (y < diameter/2)) {
direction *= -1;
}
}
void display() {
ellipse(x, y, diameter, diameter);
}
}
它产生这个输出:
我不明白为什么它会创建这些踪迹并且这些踪迹会消失。直觉上,由于
,只有一个点应该可见
for (int i=0; i < spots.length; i++) {
spots[i].move(); // Move each object
spots[i].display(); // Display each object
}
请指出实现这一点的代码行?我不知道。
参考:https://processing.org/tutorials/arrays/
@数组:Casey Reas 和 Ben Fry
场景永远不会被清除,所以当在新帧中将新点添加到场景时,之前帧中绘制的点仍然存在。
说明
fill(0, 12);
rect(0, 0, width, height);
在整个视图上绘制一个透明的黑色矩形。因此,前几帧的斑点似乎会随着时间淡出。由于 "older" 个点被透明矩形覆盖了 may 次,它们变成了深灰色。 "Younger" 斑点只是覆盖了几次,呈浅灰色。由于填充颜色为白色(fill(255);
)
,所以立即绘制的点是白色的
如果您增加混合矩形的 alpha 值,那么斑点会 "disappear" 更快并且 "tail" 会更短。
例如
fill(0, 50);
我正在通过处理学习 java。
代码执行以下操作。
1) 调用安装程序并初始化大小为 700,300 的 window。
2) 在设置中使用for循环初始化了一些点,并给出了随机速度。
3)自动调用draw函数。它是一个循环函数。它被一次又一次地调用。它每次用黑色矩形填充 space 并绘制所有圆圈并更新它们的位置。
4) 由于每次调用 draw() 时 rect() 命令都会清除屏幕,因此它必须只显示一个粒子并且 没有轨迹。 但确实如此。
我偶然发现了其中一个教程,代码如下
Spot[] spots; // Declare array
void setup() {
size(700, 100);
int numSpots = 70; // Number of objects
int dia = width/numSpots; // Calculate diameter
spots = new Spot[numSpots]; // Create array
for (int i = 0; i < spots.length; i++) {
float x = dia/2 + i*dia;
float rate = random(0.1, 2.0);
// Create each object
spots[i] = new Spot(x, 50, dia, rate);
}
noStroke();
}
void draw() {
fill(0, 12);
rect(0, 0, width, height);
fill(255);
for (int i=0; i < spots.length; i++) {
spots[i].move(); // Move each object
spots[i].display(); // Display each object
}
}
class Spot {
float x, y; // X-coordinate, y-coordinate
float diameter; // Diameter of the circle
float speed; // Distance moved each frame
int direction = 1; // Direction of motion (1 is down, -1 is up)
// Constructor
Spot(float xpos, float ypos, float dia, float sp) {
x = xpos;
y = ypos;
diameter = dia;
speed = sp;
}
void move() {
y += (speed * direction);
if ((y > (height - diameter/2)) || (y < diameter/2)) {
direction *= -1;
}
}
void display() {
ellipse(x, y, diameter, diameter);
}
}
它产生这个输出:
我不明白为什么它会创建这些踪迹并且这些踪迹会消失。直觉上,由于
,只有一个点应该可见for (int i=0; i < spots.length; i++) {
spots[i].move(); // Move each object
spots[i].display(); // Display each object
}
请指出实现这一点的代码行?我不知道。
参考:https://processing.org/tutorials/arrays/ @数组:Casey Reas 和 Ben Fry
场景永远不会被清除,所以当在新帧中将新点添加到场景时,之前帧中绘制的点仍然存在。
说明
fill(0, 12);
rect(0, 0, width, height);
在整个视图上绘制一个透明的黑色矩形。因此,前几帧的斑点似乎会随着时间淡出。由于 "older" 个点被透明矩形覆盖了 may 次,它们变成了深灰色。 "Younger" 斑点只是覆盖了几次,呈浅灰色。由于填充颜色为白色(fill(255);
)
如果您增加混合矩形的 alpha 值,那么斑点会 "disappear" 更快并且 "tail" 会更短。
例如
fill(0, 50);