We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey I'm trying to write a code that will draw rectangles across the screen, alternating between black and white. I must write a function that has a parameter "n" that will draw the rectangles, while using no global variables. I thought I had the right code, but whenever I run it, the first rectangle is double the size. Any help is appreciated. Thanks.
int lines = 50;
int linesDrawn;
color myColor = color(0);
void setup()
{
size(500, 500);
noStroke();
noLoop();
}
void lines(int n)
{
rect(linesDrawn*(width/n), 0, width/n, height);
}
void draw()
{
linesDrawn = 0;
while (linesDrawn < lines)
{
fill(myColor);
lines(50);
if (linesDrawn%2 == 0)
{
println("black " + linesDrawn);
myColor = color(0);
}
else
{
println("white " + linesDrawn);
myColor = color(255);
}
linesDrawn ++;
}
}
Answers
Thank you soooo much!
Actually I just realized that your code only works if n=50, I need a more general function that will allow me to change the value of n. Thanks!
Now you're asking for something impossible. You'll either need to be able to give this function more than one variable, or you'll need a global variable. The reason for this is that the function needs to know two things - how wide the rectangle it has to draw is, and where that rectangle needs to start. If you could pass the function two variables, that'd be fine. If the function could accept one value and read the other from a global variable, that would be fine. But what you're asking for now is akin to fitting two pigeons in one hole without putting a pigeon in a hole already occupied by a pigeon.
Also, don't start a new thread about this. You already have this one. Delete your other one.
Use the code I have given or just change your line 26 to this if (linesDrawn%2 != 0)
TfGuy44 how to delete post ?