We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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 :)
Answers
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:
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 :)
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: =:)
That is,
50
is the height gap. While150
is the initial offset. And4
the # of sequence values: :-B150, 200, 250, 300