Getting 2D Array of Objects to Randomly Scale

Hello! I’m trying to make my background of stars randomly scale. I used a 2D array of class Star to display what I have right now. I’m very new to processing so explaining this to me in basic terms would be very much appreciated! Here is my code:

Star st;
Star [][] starArray;

void setup() {
  size(800, 600);
  st = new Star();
  starArray = new Star[10][10];
  for (int i=0; i<10; i++) {
    for (int j=0; j<10; j++) {
      //println("For");
      starArray[i][j]= new Star(i*10, j*10);
    }
  }
}

void draw () {
  background(30, 85, 147);
  for (int i=0; i<10; i=i+1) {
    for (int j=0; j<10; j=j+1) {
      starArray[i][j].display();
    }
  }
}

class Star {
  color starColor;
  float x;
  float y;

    Star(float i, float j) {
    this.starColor= color(247,236,96);
    this.x=j*10;
    this.y=i*10;
    }
      
  void display() {
    stroke(starColor);
    line(x,y, x,y+20);
    line(x,y, x,y-20);
    line(x,y,x-20,y);
    line(x,y,x+20,y);
    line(x-10,y+10,x+10,y-10);
    line(x-10,y-10,x+10,y+10);
  }
}
1 Like
starArray[i][j]= new Star(i *10, j* 10);

you want random try random
https://processing.org/reference/random_.html

starArray[i][j]= new Star( random(0,width), random(0,height) );

or did you mean the array size ( number of stars ) need to be random?

1 Like

The number of stars don’t need to be random. I just want each star’s size to be random. When I tried starArray[i][j]= new Star( random(0,width), random(0,height) ); only one star displayed

sorry, i was thinking you talk about position?

The size of each star needs to be random. Not the stars’ positions

can have both:

Star [][] starArray= new Star[10][10];

void setup() {
  size(800, 600);
  for (int i=0; i<10; i++)
    for (int j=0; j<10; j++)
      starArray[i][j]= new Star( random(0, width), random(0, height), random(5,20) ); //__ random position
}

void draw () {
  background(30, 85, 147);
  for (int i=0; i<10; i=i+1) 
    for (int j=0; j<10; j=j+1) 
      starArray[i][j].display();
}

class Star {
  color starColor;
  float x,y,w;

  Star(float _x, float _y, float _w) {
    starColor= color(247, 236, 96);
    x=_x;
    y=_y;
    w=_w;
  }

  void display() {
    stroke(starColor);
    line(x, y, x, y+w);
    line(x, y, x, y-w);
    line(x, y, x-w, y);
    line(x, y, x+w, y);
    line(x-w/2, y+w/2, x+w/2, y-w/2);
    line(x-w/2, y-w/2, x+w/2, y+w/2);
  }
}


1 Like

That’s awesome! Thank you! Could this also work using scale? I’m trying to learn how to make things different sizes using scale

yes, try it
https://processing.org/reference/scale_.html
but there might need a different concept, like

push
tanslate x,y
scale w
line 0,0,+10,-10
pop

please repair in your first post
your code:

using the

</> button from edit menu 
2 Likes

If you don’t need to access your stars like a checkerboard (by square IDs), then you don’t need to store them in a 2d array just because they are arranged like a checkerboard.

Star[] starArray = new Star[100];
// create
for (int i=0; i<10; i++) {
  for (int j=0; j<10; j++) {
    starArray[i*10 + j]= new Star(i*10, j*10);
  }
}
// display
for (int i=0; i<starArray.length; i++) {
  starArray[i].display();
}

Also, if you need three floats – x, y, w – then one option is to extend PVector, using “z” for scale instead of “w”.

class Star extends PVector {
  color starColor;
  
  Star(float _x, float _y, float _z) {
    starColor= color(247, 236, 96);
    x=_x;
    y=_y;
    z=_z;
  }

  void display() {
    stroke(starColor);
    line(x, y, x, y+z);
    line(x, y, x, y-z);
    line(x, y, x-z, y);
    line(x, y, x+z, y);
    line(x-z/2, y+z/2, x+z/2, y-z/2);
    line(x-z/2, y-z/2, x+z/2, y+z/2);
  }
}

With this change, your Star now also supports all the PVector methods in case you need them – like sub, mult, heading, rotate, et cetera.

https://processing.org/reference/PVector.html

Here it is all together:

Star [] starArray= new Star[100];

void setup() {
  size(800, 600);
  for (int i=0; i<10; i++) {
    for (int j=0; j<10; j++) {
      starArray[i*10 + j]= new Star( random(0, width), random(0, height), random(5, 20) ); //__ random position
    }
  }
}

void draw () {
  background(30, 85, 147);
  for (int i=0; i<starArray.length; i++) {
    starArray[i].display();
  }
}

class Star extends PVector {
  color starColor;
  Star(float _x, float _y, float _z) {
    starColor= color(247, 236, 96);
    x=_x;
    y=_y;
    z=_z;
  }
  void display() {
    stroke(starColor);
    line(x, y, x, y+z);
    line(x, y, x, y-z);
    line(x, y, x-z, y);
    line(x, y, x+z, y);
    line(x-z/2, y+z/2, x+z/2, y-z/2);
    line(x-z/2, y-z/2, x+z/2, y+z/2);
  }
}
1 Like