Computation as an Expressive Medium Lab 2: Polygons, Transformations, and Arrays Evan.

Slides:



Advertisements
Similar presentations
Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming.
Advertisements

MATH 224 – Discrete Mathematics
This terms course Last term we both worked on learning 2 things –Processing –The concepts of graphics etc. This term will focus more on the basic concepts.
A Quick Introduction to Processing
Recursion CS 367 – Introduction to Data Structures.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
IAT 334 Lab 2 Computer Graphics: Rocket, PImage. June 4, 2010IAT 3342 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.
Translate, Rotate, Matrix Pages Function Function Definition Calling a function Parameters Return type and return statement.
Translation and Rotation in 2D and 3D David Meredith Aalborg University.
You will need your think book.. Review… An angle is … Draw an angle with points A, B, C Label the angle ABC or CBA Point to the vertex of the angle. A.
Searching and Sorting SLA Computer Science 4/16/08 Allison Mishkin.
, Fall 2006IAT 800 Lab 2: Polygons, Transformations, and Arrays.
IAT 800 Lecture 4. Sept 18, Fall 2006IAT 8002 Outline  Programming concepts –Methods –Classes  Talk about project 1  Reading: Read Chapters 1-4 of.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
IAT 355 Lecture 4 Computer Graphics: Rocket. May 9, 2014IAT 3552 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Classes / Objects An introduction to object-oriented programming.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that.
February 14,  Relations: in mathematics, we refer to a group of points as a “relation” Examples: { (2, -3); (2, 5); (5,1) }
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Module 8 Lesson 6. Objective Combine shapes to create a composite shape; create a new shape from composite shapes.
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of different colors. Don’t show the grey grid.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Lecture 6: 3D graphics Concepts 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271 
CS107 References and Arrays By Chris Pable Spring 2009.
Introduction to Using the Notebook 10 Software for SMART Board Day 2 LIVINGSTON PARISH PUBLIC SCHOOLS Facilitated by S. Waltman.
1 ball, 2 ball, red ball, blue ball By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Geometric transformations!
Data Structures & Algorithms
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
CRE Programming Club - Class 2 Robert Eckstein and Robert Heard.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Vertices, Curves, Color, Images mostly without details 02/16/2010.
Computation as an Expressive Medium Lab 2: Polygons, Transformations, and Arrays (Oh My) Micah.
Data Structures Arrays and Lists Part 2 More List Operations.
Aims: To learn about some simple sorting algorithms. To develop understanding of the importance of efficient algorithms. Objectives: All:Understand how.
Array Sort. Sort Pass 1 Sort Pass 2 Sort Pass 3.
Bubble sort. Quite slow, but simple Principles: Compare 2 numbers next to each other (lets call it current and the one next to it) If the current number.
Review Objects – data fields – constructors – Methods Classes.
In the last several lessons, you have described translations using coordinates. You have also developed strategies for determining where an object started.
Functions. functions: a collection of lines of code with a name that one can call. Functions can have inputs and outputs.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
Programming in Processing Taught by Ms. Madsen Assistants: Ms. Fischer and Ms. Yen Winsor School, 2/20/08.
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
Data Structures I (CPCS-204) Week # 5: Recursion Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Computer Graphics: Rocket, Java: Class
Chapter 14, Translate & Rotate
Polygons, Transformations, and Arrays
CS/COE 0447 (term 2181) Jarrett Billingsley
LCC 6310 Computation as an Expressive Medium
Selection Sort Find the smallest value in the array. Put it in location zero. Find the second smallest value in the array and put it in location 1. Find.
Introduction to Programming
IAT 265 Lecture 2: Java Loops, Arrays Processing setup, draw, mouse Shapes Transformations, Push/Pop.
Search,Sort,Recursion.
Translate, Rotate, Matrix
Computation as an Expressive Medium
Presentation transcript:

Computation as an Expressive Medium Lab 2: Polygons, Transformations, and Arrays Evan

On this Episode of Lab... Arrays, or Dude, What's My Index? Arrays, or Dude, What's My Index? Building Complex Shapes Building Complex Shapes Translation and Rotation Translation and Rotation Pushing and Popping Pushing and Popping

Arrays Arrays store the same kind of data that variables do, they just have more than one “spot” for it under one name Arrays store the same kind of data that variables do, they just have more than one “spot” for it under one name If a variable is like a 1/2” binder, an array is a much bigger binder If a variable is like a 1/2” binder, an array is a much bigger binder

Array Example: Bubblesort Sorting functions sort arrays! Sorting functions sort arrays! What 'sorting' means depends on the contents of the array, but with numbers it usually means 'smallest to largest' What 'sorting' means depends on the contents of the array, but with numbers it usually means 'smallest to largest' Bubblesort is the “simplest” sorting algorithm Bubblesort is the “simplest” sorting algorithm 'Bubble' larger values to the end of the array, one by one 'Bubble' larger values to the end of the array, one by one

Bubblesort, the code void bubblesort(int[] input)‏ { int temp = -1; for(int i = 0; i < input.length; i++)‏ { for(int j = 0; j < input.length - 1; j++)‏ { if(input[j] > input[j+1])‏ { temp = input[j+1]; input[j+1] = input[j]; input[j] = temp; } //end if } //end for(j)‏ } //end for(i)‏ }

Bubblesort, explained void bubblesort(int[] input)‏ { int temp = -1; for(int i = 0; i < input.length; i++)‏ { for(int j = 0; j < input.length - 1; j++)‏ { if(input[j] > input[j+1])‏ { temp = input[j+1]; input[j+1] = input[j]; input[j] = temp; } //end if } //end for(j)‏ } //end for(i)‏ } One pass per element in the array

Bubblesort, explained void bubblesort(int[] input)‏ { int temp = -1; for(int i = 0; i < input.length; i++)‏ { for(int j = 0; j < input.length - 1; j++)‏ { if(input[j] > input[j+1])‏ { temp = input[j+1]; input[j+1] = input[j]; input[j] = temp; } //end if } //end for(j)‏ } //end for(i)‏ } Check each element against the next...but don't run off the end of the array!

Bubblesort, explained void bubblesort(int[] input)‏ { int temp = -1; for(int i = 0; i < input.length; i++)‏ { for(int j = 0; j < input.length - 1; j++)‏ { if(input[j] > input[j+1])‏ { temp = input[j+1]; input[j+1] = input[j]; input[j] = temp; } //end if } //end for(j)‏ } //end for(i)‏ } If the current element is larger than the next......swap their positions! Note that we use a 'temp' variable to hold our value.

Building Special Shapes The beginShape() and endShape() functions allow us to draw irregular shapes from any number of points we define. The beginShape() and endShape() functions allow us to draw irregular shapes from any number of points we define. Many types of Shape: Many types of Shape: POINTS, LINES, LINE_STRIP, LINE_LOOP, TRIANGLES, TRIANGLE_STRIP, TRIANGLE_FAN, QUADS, QUAD_STRIP, POLYGON POINTS, LINES, LINE_STRIP, LINE_LOOP, TRIANGLES, TRIANGLE_STRIP, TRIANGLE_FAN, QUADS, QUAD_STRIP, POLYGON POLYGON will be the most useful. POLYGON will be the most useful. …

Building Polygons beginShape(POLYGON); beginShape(POLYGON); Tells the program to start the polygon. Tells the program to start the polygon. vertex(x, y); vertex(x, y); Make as many calls to this as you have vertices in your polygon. Make as many calls to this as you have vertices in your polygon. endShape(); endShape(); Finishes the shape, connecting the last vertex to the first vertex to close the polygon, then colors it with the current fill() color. Finishes the shape, connecting the last vertex to the first vertex to close the polygon, then colors it with the current fill() color.

Building Polygons beginShape(POLYGON); beginShape(POLYGON); vertex(10, 50); vertex(10, 50); (starts a new polygon, and begins at point (10, 50).)‏

Building Polygons vertex(20, 10); vertex(20, 10); vertex(30, 40); vertex(30, 40); vertex(80, 60); vertex(80, 60); vertex(40, 80); vertex(40, 80); (adds 4 more points to the polygon, and connects them in the order they are called.)‏

Building Polygons endShape(); endShape(); (connects the last point to the first point, and fills the polygon.)‏

Let’s Use Arrays How can we apply arrays to this? Let’s store the points that we’re drawing. How can we apply arrays to this? Let’s store the points that we’re drawing. int[] xvals = {10, 20, 30, 80, 40}; int[] yvals = {50, 10, 40, 60, 80}; beginShape(POLYGON); for(int i = 0; i < xvals.length; i++) { vertex(xvals[i], yvals[i]); } endShape();

Let’s Use Arrays Great! Now we know a somewhat shorter way of doing the same thing. Great! Now we know a somewhat shorter way of doing the same thing. Well, we can also use those arrays to draw the same shape somewhere else. Well, we can also use those arrays to draw the same shape somewhere else. beginShape(POLYGON); for(int i = 0; i < xvals.length; i++) { vertex(xvals[i] + 10, yvals[i] + 10); } endShape();

Now it’s time to break your brain. What if there were an easier way than adding to these values each time? What if there were an easier way than adding to these values each time?

Translation Translation gives us another way of drawing in a new location. It in essence, moves the point (0, 0) in our window. Translation gives us another way of drawing in a new location. It in essence, moves the point (0, 0) in our window. (0, 0)‏ beginShape(POLYGON); for(int i = 0; i < xvals.length; i++) { vertex(xvals[i], yvals[i]); } endShape();

Translation After the call to translate(), any drawing functions called will treat our new orange point as if it were (0, 0). After the call to translate(), any drawing functions called will treat our new orange point as if it were (0, 0). (10, 10)‏ translate(10, 10); (0, 0)‏

Translation So now, if we write the exact same polygon code as above, the new polygon will be 10 pixels down and 10 pixels to the right. So now, if we write the exact same polygon code as above, the new polygon will be 10 pixels down and 10 pixels to the right. (10, 10)‏ beginShape(POLYGON); for(int i = 0; i < xvals.length; i++) { vertex(xvals[i], yvals[i]); } endShape(); (0, 0)‏

Rotation Much like Translation, Rotation moves our drawing space, so that we can draw at different angles. Much like Translation, Rotation moves our drawing space, so that we can draw at different angles. Most of the time, you’ll want to use Rotation in conjunction with Translation, because rotate() rotates the drawing window around the point (0, 0). Most of the time, you’ll want to use Rotation in conjunction with Translation, because rotate() rotates the drawing window around the point (0, 0).

Rotation Let’s look at an example without translation: Let’s look at an example without translation: rect(10, 10, 50, 50); (0, 0)‏

Rotation Make a variable with the value for 45 degrees in Radians. Make a variable with the value for 45 degrees in Radians. float angle = radians(45); (0, 0)‏ ( radians() takes an int or float degree value and returns a float radian value. If you’re confused about the concept of radians, ask me afterward.)‏

Rotation Rotate our drawing canvas 45 degrees around the origin. Rotate our drawing canvas 45 degrees around the origin. rotate(angle); (0, 0)‏ (You can see that one problem now is that much of our drawing canvas is now outside of the window.)‏

Rotation Draw the same square, now relative to our rotated canvas. Draw the same square, now relative to our rotated canvas. rect(10, 10, 50, 50); (0, 0)‏ (We only get to see about half of our square, and it isn’t really rotated in any satisfactory way.)‏

Rotation Let’s try this from the start, using translation. Let’s try this from the start, using translation. Where should we translate to? Where should we translate to? The point around which we want to rotate. So let’s try and rotate around the center of the square. The point around which we want to rotate. So let’s try and rotate around the center of the square. This means moving the origin, and drawing the square around it. This means moving the origin, and drawing the square around it.

Rotation Let’s start with setting our rotation point: Let’s start with setting our rotation point: translate(35, 35); (0, 0)‏ (35, 35)‏

Rotation Now let’s draw a square with this point at its center. Now let’s draw a square with this point at its center. rect(-25, -25, 50, 50); (0, 0)‏ (35, 35)‏

Rotation Then let’s do the same rotate we did last time. Then let’s do the same rotate we did last time. float angle = radians(45); rotate(angle); (0, 0)‏ (35, 35)‏

Rotation Now when we draw the same square as before, it will have the same center point. Now when we draw the same square as before, it will have the same center point. float angle = radians(45); rotate(angle); (0, 0)‏ (35, 35)‏

Rotation Try applying rotation to your animations using draw(). What variable will you want to iterate to make a shape rotate over time? Try applying rotation to your animations using draw(). What variable will you want to iterate to make a shape rotate over time? Try making a custom polygon rotate instead of a square. Try making a custom polygon rotate instead of a square.

Wait! How do I get back to normal?! If you plan to do a lot of translations and rotations, it helps to know about the concepts of push and pop… If you plan to do a lot of translations and rotations, it helps to know about the concepts of push and pop… (0, 0)‏ (35, 35)‏ (60, 15)‏ I just want to go back to Kansas! I just want to go back to where I started before this whole crazy mess!

Pushing and Popping Pushing is a way to say: “Hey, computer! Remember this orientation!” Pushing is a way to say: “Hey, computer! Remember this orientation!” PushMatrix(); PushMatrix(); Popping is a way to say: “AAAAAAAAAA! Computer, take me back to the way things once were!” Popping is a way to say: “AAAAAAAAAA! Computer, take me back to the way things once were!” PopMatrix(); PopMatrix();

Push & Pop …IN ACTION! If we want to remember the original orientation… If we want to remember the original orientation… pushMatrix(); translate(35,35); rotate( radians(45) ); rect(-25,-25,50,50); popMatrix(); rect(-25,-25,50,50); (0, 0)‏ You can push and pop as many times as you want. It’s like you’re writing an address for the way things were on a card, and putting it on a stack each time you push… and pop just takes the first card off the top of the stack. (35, 35)‏

How is this useful? You don’t have to remember the math to reverse all the translations and rotations you’ve done! You don’t have to remember the math to reverse all the translations and rotations you’ve done! You can make spiral-y shapes, then go back to normal! You can make spiral-y shapes, then go back to normal! You can make drawings with limbs! You can make drawings with limbs! (You don’t want to have to calculate the angles of every finger, do you?)‏

Confused yet? We went over: We went over: Arrays Arrays Drawing Polygons Drawing Polygons Translation and Rotation Translation and Rotation PushMatrix and PopMatrix PushMatrix and PopMatrix Fig. 2: Cranial leakage. ow.