【问题标题】:Processing, unable to set alpha value for objects in ArrayList处理中,无法为 ArrayList 中的对象设置 alpha 值
【发布时间】:2016-08-31 16:07:30
【问题描述】:

这是question的后续行动。

我希望让一堆粒子对象保持在某个“alpha”值。该 alpha 值将根据其与相邻粒子的接近程度而增加/减少。

目前我的代码导致所有粒子都保持在最大 alpha。我相信这是由于遍历 ArrayList 导致 alpha 被多次重绘。程序也因此运行缓慢。

  class Particle{

  PVector velocity, location; //PVector variables for each particle.

  Particle(){ //Constructor - random location and speed for each particle.
    velocity = new PVector(random(-0.5,0.5), random(-0.5,0.5));
    location = new PVector(random(0,width),random(0,width));
  }

  void update() { location.add(velocity); } //Motion method.

  void edge() {  //Wraparound case for particles.
    if (location.x > width) {location.x = 0;} 
    else if (location.x < 0) {location.x = width;}

    if (location.y > height) {location.y = 0;}
    else if (location.y < 0) {location.y = height;}
  }

  void display(ArrayList<Particle> p){ //Display method to show lines and ellipses between particles.

    for(Particle other: p){ //For every particle in the ArrayList.
     float d = PVector.dist(location,other.location); //Get distance between any two particle.
     float a = 255 - map(d,0,112,0,255); //Map variable 'a' as alpha based on distance. E.g. if distance is high, d = 100, alpha is low, a = 255 - 225 = 30.

if(other==this){continue;}

     println("Lowest distance of any two particle =" + d); //Debug output.

     if(d<112){ //If the distance of any two particle falls bellow 112.
      noStroke(); //No outline.
      fill(0,a); //Particle are coloured black, 'a' to vary alpha.
      ellipse(location.x, location.y, 8, 8); //Draw ellipse based on location of particle.

    }else{
      noStroke(); //No outline.
      fill(0,30); //For particles far away, set them to a fix alpha of '30'
      ellipse(location.x, location.y, 8, 8); //Draw ellipse based on location of particle.
    }
  }
}
}

ArrayList<Particle> particles = new ArrayList<Particle>(); //Create a new arraylist of type Particle.

void setup(){
  size(640,640,P2D); //Setup frame of sketch.

    for (int i=0; i<40; i++) { 
  particles.add(new Particle()); //Add five Particle elements into arraylist.
    }
}

void draw(){
 background(255); //Set white background.
 for(Particle p: particles){ //For every 'p' of type Particle in arraylist particles.
   p.update(); //Update location based on velocity.
   p.display(particles); //Display each particle in relation to other particles.
   p.edge(); //Wraparound if particle reaches edge of screen.
 }
}

【问题讨论】:

标签: java arraylist processing alpha


【解决方案1】:

your previous question 遇到了同样的问题:对于每个 Particle,您都在循环遍历 每个 其他 Particle,因此您最终会为每个 Particle 绘制 40 个椭圆.相反,您需要遍历 Particles 并找到最近的邻居,然后根据 one Particle 进行 alpha 计算。

换句话说,您的绘图代码应该发生在for 循环之后。

定位最近的邻居如下所示:

Particle closestNeighbor = null;
float closestDistance = 100000;

for (Particle other : p) { //For every particle in the ArrayList.

  if (other == this) {
    continue;
  }


  float d = PVector.dist(location, other.location);
  if (d < closestDistance) {
    closestDistance = d;
    closestNeighbor = other;
  }
}

此循环完成后,closestNeighbor 将指向最近的邻居,closestDistance 将是最近的距离。您可以在计算 alpha 时使用它:

float a = 255 - map(closestDistance, 0, 112, 0, 255); //Map variable 'a' as alpha based on distance. E.g. if distance is high, d = 100, alpha is low, a = 255 - 225 = 30.

if (closestDistance<112) {
  noStroke(); //No outline.
  fill(0, a); //Particle are coloured black, 'a' to vary alpha.
  ellipse(location.x, location.y, 8, 8); //Draw ellipse based on location of particle.
} else {
  noStroke(); //No outline.
  fill(0, 30); //For particles far away, set them to a fix alpha of '30'
  ellipse(location.x, location.y, 8, 8); //Draw ellipse based on location of particle.
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    • 2016-09-14
    • 2019-07-15
    • 2021-02-06
    • 2013-07-10
    相关资源
    最近更新 更多