处理,椭圆不跟随 alpha 值? [英] Processing, ellipse not following alpha values?

查看:52
本文介绍了处理,椭圆不跟随 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 - d*2.5; //Map variable 'a' as alpha based on distance. E.g. if distance is high, d = 100, alpha is low, a = 255 - 225 = 30.

     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.

      stroke(0,a); //Lines are coloured black, 'a' to vary alpha.
      strokeWeight(0.7);
      line(location.x,location.y,other.location.x,other.location.y); //Draw line between four coordinates, between two particle.
     }

    }
  }
}

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

void setup(){
  size(640,640,P2D); //Setup frame of sketch.
  particles.add(new Particle()); //Add five Particle elements into arraylist.
  particles.add(new Particle());
  particles.add(new Particle());
  particles.add(new Particle());
  particles.add(new Particle());
}

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.
 }
}

在上面的代码中,有形状对象,线条和椭圆.其透明度受变量a的影响.

In the above code, there are to shape objects, lines and ellipses. The transparency of which are affected by variable a.

变量 'a' 或 alpha 是从距离 'd' 推断出来的.因此,当物体更远时,物体的alpha值下降.

Variable 'a', or alpha, is extrapolated from 'd' which is distance. Hence, when the objects are further, the alpha value of the objects falls.

在这种情况下,线的 alpha 值不会随时间变化,例如随着距离逐渐消失.然而,尽管代码非常相似,但省略号似乎卡在了 alpha '255' 上.

In this scenario, the alpha values of the line do not change over time e.g. fade with distance. However the ellipses seem to be stuck on alpha '255' despite having very similar code.

如果 'a' 的值是硬编码的,例如

If the value of 'a' is hardcoded, e.g.

if(d<112){ //If the distance of any two particle falls bellow 112.
      noStroke(); //No outline.

      fill(0,100); //Particle are coloured black, set alpha 'a' to be 100, grey tint.

      ellipse(location.x, location.y, 8, 8); //Draw ellipse based on location of particle.

椭圆按预期将颜色更改为灰色.

the ellipses changes colour as expected to a grey tint.

我相信我已经找到了问题的根源.变量a"不区分正在迭代的粒子.因此,alpha 可能会卡住/加起来为 255.

I believe I have found the root of the issue. The variable 'a' does not discriminate between the particles that are being iterated. As such, the alpha might be stuck/adding up to 255.

推荐答案

您将不得不发布 MCVE.请注意,这不应该是您的整个草图,只是一些硬编码的行,因此我们都使用相同的代码工作.我们应该能够将您的代码复制并粘贴到我们自己的机器中以查看问题.另外,请尝试正确格式化您的代码.缺少缩进会使您的代码难以阅读.

You're going to have to post an MCVE. Note that this should not be your entire sketch, just a few hard-coded lines so we're all working from the same code. We should be able to copy and paste your code into our own machines to see the problem. Also, please try to properly format your code. Your lack of indentation makes your code hard to read.

话虽如此,我可以尝试提供一般意义上的帮助.首先,您正在打印 a 的值,但是您没有告诉我们它的值是什么.它的价值是否符合您的预期?如果是这样,您是在绘制椭圆之前清除先前的帧,还是在先前绘制的椭圆之上绘制它们?您是否在代码的其他地方绘制了椭圆?

That being said, I can try to help in a general sense. First of all, you're printing out the value of a, but you haven't told us what its value is. Is its value what you expect? If so, are you clearing out previous frames before drawing the ellipses, or are you drawing them on top of previously drawn ellipses? Are you drawing ellipses elsewhere in your code?

从一个空白草图重新开始,并添加足够的线条来显示问题.以下是您可以使用的 MCVE 示例:

Start over with a blank sketch, and add just enough lines to show the problem. Here's an example MCVE that you can work from:

stroke(0);
fill(0);
ellipse(25, 25, 25, 25);
line(0, 25, width, 25);

stroke(0, 128);
fill(0, 128);
ellipse(75, 75, 25, 25);
line(0, 75, width, 75);

这段代码先画一条黑色的线和椭圆,然后再画一条透明的线和椭圆.请从您的代码中硬编码 a 值,或添加足够的代码,以便我们可以准确地看到发生了什么.

This code draws a black line and ellipse, then draws a transparent line and ellipse. Please hardcode the a value from your code, or add just enough code so we can see exactly what's going on.

感谢 MCVE.您更新后的代码仍有问题.我不明白这个循环:

Thanks for the MCVE. Your updated code still has problems. I don't understand this loop:

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 - d*2.5; //Map variable 'a' as alpha based on distance. E.g. if distance is high, d = 100, alpha is low, a = 255 - 225 = 30.

     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.

      stroke(0,a); //Lines are coloured black, 'a' to vary alpha.
      strokeWeight(0.7);
      line(location.x,location.y,other.location.x,other.location.y); //Draw line between four coordinates, between two particle.
     }

    }
  }

您是说对于每个Particle,遍历每个Particle,然后在当前Particle 的位置绘制一个椭圆?那没有任何意义.如果你有 100 个 Particles,这意味着每个 Particle 将被绘制 100 次!

You're saying for each Particle, you loop through every Particle and then draw an ellipse at the current Particle's location? That doesn't make any sense. If you have 100 Particles, that means each Particle will be drawn 100 times!

如果您希望每个 Particle's 的颜色基于它与最近的其他 Particle 的距离,那么您需要修改此循环以简单地找到最接近的 粒子,然后以此为基础进行计算.它可能看起来像这样:

If you want each Particle's color to be based off its distance to the closest other Particle, then you need to modify this loop to simply find the closest Particle, and then base your calculations off of that. It might look something like this:

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;
  }
}

注意 if (other == this) { 部分.这很重要,否则您将每个 Particle 与其自身进行比较,距离将为零!

Notice the if (other == this) { section. This is important, because otherwise you'll be comparing each Particle to itself, and the distance will be zero!

一旦您有了 closestNeighborclosestDistance,您就可以进行计算了.

Once you have the closestNeighbor and the closestDistance, you can do your calculations.

请注意,您仅在粒子具有比 112 像素更近的邻居时才绘制粒子.这是你想做的吗?

Note that you're only drawing particles when they have a neighbor that's closer than 112 pixels away. Is that what you want to be doing?

如果您有后续问题,请在新问题中发布更新的 MCVE.不断地编辑问题和答案会让人感到困惑,所以如果再次卡住,请提出一个新问题.

If you have a follow-up question, please post an updated MCVE in a new question. Constantly editing the question and answer gets confusing, so just ask a new question if you get stuck again.

这篇关于处理,椭圆不跟随 alpha 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆