# Need help with arrays

**URL:** https://discourse.processing.org/t/need-help-with-arrays/10749
**Category:** Coding Questions
**Created:** [April 29, 2019, 10:15am UTC](https://discourse.processing.org/t/need-help-with-arrays/10749 "2019-04-29T10:15:18Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![abigail](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@abigail](https://discourse.processing.org/u/abigail)
#### Post date: [April 29, 2019, 10:15am UTC](https://discourse.processing.org/t/need-help-with-arrays/10749/1 "2019-04-29T10:15:18Z")

</div>

Hi 🙂

```auto
Sun sun1;

Hill hill1;
Hill hill2;

Star[] stars = new Star[2];

float r = 0;
float g = 0;
float b = 200;

void setup() {
  size(600, 400);

  sun1 = new Sun();

  hill1 = new Hill(0);
  hill2 = new Hill(width);

  for (int i = 0; i < stars.length; i++) {
    stars[i] = new Star(100, 100);
  }
}

void draw() {
  background(r, g, b);
  noStroke();

  sun1.display();
  sun1.sunset();

  hill1.display();
  hill2.display();

  for (int i = 0; i < stars.length; i++) {
    stars[i].display();
  }
}

float xStar;
float yStar;

class Star {

  Star(float tempXStar, float tempYStar) {
    xStar = tempXStar;
    yStar = tempYStar;
  }

  void display() {
    fill(255);
    rect(xStar, yStar, 2, 2);
  }
}

```

So in my code I’d like to make many stars on the screen using an array. I’d like to change the x and y position of each star. So far I have two stars in the array with both the same x and y position. The co-ordinates of each star are in the arguments of the constructor. Would anyone be able to explain to me how to change the value of x and y for each star?

Thanks in advance! 🙂

---

<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: [April 29, 2019, 10:30am UTC](https://discourse.processing.org/t/need-help-with-arrays/10749/2 "2019-04-29T10:30:51Z")

</div>

you can use random position?  
[https://processing.org/reference/random\_.html](https://processing.org/reference/random_.html)

```auto
int many =100;
Star[] stars = new Star[many];
int horizon = 150;

void setup() {
  size(600, 400);
  for (int i = 0; i < stars.length; i++) stars[i] = new Star((int)random(0,width),(int)random(0,horizon));
}

void draw() {
  background(0,0,80);
  for (int i = 0; i < stars.length; i++) stars[i].show();  
}

class Star {
  int xpos,ypos;
  Star(int xpos,int ypos) {
    this.xpos = xpos;
    this.ypos= ypos;
  }
  void show() {
    fill(200,200,0);
    noStroke();
    circle(xpos,ypos,3); 
  }
}

```

![SNAG-0096](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/bc4017f7e147b4991cac0504763adb9be8b8114e.jpeg)

or a better STAR and random size

```auto
int many =100;
Star[] stars = new Star[many];
int horizon = 150;

void setup() {
  size(600, 400);
  for (int i = 0; i < stars.length; i++) stars[i] = new Star((int)random(0,width),(int)random(0,horizon),(int)random(4,10));
}

void draw() {
  background(0,0,80);
  for (int i = 0; i < stars.length; i++) stars[i].show();  
}

class Star {
  int xpos,ypos,w;
  float h;
  Star(int xpos,int ypos,int w) {
    this.xpos = xpos;
    this.ypos = ypos;
    this.w = w;
    h = sqrt(sq(w)-sq(w/2)); 
  }
  void show() {
    fill(200,200,0);
    noStroke();
    // try a |6/2| star polygon
    triangle(xpos-w/2,ypos+h/2,xpos,ypos-h/2,xpos+w/2,ypos+h/2); //circle(xpos,ypos,w);
    triangle(xpos-w/2,ypos-h/5,xpos,ypos+h-h/5,xpos+w/2,ypos-h/5);
  }
}

```

---

<div class="post-metadata">

### Author: ![abigail](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@abigail](https://discourse.processing.org/u/abigail)
#### Post date: [April 29, 2019, 6:48pm UTC](https://discourse.processing.org/t/need-help-with-arrays/10749/3 "2019-04-29T18:48:10Z")

</div>

Thanks sm for the reply! I’m gonna try implement your idea, hope it works 🙂

---

<div class="post-metadata">

### Author: ![abigail](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@abigail](https://discourse.processing.org/u/abigail)
#### Post date: [April 29, 2019, 6:52pm UTC](https://discourse.processing.org/t/need-help-with-arrays/10749/4 "2019-04-29T18:52:18Z")

</div>

Also, I didn’t know about this.xpos = xpos; etc. Makes it easier!

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [April 29, 2019, 7:20pm UTC](https://discourse.processing.org/t/need-help-with-arrays/10749/5 "2019-04-29T19:20:48Z")

</div>

Hi,

It is quite hard to answer because there is no good way to initialize the position of the stars: you do it the way you want them to be generated.

ou can do it as random as @kll said or you can use math formulas like in the following example to generate special shapes:

```auto
Star[] stars = new Star[1000];

void setup() {
  size(1000, 1000);

  float s = 200;
  for (int i = 0; i < stars.length; i++) {
    float t = 0 + i * 0.1;
    stars[i] = new Star(s*(cos(6*t)+cos(6*t)/2+sin(17*t)/3), s*(sin(6*t)+sin(6*t)/2+cos(17*t)/3));
  }
}

void draw() {
  translate(width/2, height/2);
  background(20);
  noStroke();

  for (int i = 0; i < stars.length; i++) {
    stars[i].display();
  }
}

class Star {

  float xStar;
  float yStar;

  Star(float tempXStar, float tempYStar) {
    xStar = tempXStar;
    yStar = tempYStar;
  }

  void display() {
    fill(200);
    rect(xStar, yStar, 2, 2);
  }
}

```

Also be careful, you have the 2 following stars declared outside the Star class when they should be inside:

```auto
float xStar;
float yStar;

```

---

<div class="post-metadata">

### Author: ![abigail](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@abigail](https://discourse.processing.org/u/abigail)
#### Post date: [May 7, 2019, 10:53am UTC](https://discourse.processing.org/t/need-help-with-arrays/10749/6 "2019-05-07T10:53:41Z")

</div>

Thanks! I fixed the stars being declared outside the class 🙂

I haven’t used any math formulas yet so I find that hard to understand. I used the random function and it works well.

Maybe after I get used to classes I’ll move onto that!
