Design Programming DECO2011 Rob Saunders web: http://www.arch.usyd.edu.au/~rob e-mail: rob@arch.usyd.edu.au office: Room 274, Wilkinson Building Functions What is a Function? Functions are named blocks of code Functions allow complex programs to be broken down into smaller, simpler tasks Functions allow commonly required code to be defined once and called again and again Calling Functions A function is called using its name followed by a list of parameters We’ve already been using the functions that are included in Processing line(0, 10, 10, 20) Function Parameters Function Definitions A function is defined using its name, a list of named parameters and its return type The definition of the line function included in Processing looks something like this: Function Parameters Return Type void line(float x1, float y1, float x2, float y2) { // line drawing code goes here... } A Very Simple Function This function takes no parameters and returns no value void printHello() { println(“Hello”); } Void When a function is defined with a return type of void it means that the function doesn’t return anything. A Simple Math Function We can define a new math function that returns the average of three numbers: float average(float a, float b, float c) { return (a + b + c) / 3.0; } Return A function that returns a value must contain at least one return statement. The type of the value following return must match the data type before the name of the function. float average(float a, float b, float c) { return (a + b + c) / 3.0; } The return statement evaluates to a float A Simple Drawing Function We can define a new drawing function to draw squares: void square(float x, float y, float side) { rectMode(CENTER); rect(x, y, side, side); } Local Variables Functions can use local variables, for example we can use a local variable in the average function defined previously: float average(float a, float b, float c) { float sum = a + b + c; return sum / 3.0; } Array Parameters Functions can take arrays as parameters float average(float[] values) { float sum = 0; for (int i = 0; i < values.length; i++) { sum += values[i]; } return sum / values.length; } Pass by Value Pass by Reference Java passes parameters in two different ways depending on the type of parameter: Pass by value The values of parameters that are primitive data type are copied Pass by reference Parameters that are arrays or objects are shared between caller and callee Pass by Value The value of a variable used as a parameter passed by value does not change void triple(int a) { a *= 3; } int x = 3; triple(x); println(x); // prints “3” Pass by Reference A variable that is passed as a parameter by reference is shared between caller and callee void triple(int[] values) { for (int i = 0; i < values.length; i++) { values[i] *= 3; } } int[] x = {2, 3, 4}; triple(x); println(x[0] + “ ” + x[1] + “ ” + x[2]); setup() and draw() Processing programs have two special functions setup() and draw() Code in setup() is run once at the beginning of a program’s execution Code in draw() is run many times per second to update the display A Simple Animation void setup() { size(400, 400); framerate(5); } void draw() { background(0); for (int y = 20; y < height; y += 20) { for (int x = 20; x < width; x += 20) { fill(random(255)); rect(x - 5, y - 5, 10, 10); } } } Pixels & Images Loading Images Processing makes it easy to load images using the loadImage() function: size(615, 400); PImage img = loadImage("opera_house.jpg"); image(img, 0, 0); Displaying Images The function image() draws an image at a given location at a given size image(img, x, y) image(img, x, y, width, height) Calls to the function imageMode() control how images are drawn, it supports CORNER and CORNERS (but not CENTER) Animating an Image We can use the functions setup() and draw() to create animations using images. PImage img; int x, dx, xmin; void setup() { size(400, 400); img = loadImage("sydney_skyline.jpg"); x = 0; dx = -1; xmin = width - img.width; } void draw() { image(img, x, 0); x += dx; if ((x <= xmin) || (x >= 0)) dx = -dx; } Animating an Image Transforming an Image Images are drawn according to the current transformation, i.e. the current translation, rotation and scale PImage img; float angle; void setup() { size(615, 400); img = loadImage("opera_house.jpg"); angle = 0; } void draw() { translate(width/2, height/2); rotate(radians(angle)); scale(0.5 + 0.5*cos(radians(angle))); image(img, -img.width/2, -img.height/2); angle = (angle + 1) % 360; } Transforming an Image Pixels Images are made up of many pixels. The values of individual pixels in an image can be accessed using get() and set(). get(x, y) returns the pixel colour at (x, y) set(x, y, c) sets the pixel colour at (x, y) PImage img; size(615, 400 + 133); img = loadImage("opera_house.jpg"); PImage red_img = new PImage(615, 400); PImage green_img = new PImage(615, 400); PImage blue_img = new PImage(615, 400); for (int y = 0; y < img.height; y++) { for (int x = 0; x < img.width; x++) { color c = img.get(x, y); red_img.set(x, y, color(red(c), 0, 0)); green_img.set(x, y, color(0, green(c), 0)); blue_img.set(x, y, color(0, 0, blue(c))); } } image(img, 0, 0); image(red_img, 0, 400, 205, 133); image(green_img, 205, 400, 205, 133); image(blue_img, 410, 400, 205, 133); Extracting Colour Values from Pixels Arrays of Pixels The pixels of an image are held in an array called pixels that is width*height in size. To access pixel values directly from the pixel array it is necessary to calculate the index of a pixel from its x and y position. color c = img.pixels[y*width+x]; img.pixels[y*width+x] = color(255); The Display as an Image The display can be treated as an image. get() gets the colour of a pixel in the display. set() sets the colour of a pixel in the display. pixels[] contains the pixels of the display. To access the pixels[] array, loadPixels() must be called at the start of draw() and updatePixels() must be called at the end. Lab Assignment Develop an Animated Sketch Start development of an animated sketch by loading and displaying an image. To import an image into your sketch use the menu “Sketch > Add File...” Try experimenting with the functions copy(), blend(), and filter() to change the image with each call to the draw() function.