正在处理,椭圆不遵循 alpha 值?
Processing, ellipse not following alpha values?
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 可能是 stuck/adding 到 255。
您将不得不 post 一个 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
值进行硬编码,或者添加足够的代码,以便我们可以准确地看到发生了什么。
编辑: 感谢 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's
颜色基于其与最近的其他颜色 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
与自身进行比较,并且距离将为零!
获得 closestNeighbor
和 closestDistance
后,您就可以进行计算了。
请注意,只有当粒子的邻居距离小于 112
像素时,您才会绘制粒子。那是你想做的吗?
如果您有后续问题,请 post 在新问题中更新 MCVE。不断编辑问题和答案会让人感到困惑,所以如果您再次遇到问题,请提出一个新问题。
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 可能是 stuck/adding 到 255。
您将不得不 post 一个 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
值进行硬编码,或者添加足够的代码,以便我们可以准确地看到发生了什么。
编辑: 感谢 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's
颜色基于其与最近的其他颜色 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
与自身进行比较,并且距离将为零!
获得 closestNeighbor
和 closestDistance
后,您就可以进行计算了。
请注意,只有当粒子的邻居距离小于 112
像素时,您才会绘制粒子。那是你想做的吗?
如果您有后续问题,请 post 在新问题中更新 MCVE。不断编辑问题和答案会让人感到困惑,所以如果您再次遇到问题,请提出一个新问题。