How to put an object randomly on a specific place

edited October 2014 in Questions about Code

So far, I have programmed for fun for a little project of mine, but now I´m stuck. I hope you can help me :) Here´s a little excerp:

int Width = 500;
int Height = 700;

int Width1 = 50;
int Height1 = 30;
int X = 225;

// Y-Coordinates the rectangle can have
int YPosi1 = 150;
int YPosi2 = 200;
int YPosi3 = 250;
int YPosi4 = 300;

void setup () {
  size (Width,Height);
  background (229,255,255);
}

void draw () {
  rectangle();
}

void rectangle (){
  stroke (0); 
  strokeWeight (1);

  rect (X, YPosi1, Width1, Height1); // the Vertical Position of the rectangle should change randomly 
}

What I want to do, is to randomise the y-coordinate of my rectangle. However this y-coordinate has to be YPosi1, YPosi2, YPosi3 or YPosi4. So with every new start of the program the rectangle position will change. I´m sure it can be easily done, but I am fairly new to all of this, so a little help would be appreciated :)

Tagged:

Answers

  • edited October 2014 Answer ✓
    // forum.processing.org/two/discussion/7455/
    // how-to-put-an-object-randomly-on-a-specific-place
    
    static final short[] Y_COORDS = {
      150, 200, 250, 300
    };
    
    static final int W = 50, H = 30;
    int x;
    
    void setup() {
      size(200, 400, JAVA2D);
      noLoop();
      frameRate(10);
      smooth(4);
    
      rectMode(CORNER);
      strokeWeight(2);
      stroke(0);
      fill(0350);
    
      x = width-W >> 1;
    }
    
    void draw() {
      background(#E0FFFF);
      rect(x, Y_COORDS[(int) random(Y_COORDS.length)], W, H);
    }
    
    void keyPressed() {
      redraw();
    }
    
    void mousePressed() {
      redraw();
    }
    
  • Since your Y positions make up a pattern (increment by 50), in this case, we could use the formula: int(random(4)) * 50 + 150

    So what you'd end up doing, if you were to use that formula, is replace line 27 with:

    rect (X, int(random(4)) * 50 + 150, Width1, Height1);
    

    However, should you change those coordinate to something that has no pattern, then, if you haven't already, you will need to be introduced to a thing called an array.

    And @GoToLoop, while didn't make the effort to provide you with an introduction to what are called "arrays", did provide you with the code you will likely use in the end.

    As for a brief introduction, you can read up on that here, at the "Description" tag: processing.org/reference/Array.html

  • Thank you both for the answers :) However @MenteCode, what you told me to do, isn´t what I wanted to do, because the rectangle shows up on all mentioned positions... But I will look up Arrays :)

  • edited October 2014

    Array is essential programming subject. You've gotta learn that too! (*)
    BtW, @MenteCode's solution is valid as well. Rather than relying on an array of y coordinate values,
    he's simply found out a math pattern for them: =:)

    void draw() {
      background(#E0FFFF);
      //rect(x, Y_COORDS[(int) random(Y_COORDS.length)], W, H);
      rect(x, (int) random(4)*50 + 150, W, H);
    }
    

    That is, 50 is the height gap. While 150 is the initial offset. And 4 the # of sequence values: :-B
    150, 200, 250, 300

Sign In or Register to comment.