2016-08-30 5 views
1
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まで加算されます。

+0

あなたが期待していないところでアルファを上手くくすぐるようなものがある場合に備えて、プッシュスタイル()/ popStyle()を使って描画スタイルを切り離したい場合があります。 [この回答を確認する](http://stackoverflow.com/questions/18904080/is-there-a-way-to-change-the-color-of-pshape-without-entering-begin-end/18905363#18905363 )詳細 –

+0

@George Profenzaフィードバックをありがとう、私はプッシュ/ポップのメソッドをテストしたが、結果には影響しません。 – user4985

+0

クロスポストをリンクしてください:https://forum.processing.org/two/discussion/18015/processing-ellipse-not-following-alpha-values#latest –

答えて

1

MCVEを投稿する必要があります。これはスケッチ全体ではなく、ハードコーディングされた数行だけであって、同じコードから作業していることに注意してください。私たちは、あなた自身のマシンにあなたのコードをコピーして貼り付けることができなければなりません。また、コードを適切にフォーマットしてください。あなたのインデントの欠如はあなたのコードを読みにくくします。

私は一般的な意味で助けようとすることができます。まず、aの値を印刷していますが、その値が何であるかは教えていません。その価値はあなたの期待ですか?もしそうなら、省略記号を描く前に前のフレームを消去していますか?それとも前に描いた楕円の上に描いていますか?コードのどこかに省略記号を描いていますか?

最初から空白のスケッチを作成し、問題を示すのに十分な行を追加します。ここから働くことができる例MCVEです:

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の値をハードコードしてください。あるいは、何が起こっているかを正確に見るために十分なコードを追加してください。

transparency

編集:MCVEため感謝。更新されたコードにはまだ問題があります。 、あなたループすべての粒子を通じて

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の場所に楕円を描く:私はこのループを理解していませんか?それは意味をなさない。あなたは100 Particlesを持っている場合、それはそれぞれParticleが100回描かれることを意味します!

Particleの距離に基づいて各色を設定したい場合は、このループを修正して、最も近いParticleを見つけて計算をオフにする必要があります。

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をそれぞれそれと比較し、距離はゼロになるからです!

closestNeighborclosestDistanceを取得したら、計算できます。

隣接ピクセルが112ピクセルよりも近い場合にのみパーティクルを描画することに注意してください。それはあなたがしたいことですか?

フォローアップの質問がある場合は、新しい質問に更新されたMCVEを投稿してください。常に質問と回答を編集するのは紛らわしいので、もう一度立ち寄れば新しい質問をしてください。

+0

返信いただきありがとうございます。私は問題をよりよく強調すべき簡単なMCVEのスケッチを作成しました。 – user4985

+0

@ user4985編集した回答を確認してください。 –

+0

"if(other == this)"が私の問題を解決しました!ありがとう。隣のパーティクルに閉じ込められて不透明になる前に、パーティクルを固定アルファ、たとえば30にしておきたい場合はどうすればいいですか? – user4985

関連する問題