2016-08-31 63 views
1

這是此question的後續操作。正在處理,無法爲ArrayList中的對象設置Alpha值

我正在尋找一堆粒子物體以保持某個'alpha'值。此Alpha值將根據其與相鄰粒子的鄰近性而增加/減少。

目前我的代碼導致所有粒子停留在max 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. 
} 
} 
+0

請多個列表之間的鏈接:https://forum.processing.org/two/discussion/18015/processing-ellipse-not-following-alpha-values#latest –

回答

1

你必須從your previous question同樣的問題:每個Particle,你通過其他Particle循環,所以你最終繪製橢圓40爲每一個Particle。相反,您需要循環訪問Particle並找到最近的鄰居,然後將您的阿爾法計算從該一個Particle

換句話說,您的圖紙代碼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. 
}