LCC 6310 Computation as an Expressive MediumOutlineLoop() has nothing to do with timeDrawing a rocketNow I want to draw several rocketsFirst method for drawing a rocketDidn’t seem to help much…DrawRocket() with argumentsNow I’d like to specify rotationRocket centered around originNow add translation and rotationWorks fine for one call…Using push and popClassesParts of a classDefining the rocket classUsing the class to create instancesAdding a draw routine to our RocketCalling methods on objectsWhat else do we want to do to the Rocket?LCC 6310Computation as an Expressive MediumLCC 6310Computation as an Expressive MediumLecture 4Lecture 4OutlineOutline•Programming conceptsProgramming concepts•Loop •Methods•Classes•Discuss Schneiderman articleDiscuss Schneiderman article•Talk about Assignment 2Talk about Assignment 2Loop() has nothing to do with timeLoop() has nothing to do with time•The value returned by second (or milliseconds()) has The value returned by second (or milliseconds()) has nothing to do with how often loop() is callednothing to do with how often loop() is called•In loop() you draw frames – you don’t know how often In loop() you draw frames – you don’t know how often it will be called it will be called •Put a println in loop to see how often it gets called Put a println in loop to see how often it gets called long lastTimeLoopWasCalled = 0;long lastTimeLoopWasCalled = 0;void loop() {void loop() {long milliseconds = millis(); long milliseconds = millis(); println(milliseconds - lastTimeLoopWasCalled); println(milliseconds - lastTimeLoopWasCalled); lastTimeLoopWasCalled = milliseconds;lastTimeLoopWasCalled = milliseconds;}}Drawing a rocketDrawing a rocketbackground(0);background(0);fill(255);fill(255);triangle(10, 0, 0, 20, 20, 20);triangle(10, 0, 0, 20, 20, 20);rectMode(CORNERS);rectMode(CORNERS);rect(5, 20, 8, 23);rect(5, 20, 8, 23);rect(12, 20, 15, 23);rect(12, 20, 15, 23);Now I want to draw several rocketsNow I want to draw several rockets•Want several rockets in different Want several rockets in different locations on the screenlocations on the screen•I could copy and paste the codeI could copy and paste the code•Need to adjust all the numbers for the new location•Or… define a methodOr… define a methodFirst method for drawing a rocketFirst method for drawing a rocketvoid drawRocket() {void drawRocket() { fill(255);fill(255); triangle(10, 0, 0, 20, 20, 20);triangle(10, 0, 0, 20, 20, 20); rectMode(CORNERS);rectMode(CORNERS); rect(5, 20, 8, 23);rect(5, 20, 8, 23); rect(12, 20, 15, 23);rect(12, 20, 15, 23);}}Gotcha! Once you start using methods, all code must Gotcha! Once you start using methods, all code must be in methods (can’t just directly call be in methods (can’t just directly call drawRocket() at the top of the file)drawRocket() at the top of the file)Didn’t seem to help much…Didn’t seem to help much…•Still just draws a rocket at one fixed locationStill just draws a rocket at one fixed location•Need arguments that allow me to tell the Need arguments that allow me to tell the program where I want the rocket!program where I want the rocket!•Must figure out the relationship between the position and the location of the rest of the parts•Argument variables are available within the Argument variables are available within the method, but not outside (method scope)method, but not outside (method scope)DrawRocket() with argumentsDrawRocket() with argumentsvoid drawRocket(int noseX, int noseY) {void drawRocket(int noseX, int noseY) { int bottomOfRocket = noseY + 20;int bottomOfRocket = noseY + 20; int leftOfRocket = noseX - 10;int leftOfRocket = noseX - 10; int rightOfRocket = noseX + 10;int rightOfRocket = noseX + 10; fill(255);fill(255); triangle(noseX, noseY, leftOfRocket, bottomOfRocket, rightOfRocket, triangle(noseX, noseY, leftOfRocket, bottomOfRocket, rightOfRocket, bottomOfRocket);bottomOfRocket); rectMode(CORNERS);rectMode(CORNERS); rect(leftOfRocket + 5, bottomOfRocket, leftOfRocket + 8, rect(leftOfRocket + 5, bottomOfRocket, leftOfRocket + 8, bottomOfRocket + 3);bottomOfRocket + 3); rect(rightOfRocket - 8, bottomOfRocket, rightOfRocket - 5, rect(rightOfRocket - 8, bottomOfRocket, rightOfRocket - 5, bottomOfRocket + 3);bottomOfRocket + 3);}}Now I’d like to specify rotation Now I’d like to specify rotation •Currently I specify the nose position and it draws a Currently I specify the nose position and it draws a rocket that faces straight uprocket that faces straight up•To rotate the rocket, I’d need to figure out the new To rotate the rocket, I’d need to figure out the new position of the verticesposition of the vertices•Too hard!•Instead of rotating the rocket, rotate the paperInstead of rotating the rocket, rotate the paper•As long as I’m rotating the paper, I may as well slide it around as well•To prepare for translating and rotating, first draw the To prepare for translating and rotating, first draw the rocket centered around the origin (0,0)rocket centered around the origin (0,0)Rocket centered around origin Rocket centered around origin void drawRocket(int x, int y, float rot) {void drawRocket(int x, int y, float rot) {final int halfHeight = 10;final int halfHeight = 10;final int halfWidth = 10; final int halfWidth = 10; triangle(0, -halfHeight, -halfWidth, halfHeight, halfWidth, triangle(0, -halfHeight, -halfWidth, halfHeight, halfWidth, halfHeight); halfHeight); rectMode(CORNERS);rectMode(CORNERS); rect(-halfWidth + 5, halfHeight, -halfWidth + 8, halfHeight + rect(-halfWidth + 5, halfHeight, -halfWidth + 8, halfHeight + 3);3); rect(halfWidth - 8, halfHeight, halfWidth - 5, halfHeight + 3);rect(halfWidth - 8, halfHeight, halfWidth - 5, halfHeight + 3);}}We’re purposely ignoring the arguments for We’re purposely ignoring the arguments for nownowNow add translation and rotation Now add translation and rotation void drawRocket(int x, int y, float rot) {void drawRocket(int x, int y, float rot) {final int halfHeight = 10;final int halfHeight = 10;final int halfWidth = 10; final int halfWidth = 10; translate(x, y);translate(x, y);rotate(rot);rotate(rot);triangle(0, -halfHeight, -halfWidth, halfHeight, halfWidth, triangle(0, -halfHeight, -halfWidth, halfHeight, halfWidth, halfHeight); halfHeight); rectMode(CORNERS);rectMode(CORNERS); rect(-halfWidth + 5, halfHeight, -halfWidth + 8, halfHeight + rect(-halfWidth + 5, halfHeight, -halfWidth + 8, halfHeight +
View Full Document