What's wrong with this code?

edited November 2013 in Questions about Code

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

  • Answer ✓
    void setup() {
      size(500, 500);
      noStroke();
      noLoop();
    }
    
    void lines(int n) {
      rect(n, 0, 10, height);
    }
    
    void draw() { 
      int lines=0;
      color c = color(255); 
      while (lines<width/10) {
        if (c==color(0)) {
          c=color(255);
        }
        else {
          c=color(0);
        }
        fill(c);
        lines(lines*10);
        lines++;
      }
    }
    
  • 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!

  • edited November 2013

    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.

  • edited November 2013

    Use the code I have given or just change your line 26 to this if (linesDrawn%2 != 0)

              int numberofbox = 10;
              float boxsize;
              void setup()
              {
                size(500, 500);
                stroke(0,255,0);
                boxsize = width/numberofbox;
              }
    
    
              void draw()
              {
                for (int x=0;x<numberofbox;x++) {
                  for (int y=0;y<numberofbox;y++) {
                    int loc = x+y;
                    if(loc%2!=0)fill(-1);
                    else fill(0);
                    rect(x*boxsize, y*boxsize, boxsize, boxsize);
                  }
                }
              }
    
  • TfGuy44 how to delete post ?

Sign In or Register to comment.