处理:创建通过绘制循环更改大小的形状

时间:2013-11-13 21:36:31

标签: processing

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

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);
  }
}

1 个答案:

答案 0 :(得分:1)

问题是,在你的draw()函数中,你使用第一个for循环遍历大小数组,并将该大小的值分配给球。这样在每个draw()中你随后在每个球上附加每个尺寸,并且每次你附加的尺寸都覆盖前一个尺寸......记住,Processing的窗口只在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);
  }
}

顺便说一下,我删除了所有那些打印语句,因为它们使草图速度非常慢,但是作为我的客人并重新介绍它们!