DOC PREVIEW
GT LCC 6310 - LCC 6310 The Computer as an Expressive Medium

This preview shows page 1-2-17-18-19-35-36 out of 36 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

LCC 6310 The Computer as an Expressive Medium Lecture 5Overview Programming concepts Methods Classes Processing, pp.301-318Drawing a rocket background(0); fill(255); triangle(10, 0, 0, 20, 20, 20); rectMode(CORNERS); rect(5, 20, 8, 23); rect(12, 20, 15, 23); Let's try this in Processing…Now I want to draw several rockets I want several rockets in different locations on the screen I could copy and paste the code But I would need to adjust all the numbers for the new locations Or… I can just define a method…First method for drawing a rocket void drawRocket() { fill(255); triangle(10, 0, 0, 20, 20, 20); rectMode(CORNERS); rect(5, 20, 8, 23); rect(12, 20, 15, 23); } Now how do we use this code? Can we just call the method at the top of the file? Let's try it… Note: we can call the method whatever we want, but it's conventional to give methods names that begin with a lower case letter E.g. drawRocket rather than DrawRocketFirst method for drawing a rocket void drawRocket() { fill(255); triangle(10, 0, 0, 20, 20, 20); rectMode(CORNERS); rect(5, 20, 8, 23); rect(12, 20, 15, 23); } Now how do we use this code? Can we just call the method at the top of the file? Let's try it… causes an error!! You can’t just directly call drawRocket() at the top of the file Once you start using methods, all code must be in methods…Calling drawRocket() void setup() { drawRocket(); } void drawRocket() { fill(255); triangle(10, 0, 0, 20, 20, 20); rectMode(CORNERS); rect(5, 20, 8, 23); rect(12, 20, 15, 23); }This didn’t seem to help much… Our method still just draws a rocket at one fixed location We need arguments that allow us to tell the program where we want the rocket! This means we need to figure out the relationship between the position of the rocket and the location of the rest of its parts Note: Argument variables are available within the method, but not outside (method scope)drawRocket() with arguments void drawRocket(int noseX, int noseY) { // draw a rocket with respect to its nose // other parts of the rocket with respect to its nose int bottomOfRocket = noseY + 20; int leftOfRocket = noseX - 10; int rightOfRocket = noseX + 10; fill(255); // draw the rocket body triangle(noseX, noseY, leftOfRocket, bottomOfRocket, rightOfRocket, bottomOfRocket); // draw the rocket feet rectMode(CORNERS); rect(leftOfRocket + 5, bottomOfRocket, leftOfRocket + 8, bottomOfRocket + 3); rect(rightOfRocket - 8, bottomOfRocket, rightOfRocket - 5, bottomOfRocket + 3); }Now to specify rotation Currently we specify the nose position and it draws a rocket that faces straight up To rotate the rocket, we would need to figure out the new position of the vertices That's hard! Instead of rotating the rocket, let's rotate the paper As long as we're rotating the paper, might as well slide it around as well To prepare for translating and rotating, first let's draw the rocket centered around the origin (0,0)Centering the rocket around the origin Currently the rocket draws with respect to its nose You can see this by calling drawRocket(0,0) Now we want to draw the rocket around the origin We know the rocket dimensions are: int bottomOfRocket = noseY + 20; int leftOfRocket = noseX - 10; int rightOfRocket = noseX + 10; From this, we know our rocket has a width and height of 20 pixels, so we can define some constants: final int halfHeight = 10; final int halfWidth = 10; Now let's use these to draw the rocket…Body and feet around the origin Drawing the rocket body used to look like: triangle(noseX, noseY, leftOfRocket, bottomOfRocket, rightOfRocket, bottomOfRocket); Now instead we start at the origin and use halfHeight and halfWidth: triangle(0, -halfHeight, -halfWidth, halfHeight, halfWidth, halfHeight); Drawing the rocket feet used to look like: rect(leftOfRocket + 5, bottomOfRocket, leftOfRocket + 8, bottomOfRocket + 3); rect(rightOfRocket - 8, bottomOfRocket, rightOfRocket - 5, bottomOfRocket + 3); Now instead we center around the origin: rect(-halfWidth + 5, halfHeight, -halfWidth + 8, halfHeight + 3); rect(halfWidth - 8, halfHeight, halfWidth - 5, halfHeight + 3);Putting it together void drawRocket(int x, int y, float rot) { final int halfHeight = 10; final int halfWidth = 10; triangle(0, -halfHeight, -halfWidth, halfHeight, halfWidth, halfHeight); rectMode(CORNERS); rect(-halfWidth + 5, halfHeight, -halfWidth + 8, halfHeight + 3); rect(halfWidth - 8, halfHeight, halfWidth - 5, halfHeight + 3); } Note: we’re purposely ignoring the arguments for now!Now add translation and rotation void drawRocket(int x, int y, float rot) { final int halfHeight = 10; final int halfWidth = 10; translate(x, y); rotate(rot); triangle(0, -halfHeight, -halfWidth, halfHeight, halfWidth, halfHeight); rectMode(CORNERS); rect(-halfWidth + 5, halfHeight, -halfWidth + 8, halfHeight + 3); rect(halfWidth - 8, halfHeight, halfWidth - 5, halfHeight + 3); } Let’s try drawing several rockets… drawRocket(50,50,HALF_PI); drawRocket(25,25,HALF_PI);Works fine for one call… But when we call it twice, it doesn’t seem to work Looks like first call messes up subsequent calls What is happening?! When you move the paper (translate, rotate) the paper stays moved Subsequent paper moves (additional translates and rotates) build on top of the previous ones So what we want is to move the paper, draw, and then move it back We need pushMatrix() and popMatrix() pushMatrix() remembers the previous paper position (i.e. the previous transformation matrix) popMatrix() restores the paper to the remembered position (i.e. restores the transformation matrix)Using pushMatrix() and popMatrix() void drawRocket(int x, int y, float rot) { final int halfHeight = 10; final int halfWidth = 10; pushMatrix(); translate(x, y); rotate(rot); triangle(0, -halfHeight, -halfWidth, halfHeight, halfWidth, halfHeight); rectMode(CORNERS); rect(-halfWidth + 5, halfHeight, -halfWidth + 8, halfHeight + 3); rect(halfWidth - 8, halfHeight, halfWidth - 5, halfHeight + 3); popMatrix(); } Let’s try drawing several rockets now… drawRocket(50,50,HALF_PI); drawRocket(25,25,HALF_PI);Classes Java (Processing) is an object-oriented language This means that parts of your program that you treat as conceptual things, become things (objects) in the program code Objects are built from classes Classes are the blueprint, objects are built from the blueprint Objects are called


View Full Document

GT LCC 6310 - LCC 6310 The Computer as an Expressive Medium

Documents in this Course
Load more
Download LCC 6310 The Computer as an Expressive Medium
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LCC 6310 The Computer as an Expressive Medium and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LCC 6310 The Computer as an Expressive Medium 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?