【发布时间】:2016-08-30 03:19:09
【问题描述】:
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' 或 alpha 是从距离 'd' 外推而来的。因此,当物体更远时,物体的 alpha 值会下降。
在这种情况下,线的 alpha 值不会随时间变化,例如随着距离消退。然而,尽管代码非常相似,但省略号似乎停留在 alpha '255' 上。
如果 'a' 的值是硬编码的,例如
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.
椭圆按预期将颜色变为灰色。
编辑:我相信我找到了问题的根源。变量“a”不区分正在迭代的粒子。因此,alpha 可能会卡住/加起来最多 255。
【问题讨论】:
-
你可能想看看 pushStyle()/popStyle() 来隔离绘图样式,以防万一你有一些东西在你不期望的地方弄乱了 alpha。详情请查看this answer
-
@George Profenza 感谢您的反馈,我已经测试了 push/pop 方法,但不影响结果。
-
请在交叉帖子之间链接:forum.processing.org/two/discussion/18015/…
标签: processing ellipse extrapolation