0

我正在尝试创建一个可视化,其中我的网络节点随着可视化的进行而循环改变大小(为了简单起见,我已经剥离了节点之间的交互)。我有在index 函数sizes中循环的数组。我不确定为什么节点没有改变大小。对此问题的任何见解将不胜感激。drawj

int numBalls = 5;

Ball[] balls = new Ball[numBalls];

float[] sizes = {15,25,35,45,55,65};

void setup() {
  size(800, 400);
  int l = 0 ;
  for (int i = 0; i < numBalls; i++) {
    balls[i] = new Ball(random(width),random(height), random(30, 50), i, balls);
  }
  noStroke();
  fill(255, 204);
}

void draw() {
  background(0);
  for (int j = 0; j < 6; j++){
    for (int i = 0; i < numBalls; i++) {
    print("\nNEW ID\n");
    print(i);
    print("\n");
    print("Diameter in balls\n");
    print(balls[i].diameter);
    print("\n");
    balls[i].diameter = sizes[j];
    print("Diameter in balls after fix\n");
    print(balls[i].diameter);
    balls[i].display();  
    }
  }
}

class Ball {
  float x, y;
  float diameter;
  float mass; 
  float vx = 0;
  float vy = 0;
  int id;
  Ball[] others;

  Ball(float xin, float yin, float din, int idin, Ball[] oin) {
    x = xin;
    y = yin;
    diameter = din;
    mass = 50;
    id = idin;
    others = oin;
  } 

  void display() {
    textSize(32);
    fill(0,255,0,255);
    print("\nDiameter in display\n");
    print(diameter);
    print("\n");
    ellipse(x, y, diameter, diameter);
    print("\nDiameter in display\n");
    print(diameter);
    print("\n");
    fill(255, 0, 0, 255);
    text(id,x,y);
  }
}
4

1 回答 1

1

问题是,在您的 draw() 函数中,您正在使用第一个 for 循环遍历大小数组并将该大小的值分配给球。这样,在每个 draw() 中,您随后将每个尺寸附加到每个球上,并且每次附加的尺寸都会覆盖前一个尺寸...请记住,处理窗口仅在 draw() 完成后刷新!而不是循环遍历每个 draw() 中的所有大小,您可能希望每个 draw() 中的大小不同。因此,一种方法是:

int numBalls = 5;
int sizeCounter = 0;
int everySoManyFramesChange = 3;

Ball[] balls = new Ball[numBalls];
float[] sizes = {
  15, 25, 35, 45, 55, 65
};

void setup() {
  size(800, 400);
  int l = 0 ;
  for (int i = 0; i < numBalls; i++) {
    balls[i] = new Ball(random(width), random(height), random(30, 50), i, balls);
  }
  noStroke();
  fill(255, 204);
}

void draw() {
  background(0);
  for (int i = 0; i < numBalls; i++) {
    balls[i].diameter = sizes[sizeCounter];
    balls[i].display();
  }
  if (frameCount%everySoManyFramesChange == 0) sizeCounter=(sizeCounter+1)%sizes.length;
}
class Ball {
  float x, y;
  float diameter;
  float mass; 
  float vx = 0;
  float vy = 0;
  int id;
  Ball[] others;

  Ball(float xin, float yin, float din, int idin, Ball[] oin) {
    x = xin;
    y = yin;
    diameter = din;
    mass = 50;
    id = idin;
    others = oin;
  } 

  void display() {
    textSize(32);
    fill(0, 255, 0, 255);
    ellipse(x, y, diameter, diameter);
    fill(255, 0, 0, 255);
    text(id, x, y);
  }
}

顺便说一句,我删除了所有这些打印语句,因为它们使草图变得非常缓慢,但请成为我的客人并重新介绍它们!

于 2013-11-13T22:33:49.457 回答