# Getting 2D Array of Objects to Randomly Scale

**URL:** <https://discourse.processing.org/t/getting-2d-array-of-objects-to-randomly-scale/15390>\
**Category:** Coding Questions\
**Created:** [November 11, 2019, 2:08am UTC](https://discourse.processing.org/t/getting-2d-array-of-objects-to-randomly-scale/15390 "2019-11-11T02:08:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lane](https://avatars.discourse-cdn.com/v4/letter/l/779978/32.png) [@Lane](https://discourse.processing.org/u/Lane)\
**Post date:** [November 11, 2019, 2:08am UTC](https://discourse.processing.org/t/getting-2d-array-of-objects-to-randomly-scale/15390/1 "2019-11-11T02:08:47Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 11, 2019, 2:22am UTC](https://discourse.processing.org/t/getting-2d-array-of-objects-to-randomly-scale/15390/2 "2019-11-11T02:22:29Z")

</div>

```auto
starArray[i][j]= new Star(i *10, j* 10);

```

you want random try random  
[https://processing.org/reference/random\_.html](https://processing.org/reference/random_.html)

```auto
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?

---

<div class="post-metadata">

**Author:** ![Lane](https://avatars.discourse-cdn.com/v4/letter/l/779978/32.png) [@Lane](https://discourse.processing.org/u/Lane)\
**Post date:** [November 11, 2019, 2:37am UTC](https://discourse.processing.org/t/getting-2d-array-of-objects-to-randomly-scale/15390/3 "2019-11-11T02:37:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 11, 2019, 3:03am UTC](https://discourse.processing.org/t/getting-2d-array-of-objects-to-randomly-scale/15390/4 "2019-11-11T03:03:15Z")

</div>

> [@Lane](#):
>
> I just want each star’s size to be random

sorry, i was thinking you talk about position?

---

<div class="post-metadata">

**Author:** ![Lane](https://avatars.discourse-cdn.com/v4/letter/l/779978/32.png) [@Lane](https://discourse.processing.org/u/Lane)\
**Post date:** [November 11, 2019, 3:07am UTC](https://discourse.processing.org/t/getting-2d-array-of-objects-to-randomly-scale/15390/5 "2019-11-11T03:07:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 11, 2019, 3:16am UTC](https://discourse.processing.org/t/getting-2d-array-of-objects-to-randomly-scale/15390/6 "2019-11-11T03:16:11Z")

</div>

can have both:

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

```

---

<div class="post-metadata">

**Author:** ![Lane](https://avatars.discourse-cdn.com/v4/letter/l/779978/32.png) [@Lane](https://discourse.processing.org/u/Lane)\
**Post date:** [November 11, 2019, 3:20am UTC](https://discourse.processing.org/t/getting-2d-array-of-objects-to-randomly-scale/15390/7 "2019-11-11T03:20:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 11, 2019, 3:27am UTC](https://discourse.processing.org/t/getting-2d-array-of-objects-to-randomly-scale/15390/8 "2019-11-11T03:27:06Z")

</div>

yes, try it  
[https://processing.org/reference/scale\_.html](https://processing.org/reference/scale_.html)  
but there might need a different concept, like

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

```

* * *

please repair in your first post  
your code:

using the

```auto
</> button from edit menu 

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [November 12, 2019, 7:53pm UTC](https://discourse.processing.org/t/getting-2d-array-of-objects-to-randomly-scale/15390/9 "2019-11-12T19:53:34Z")

</div>

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.

```auto
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”.

```auto
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](https://processing.org/reference/PVector.html)

Here it is all together:

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

```
