Processing, ellipse not following alpha values?

edited August 2016 in Questions about Code
      for(Particle other: p){
          float d = PVector.dist(location, other.location);
          //a = 255 - map(d,0,51,0,255); map distance to alpha channel range 0-255.
           a = 255 - d*5; //without map.

          stroke(0,a); //set stroke and fill to be black, vary their alpha by 'a'
          ellipse(location.x, location.y, 6, 6);  

          stroke(0,a); //set stroke and fill to be black, vary their alpha by'a'


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

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

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



  • You are missing some code. Please format your code. Edit post, select code and hit ctrl+o.


  • Think it's a snippet with an extraneous =

    Problem might be the mapping of d. Map will map values outside of the input range so if d is above 51 you'll get unexpected values.

    First thing I would do is print d.

    Actually the first thing I would do is press ctrl-t to indent my code nicely then edit the above, paste the newly invented code, highlight it and press ctrl-o to format it for the forum...

  • Those push and Pop matrix calls are redundant BTW, you aren't changing anything that needs them.

  • Thank you for your feedback. I have formatted the display properly and edited it to show the extend of the issue.

    I have tested alpha = 255 - d*5; instead of map, since 51 is a factor of 255 and the results are the same.

    I understand the push, pop should not affect the code by I was not sure what else could have resulted in this oddity.

  • Line 13 doesn't have an alpha value. The comment, and line 8 suggests it should. The fill value for a line shouldn't matter, but it might.

    Bug might be in your other code. Are you comparing all particles with all other particles? If so you might be doing the check twice. Are you calling background at start of draw?

    Actually, line 9 gets called for every particle within a certain distance so you are drawing the current particle multiple times. The alpha will accumulate.

  • Ah, I believe you are right. I'm not sure how to address that in an efficient manner though. Is there a way to individually affect each element in an ArrayList?

    I have added a MCVE of this question on stackoverflow that might help as well.

  • As I answered on Stack Overflow (and again here), the problem is caused by you drawing inside the loop for every other Particle.

    Instead, you need to find the closest Particle and base your alpha calculations only off that one Particle. My answers on Stack Overflow contain code that does exactly that.

    In the future, please please please link between crossposts as soon as you post them. It can be frustrating when we take time to answer questions that have already been answered somewhere else.

Sign In or Register to comment.