Need help with arrays

Hi :slight_smile:

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! :slight_smile:

1 Like

you can use random position?
https://processing.org/reference/random_.html

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

or a better STAR and random size

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

4 Likes

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

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

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:

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:

float xStar;
float yStar;
2 Likes

Thanks! I fixed the stars being declared outside the class :slight_smile:

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!