Programming

Page 1

BITP2012 INTRODUCTION TO PROGRAMMING th Exam May 30 2012 IT University of Copenhagen Digital media and Design Signe Harring Hansen (sihh-­‐1318)

QUESTION 1 (IN DROPBOX “SIGNE HARRING HANSEN BITP2012”) int squareSize = 5; //This variable will set the size of the small squares -­‐ it can be changed, and it will still work :) void setup() { size(300, 200); // window size -­‐ can be changed to whatever, and it will still work } void draw() { noStroke(); // if there is a stroke, it does not look right, therefore I removed the stroke with this line for (int y = 0; y <= height; y+= squareSize) { // two for loops that draws new squares -­‐ the repetitions are multiplied. for (int x = 0; x <= width; x += squareSize) { // x and y are increased by the squareSize variable. fill(random(0, 255)); rect(x, y, squareSize, squareSize); // the size of the square depends on the squareSize variable } } }

QUESTION 2 (IN DROPBOX “SIGNE HARRING HANSEN BITP2012”) Planet sun, mercury, venus, earth, mars, jupiter, saturn, uranus, neptune; void setup() { size(800, 800); noStroke(); smooth(); colorMode(HSB, 360, 100, 100); float d = 5.5; // Earth diameter // Set planets diameter, radius, and color as close to the right numbers and colors found online :) mercury = new Planet(90, 0.382 * d, color(0, 0, 70)); venus = new Planet(110, 0.949 * d, color(40, 4, 82)); earth = new Planet(135, 1.000 * d, color(230, 75, 95)); mars = new Planet(160, 0.532 * d, color(40, 35, 70)); jupiter = new Planet(210, 11.209 * d, color(235, 15, 65)); saturn = new Planet(270, 9.449 * d, color(30, 25, 85)); uranus = new Planet(315, 4.007 * d, color(185, 15, 90)); neptune = new Planet(350, 3.883 * d, color(215, 60, 90)); } void draw() { background(0, 0, 0); translate(400, 400); // The sun fill(50, 100, 100); ellipse(0, 0, 110, 110); // Draw the planets mercury.display(); venus.display();

1


earth.display(); mars.display(); jupiter.display(); saturn.display(); uranus.display(); neptune.display(); } class Planet { float distance; float diameter; float rotation; color c; Planet(float distance, float diameter, color c) { this.distance = distance; this.diameter = diameter; this.c = c; rotation = random(0, 360); // Rotate the planet at random } void display() { pushMatrix(); //The pushMatrix() function saves the current coordinate system to the stack. rotate(rotation); //rotate planet fill(c); //set planet color ellipse(0, distance, diameter, diameter); // draw planet rotation += 1 / distance; //update rotation popMatrix(); //popMatrix() restores the prior coordinate system } }

QUESTION 3 (IN DROPBOX “SIGNE HARRING HANSEN BITP2012”) int shapeSize = 100; //variable for the initial size of the shapes int theShape = 'E'; void setup() { size(600, 400); background(125, 208, 124); // cool green background color // stroke(0); // black stroke -­‐ or it doesn't have to be there, because there is a black stroke automatically. smooth(); rectMode(CENTER); //draws the rectangle centered on the current mouse position ellipseMode(CENTER); //draws the ellipse centered on the current mouse position } void draw() { if (mousePressed) { // when the mouse is clicked 'something' will happen switch(theShape) { // use instead of the 'if else' -­‐ uses cases 'E' and 'R' in this program case 'E': ellipse(mouseX, mouseY, shapeSize, shapeSize); break; case 'R': rect(mouseX, mouseY, shapeSize, shapeSize); break; } } } void keyPressed() { if (key == 'e' || key == 'E') { // when e or E is pressed it chooses the case 'E' and draws an ellipse theShape='E'; }

2


if (key == 'r' || key == 'R') { // when r or R is pressed it chooses the case 'R' and draws a rectangle theShape='R'; } if (key == '+') { //when you press + the shape size increases by 10 shapeSize+=10; } if (key == '-­‐') { //when you press -­‐ the shape size decreases by 10 shapeSize-­‐=10; if (shapeSize < 0) { // this makes sure the shape size cannot be smaller than 0 shapeSize = 0; } } }

QUESTION 4 (IN DROPBOX “SIGNE HARRING HANSEN BITP2012”) Wheel w1, w2; void setup() { size(800, 400); noStroke(); smooth(); colorMode(HSB, TWO_PI, 100, 100); w1 = new Wheel(200, 200, 7); // creates the two wheels with different N (number of 'wheels') w2 = new Wheel(600, 200, 5); } void draw() { background(1, 0, 25); w1.display(); //draws the display function of w1 w2.display(); //draws the display function of w2 } class Wheel { // class for the wheel float x; //position x float y; //postion y int N; float angle; float spin; Wheel(float tempX, float tempY, int tempN) { // the constructer for the Wheel class x = tempX; // the temporarily position x y = tempY; // the temporarely position y N = tempN; // the number of 'wheels' angle = TWO_PI/(N*2); spin = 0; } void display() { translate(x, y); // translate and spins rotate(spin); // looping through a circle to create the colorwheel for (float a=0; a<TWO_PI-­‐angle; a+=2*angle) { fill(a, 100, 100); arc(0, 0, 380, 380, a, a+angle); } fill(1, 0, 25); ellipse(0, 0, 50, 50); rotate(-­‐spin); // translates and spins back again, so it doesn't affect more of the program translate(-­‐x, -­‐y);

3


spin += 0.01; } }

QUESTION 5 (IN DROPBOX “SIGNE HARRING HANSEN BITP2012”) Fish[] fish = new Fish[100]; //Declares the object as an array of the fish class -­‐ set to 100 fish -­‐ can be changed to whatever void setup() { size(600, 400); smooth(); fill(255); for (int i = 0; i < fish.length; i++) { fish[i] = new Fish(random(45, 555), random(35, 365), 1); //creates the fish object with the parameters (random x and y coordinates makes the position individual for each fish) if (i > fish.length/2) { //makes half of them go in the other direction fish[i] = new Fish(random(45, 555), random(35, 365), -­‐1); } } } void draw() { background(200); stroke(0); line(30, 30, 30, 370); //the three lines creates the fish tank line(30, 370, 570, 370); line(570, 370, 570, 30); for (int i = 0; i < fish.length; i++) { noStroke(); fish[i].display(); //displays the ellipse fish[i].move(); //call the move function from the class } } class Fish { // class for the fish float x; float y; float speed = random(0.1, 2); // the speed will be random/individual for each fish int direction; Fish(float tempX, float tempY, int initialDirection) { x = tempX; y = tempY; direction = initialDirection; } void move() { x += speed * direction; //x will increase by the speed and in either direction (see in setup) if ((x > 555) || (x < 45)) { // the aquarium minus and plus 15, because that is the radius of my 30px long fish direction = -­‐direction; //flips direction when it hits the walls in the aquarium } } void display() { ellipse(x, y, 30, 10); // the actual 'fish' } }

4


QUESTION 6 (IN DROPBOX “SIGNE HARRING HANSEN BITP2012”) float length=100; //the length of the horizontal and vertical bars of the T at level 1 void setup() { size(400, 200); smooth(); noLoop(); // stops looping } void draw() { background(255); stroke(0); branch(width/2, height, length); // the branch recursion function } void branch(float x, float y, float length) { line(x, y, x, y-­‐length); line(x -­‐ length/2, y-­‐length, x + length/2, y-­‐length); if (length/2 >= 2) { // recursion when the sides of the T are above or equals 2 (which means the recursion stops if it goes below 2) branch(x+length/2, y-­‐length, length/2); // draws a new T at one end of the other T half the size branch(x-­‐length/2, y-­‐length, length/2); // times two of course (in each end) } }

QUESTION 7 (IN DROPBOX “SIGNE HARRING HANSEN BITP2012”) void setup() { float weightKilos = 57; //changed to float -­‐ gives a more correct answer float heightCentimeters = 165; //changed to float -­‐ gives a more correct answer println("Your bmi is"); //changed this, since I did not find it necessary to call a function float bmi = weightKilos/(heightCentimeters*heightCentimeters/10000); // made a bmi variable instead, where it calculates the bmi float result = bmi; // the result is then the calculated bmi println(result); // then it prints the result if (bmi < 18.5) { // the below if statements print lines according to the result println("Your bmi is less than what is considered healthy"); } if (bmi >= 18.5 && bmi <= 25) { println("Your bmi is considered healthy"); } if (bmi > 25) { println("Your bmi is more than what is considered healthy"); } }

QUESTION 8 (IN DROPBOX “SIGNE HARRING HANSEN BITP2012”) void setup() { size(400, 200); smooth(); //a good function to call if your working with ellipses or other shapes with round edges. } void draw() { fillFunction(); // calls the fillFunction strokeFunction(); // calls the strokeFunction }

5


void fillFunction() { //fill(): The fill function sets the color used to fill shapes. Colors are by default in RGB (each value ranges from 0 to 255). There can also be a 4th value -­‐ the alpha value -­‐ that sets transparency. If you only want colors on the grey scale you only write one value between 0 and 255. //Example noStroke(); // The noStroke function is used to cancel the stroke. Usually, there is a stroke by default, so if you do not want a stroke on your shape, you can use this function to disable it. fill(264, 200, 10); //filled yellow ellipse ellipse(100, 100, 50, 50); } void strokeFunction() { //stroke(): The stroke function sets the color used to draw lines and borders around shapes. //Example noFill(); // The noFill function is, as the noStroke function, used to remove any fill to a shape. stroke(255, 0, 0); // red stroke strokeWeight(10); // The strokeWeight function sets the weight/width or thickness of the stroke -­‐ can be used on lines, point and other shapes. This is a weight of 10 (the middle ellipse). ellipse(200, 100, 50, 50); strokeWeight(2); // here I set the strokeWeight lower -­‐ you can see the difference on the two red stroked ellipses. ellipse(300, 100, 50, 50); }

6


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.