Reputation: 1
How do I make a line show up multiple times without writing its code multiple times? Here is my code:
int n = 15;
float[] pointX = new float[n];
void setup(){
size(1400, 900);
background(#333333);
stroke(100, 50);
for(int i = 0; i < n; i ++){
pointX[i] = random(0, n);
line(pointX[i], 0, pointX[i], 900);
}
}
I am trying to make the line at pointX appear 15 times at even increments across the screen. Does anyone know how to do this?
Upvotes: 0
Views: 57
Reputation: 42174
Right now your code is looping 15 times, and drawing a line at a random position between 0 and 1, then between 0 and 2, then between 0 and 3... all the way up to between 0 and 15.
This is going to only show lines in the first 15 pixels, and it's going to show some lines on top of each other.
If you want them to show up evenly, then you don't want it to be random. And if you want them to spread across the entire width of the screen, you're going to have to use the width
variable.
When you're stuck on stuff like this, the best thing you can do is get out a piece of paper and a pencil and draw out some examples. What coordinates do you want your lines to be drawn at? What is the distance between the lines? Draw out a few different examples until you notice the pattern.
Shameless self-promotion: here is a tutorial on using for
loops in Processing, which covers exactly what you're trying to do.
Upvotes: 1