如何在处理中创建尾随模式?

how do I create a trailing pattern in processing?

我正在尝试创建一个程序,其中椭圆围绕中心屏幕旋转并向其添加尾随模式。我在处理中使用 ArrayList 来存储旧矢量然后绘制它,但它没有渲染。还是我做错了?我写的部分在底部,谢谢。

Particle[] part = new Particle[10];
int len = 10;

void setup(){
  size(1000,1000,P2D);
  //fullScreen();
  for(int i = 0; i < len; i++){
    part[i] = new Particle();
  }
}

void draw(){
  background(255);
  for(int i = 0; i < len; i++){
    part[i].show();
    part[i].update();
    part[i].trail();
  }
}

class Particle{

  float r,angle,d;
  PVector pos = new PVector();
  ArrayList<PVector> history = new ArrayList<PVector>();

  Particle(){
    angle = random(20,360);
    r = random(20,300);
    pos.x = width/2 + cos(angle) * r;
    pos.y = height/2 + sin(angle) * r;
  }

  void show(){
    stroke(0,0,255);
    ellipse(pos.x,pos.y,5,5);
    //line(x,y,width/2,height/2);
  }

  void update(){
    pos.x = width/2 + cos(angle) * r;
    pos.y = height/2 + sin(angle) * r;
    angle += random(0.001,0.01);
  }

  void trail(){
    history.add(pos);

    if(history.size() > 50){
      history.remove(0);
    }

    for(int i = 0; i < history.size(); i++){
      PVector prev = history.get(i);
      ellipse(prev.x,prev.y,5,5);
    }
  }
}

当你这样做时

history.add(pos);

那么你不创建一个新的PVector对象。您只需将对属性 pos 的引用添加到数组列表 history。当 pos 改变时,history 的所有元素仍然引用 pos。每个 Particle 只存在一个 PVecor 对象。

在将 pos 添加到 history 时创建一个 copy,以解决问题:

history.add(pos.copy());