This is an old revision of the document!
Afternoon Teams: (Angel, Ellie), (Clair, Emily, Mei-Ling), (Anastasia, Alix), (Float, Jeni), (Ed, Andrew), (Raymond, Will), (Zuri, Jove), (Fang, Penny)
Non Animating Sort
// GLOBAL VARIABLES int [] array = {90,60,70,50,40,30,20,10,1,80}; // int i, j, k, l; // SETUP FUNCTION - Runs Once void setup () { size(500,500); int min = 0; // index of the array // outer for-loop for (int i=0; i<10; i++) { min = i; // inner for-loop for (int j=i; j<10; j++) { if (array[j] < array[min]) min = j; } // swapping int temp = array[i]; array[i] = array[min]; array[min] = temp; } for (int k=0; k<10; k++) println(array[k]); } // DRAW LOOP - Infinite Loop void draw () { background(255,0,0); ellipse(mouseX, mouseY, 20, 20); }
Ball Code
int x = 30; int dx = 0; int y = 20; int dy = 0; void setup() { size (500,500); } void draw() { //background(255,255,0); fill(random(256),random(256),random(256)); ellipse(x,y,20,20); // d-pad = direction-pad if (keyPressed && key == 'd') { dx = 1; } else { dx = 0; } if (keyPressed && key == 's') { dy = 1; } else { dy = 0; } x = dx + x; y = dy + y; /* // Collision Detection if (y > 480 || y < 0) { dy = dy * -1; } y = y + dy; if (x > 480 || x < 0) { dx = dx * -1; } x = x + dx; */ }
Option 1: For those who've not gotten their sorts working, here's my selection sort from class. It's a little buggy: http://pastebin.com/hVFvXvD2
Option 2: If you are done your sort, take the beginning of class to add the following to the sort program:
Option 3: If you'd rather work on your Seurat (Pixel Splotches), then you can create a way to change the ellipse sizes while within the program.
Remember, if your partner is done with the project, to Fork it into your own directory. Forking will allow you to continue the project on your own. You can keep a copy for yourself.
Review: Go back to the lecture on Bouncing Balls. Try to create your own Ball class that can bounce around the screen.
Make 4 balls bounce around the screen.
You must create a Ball class. Read: http://www.processing.org/tutorials/objects/
See this sample code for the Car class.
// Example: Two Car objects Car myCar1; Car myCar2; // Two objects! void setup() { size(200,200); // Parameters go inside the parentheses when the object is constructed. myCar1 = new Car(color(255,0,0),0,100,2); myCar2 = new Car(color(0,0,255),0,10,1); } void draw() { background(255); myCar1.drive(); myCar1.display(); myCar2.drive(); myCar2.display(); } // Even though there are multiple objects, we still only need one class. // No matter how many cookies we make, only one cookie cutter is needed. class Car { color c; float xpos; float ypos; float xspeed; // The Constructor is defined with arguments. Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) { c = tempC; xpos = tempXpos; ypos = tempYpos; xspeed = tempXspeed; } void display() { stroke(0); fill(c); rectMode(CENTER); rect(xpos,ypos,20,10); } void drive() { xpos = xpos + xspeed; if (xpos > width) { xpos = 0; } } }
Mousey the Mouse wants to visit each of you next month. Download a world map, NOT the one on this page. Any kind of world map will do (as long as it's a valid image file format). Find out where everyone lives in the class and help Mousey plan the shortest possible trip. You will approximate the locations based off of the map you choose. Mousey has a jetpack and starts at Stanford.
Try to see if you can create objects like you did for the ball classes.
You may have realized that using a matrix (or 2-D array) makes it easy to represent the graph. Construct an Adjacency Matrix, where the weights of the edges are the distances between one point and every other point.
Using some algorithm… Maybe Breadth First Search, draw a path that on the screen that highlights what's going on in the code. See example of lines above.
REMINDER: Shortest Path is NOT the Traveling Sales Person problem. TSP is much harder. You do not have to return home like the image above.
Find the shortest distance that Mousey can go INCLUDING his return home.
Now make the program animate! (or interactive)
One idea is to be able to change Mousey's start location.
Cookbook! You know what to do.
Additional Challenge: Make your own SPRITES! This part is fun :)
(Help Mousey get his cheese)