Practice Problems


Complete as many of the following problems as you can:


Practice void functions


myRectangle
rectangleDonut
borderOfCircles
coneOfCircles
regularPolygon *challenge problem! requires some trig *
trailOfCircles


1. myRectangle



void myRectangle(float x, float y, float w, float h){

//write your code here, you can not use rect()

//look up the beginShape() command in the reference

}


2. rectangleDonut


//Create a rectangular donut using 4 myRectangle commands.

//The hole of the donut shouldbe centered, and half the w wide, and

//half of the h high. ::

void rectangleDonut(float x, float y, float w, float h){

  //write your code here, you can not use beginContour()

  //instead use your myRectangle

}





3. Border of Circles…



//create a border of diameter 50 circles around the screen. They should start

//at 24,24so that the circles are tangent to the window. Assume the window size

//is a multiple of 50.

void borderOfCircles(){

//this function does not need parameters!

}

4. Cone of circles


// starting at position x,y draw the specified numberOfCircles, starting with

//diameter small, ending at diameter big. The circles are spaced apart 20 units

//in the x direction

void coneOfCircles(float x, float y, int numberOfCircles,

float small, float big){

//write your code here, anything goes!

//hint: calculate how much the diameter changes each time

//then make a variable for diameter, and use that step size to

//change it!

}



Example: coneOfCircles(50,50,3,100,140);  will draw:

3 ellipses, starting at 50,50 then 70,50 then 90,50 because they are spaced 20 apart.

The diameters start at 100 and end at 140 and increase in equal increments

That means you generate:

ellipse(50,50,100,100);//small

ellipse(70,50,120,120);//in between

ellipse(90,50,140,140);//big

5. regularPolygon (ADVANCED):


void regularPolygon(float x, float y, float radius, int numSides){

  //you can do this using trig, and generate a series of points

  //hint: use r*sin(theta) and r*cos(theta)

}


6. trailOfCircles

 - make  a new sketch, copy/paste the Ball tab into the sketch.

-make an arrayList of balls

-in the draw add new Ball objects located at the mouse to the ArrayList

-Display all of the balls in the arrayList

-Remove the oldest objects after there are 50 balls in the ArrayList

(you need to look up removing items in an arraylist)

-Try giving the balls a random x/y speed so they slowly float away from their position.



Practice return-functions:  (Car/Ball is at the end of the document in case you had some problems)


1)
boolean isStopped(){} // for the Ball class
  return true if dx and dy are both 0.0, false otherwise

2)
boolean isInside(int targetX,int targetY){} //for the Car class 
 return true if the x,y is inside your car, false otherwise

3)
  float area(){}  //for the ball class
  return the area of the circle

4)
float getSpeed(){} //for the Ball class
  calculates the magnitude of the resultant vector of dx and dy (easy if we did distance already) return that value

5)
boolean isInside(int targetX,int targetY){} //for the Ball class
 Do this again for the ball class, return true when the location is inside the ball.

6) Modify the ball display method to show the area, and the x,y position using text on top of the ball.



7) *tricky*
float radiusOfBiggestBall(ArrayList<Ball> ballList)  {}
This is a function that isn't part of a class, it returns the radius of the largest ball
You can assume the ArrayList has at least one Ball in it.

8) *tricky*
Ball closestBall(int x,int y, ArrayList<Ball> bals lList) {}
This is a function that isn't part of a class, it returns the ball that is closest to the given position
You can assume the ArrayList has at least one ball in it.






Things To Do With Bouncing Balls:


A - Requires the completion of the getSpeed() function in the ball class.

Create an alternate ball display that has overlayed text

on top of the ball, b.display(m) will display different things

when m is:

1: show (x,y) and speed.

2: show (x,y) only

3: show speed only

4: show (x,y), speed, and radius




B - (Requires the completion of the isInside(targetX,targetY) function in the ball class.)

Make it so when you click on a Ball in the ballList, the list will remove that ball.

use the isInside method to help you achieve this goal!




C - (Requires the completion of the 8th practice problem.)
Make it so when you click anywhere that is NOT on a Ball in the ballList:

 -The closest Ball is found

 -That Ball's radius increases by 1.

 -A line is drawn between the mouse and the Ball's center.






/*******************************

CAR CODE
*******************************/
class Car {
  float x, y, speed;
  color paint;
  
  Car() {
    x = random(width);
    y = random(height);
    paint = color(random(255), random(255), random(255));
    speed = random(1, 3);
  }
  
  Car(float startx, float starty) {
    x = startx;
    y = starty;
    paint = color(random(255), random(255), random(255));
    speed = random(1, 3);
  }
  void drive() {  
    x = x + speed;
  }
  void accelerate() { 
    speed = speed + 0.2;
  }
  void decelerate() {  
    speed = speed - 0.2;
  }
  void stop() {
    speed = 0;
  }
  void display() {
    fill(paint);
    rect(x, y, 40, 20);
  }
  void changeToRandomColor() {
    paint = color( random(255), random(255), random(255));
  }
}

/*******************************

BALL CODE
*******************************/

class Ball {
  float x, y, dx, dy, radius;
  float R, G, B;
  Ball() {
    x = 100.0;
    y = 200.0;
    radius = random(30, 40);
    dx = random(-5, 5);
    dy = random(-5, 5);
    R = random(255);
    G = random(255);
    B = random(255);
  }
    Ball(float startX, float startY) {
    x = startX;
    y = startY;
    radius = 2*random(30, 40);
    dx = random(-5, 5);
    dy = random(-5, 5);
    R = random(255);
    G = random(255);
    B = random(255);
  }

  void move() {
    x = x + dx; //change x's value
    y = y + dy;
  }

  void display() {
    fill(R, G, B);
    ellipse(x, y, 2*radius, 2*radius);
  }

  void bounce() {
    // if statements 
    if (x < radius || x >= width - radius) {
      dx = -dx;
    }
    if (y < radius || y >= height - radius) {
      dy = -dy;
    }
  }
}

No comments:

Post a Comment