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の影響を受けます。処理中、楕円はアルファ値に従っていませんか?
変数「a」またはアルファは、距離である「d」から外挿されます。したがって、オブジェクトがさらに遠い場合、オブジェクトのアルファ値が下がります。
このシナリオでは、線のアルファ値は時間とともに変化しません。遠く離れて消える。しかし、非常に似たコードを持っているにもかかわらず、楕円はアルファ '255'に固執しているようです。
'a'の値がハードコードされている場合、
楕円の色は、予想通りに灰色の色合いに変わります。
編集:私は問題の根本を見つけたと信じています。変数 'a'は反復されているパーティクルを区別しません。このように、アルファはスタックされているかもしれません/ 255まで加算されます。
あなたが期待していないところでアルファを上手くくすぐるようなものがある場合に備えて、プッシュスタイル()/ popStyle()を使って描画スタイルを切り離したい場合があります。 [この回答を確認する](http://stackoverflow.com/questions/18904080/is-there-a-way-to-change-the-color-of-pshape-without-entering-begin-end/18905363#18905363 )詳細 –
@George Profenzaフィードバックをありがとう、私はプッシュ/ポップのメソッドをテストしたが、結果には影響しません。 – user4985
クロスポストをリンクしてください:https://forum.processing.org/two/discussion/18015/processing-ellipse-not-following-alpha-values#latest –