Move a rectangle across a window by incrementing a variable. Start the shape at x coordinate 0 and use an if statement to have it stop at coordinate 100. Rewrite the sketch to use constrain( ) instead of the if statement. Fill in the missing code.

// Rectangle starts at location x
float x = 0;

void setup() {
  size(200,200);
}

void draw() {
  background(255);
  // Display object
  fill(0);
  rect(x,100,20,20);
  // Increment x
  x = x + 1;
  ______________________________________________
  ______________________________________________
  ______________________________________________
}
Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Exercise 5-3: Move a rectangle across a window by incrementing a variable. Start the shape at x coordinate 0 and use an if statement to have it stop at coordinate 100. Rewrite the sketch to use constrain( ) instead of the if statement. Fill in the missing code.

// Rectangle starts at location x
float x = 0;

void setup() {
  size(200,200);
}

void draw() {
  background(255);
  
  // Display object
  fill(0);
  rect(x,100,20,20);
  
  // Increment x
  x = x + 1;
  
  // If x is greater than 100
  if (x > 100) {
    // Set it back to 100
    x = 100;
  }
  
  // Using constrain instead
  // x = constrain(x,0,100);
  
}
  • http://twitter.com/stani101 stani

    float x=0;

    void setup(){
     size(250,250);
     smooth();
     //r=random(150);
     //g=random(250);
     //b=random(200);
    }
     
    void draw() {
      background(255);
    stroke(255);
    fill(0);
    rect(x,100,30,30);

    x = x+1;

    if(x<100) {
      x=x+1;
    } else{
      x=0;
    }

    //x = constrain(x,0,150);
    }

  • gingoball

    void setup()
    {size(600,600);background(0);}

    int t=600, i=200,x=t,y=i,w=50,h=375, m=0;
    int speed=100,v=100;//you can change the speed here to get different effects :)
    float r,g,b;

    void draw()
    { noStroke();smooth();fill(r=random(255),g=random(255),b=random(255));rect(m,y,w,h);
      x=x+speed;
      fill(r=random(255),g=random(255),b=random(255));stroke(0);strokeWeight(5);smooth();ellipse(m+23,v,w,50);
      m=m+speed;
    if( m > 550){ m=550;}

    if( x > 550){ x=550;}
    else if(x==550){speed= speed*-1;}

    if( m > 550){ m=550; }
    else if(m==550)
    {speed= speed*-1;}
    if( x < 0){ x=0; }
    else if(x==0){speed= speed*-1;}
    if( m < 0){ m=0; }
    if( m < 0){ m=0; }
    else if(m==0){speed= speed*-1;}

    if(mousePressed && mouseButton==LEFT){stroke(255,0,0,95);strokeWeight(6);line(mouseX,mouseY,pmouseX,pmouseY);}

    if(mousePressed && mouseButton==RIGHT){noStroke();fill(255,15);ellipse(mouseX,mouseY,100,100);}
     }
     

  • Alice Schoessow

    Hi Charlie, here is my lab 4

    // Rectangle starts at location x
    float x = 0;
    From Alice Schoessow
    void setup() {
    size(200,200);
    }

    void draw() {
    background(255);
    // Display object
    fill(0);
    rect(x,100,20,20);
    // Increment x
    x =x + 1;
    if (x >= width/2) {
    x = x – 1;
    }

    }

  • smithroat

    //rectangle starts at location x

    float x = 0;

    void setup(){

    size(200,200);

    smooth();

    background(255);

    frameRate(60);

    }

    void draw(){

    background(255);

    //display the object

    fill(0);

    rect(x,height/2,20,20);

    //increment x

    x++;

    x = constrain(x,0,100);

    }

  • Andrew

    float x = 0;
    float rectH = 20;
    float rectW = 20;

    void setup(){

    size(600, 600);

    }

    void draw(){

    background(255);

    fill(0);

    rect(x, height/2, rectW, rectH);

    x = x + 1;

    x = constrain(x,0, width-rectW);

    }