// CMSC110 Fall 2019 - Assignment#2 // Created by: Deepak Kumar // Date: September 2019 // Purpose: // Exercise to learn how to write functions with parameters. // Also, to learn how to use functions to draw an object with // varying width and height at any given x, y location on the // canvas. // This is an exemplar of what Assignment#2 was asking for
void setup() { // set up camvas size(500, 500); background(200); } // setup()
void draw() { // nothing to do here, as trucks are drawn // inresponse to mouse clicks } // draw()
void mousePressed() { // Generate truck color fill(random(50,200), random(50,200), random(50,200)); // Get the body size of truck float truckBody = random(50, 150); draw truck at mouse click truck(mouseX, mouseY, truckBody, truckBody*0.7); } // mousePressed
void keyPressed() { // clear screen background(200); } // keyPressed()
void truck(float x, float y, float w, float h) { // Draw a truck at (x, y) of width w, height h float body = 5 * w / 8; float hoodW = 3 * w / 8; float hoodH = h / 2; float wheelS = hoodW/2;
// Draw a truck pushMatrix(); translate(x, y);
// Draw body stroke(0); rect(0, -body, body, body);
// Draw hood rect(body, - hoodH, hoodW, hoodH); fill(0); rect(body, -hoodH, hoodW, random(hoodH*0.5)); // /headlamp fill(0); //arc(body+hoodW, -hoodH*0.9, hoodH*0.2, hoodH*0.2, PI/2, -PI/2); //arc(body+hoodW, -hoodH*0.8, hoodH*0.4, hoodH*0.4, -PI/2, PI/2); rect(body+hoodW, -hoodH, hoodH*0.1, hoodH*0.1);
// Draw window stroke(0); line(body, - body, + body + hoodW/2, -hoodH); line(body, -body, body, 0); line(0, 0, w, 0);
// Draw wheels fill(0); ellipse(body/2, 0, wheelS, wheelS); ellipse(w-hoodW/2, 0, wheelS, wheelS); // Draw Number fill(0); String number = str(int(random(10, 100))); text(number, body*0.2, -body*0.5); popMatrix(); } // truck()