Chapter 6 Loops (iteration).

Slides:



Advertisements
Similar presentations
Game with US Beginner Tutorial. Welcome!! Who I am What is Processing? Basic Coding Input Methods Images Classes Arrays.
Advertisements

Variables Conditionals Loops The concept of Iteration Two types of loops: While For When do we use them? Iteration in the context of computer graphics.
IAT 334 Java using Processing ______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
, Fall 2006IAT 800 Lab 2: Polygons, Transformations, and Arrays.
OOP&M - laboratory lectures1 OOP&M – LAB3 LABtres: drawing.
Lecture 3 IAT 800. Sept 15, Fall 2006IAT 8002 Suggestions on learning to program  Spend a lot of time fiddling around with code –Programming is something.
PROCESSING Animation. Objectives Be able to create Processing animations Be able to create interactive Processing programs.
Using Coordinate Geometry to Create Moving Images In unit 29 we created a simple drawing for the background of a children’s game. We are going to start.
CHAPTER 11 Tables. How Are Tables Used Data Display  Very tidy and very useful Better Text Alignment  Putting text in tables allows you to format indents.
Review Blocks of code {.. A bunch of ‘statements’; } Structured programming Learning Processing: Slides by Don Smith 1.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Animating Coordinate Geometry In this unit we will expand upon our knowledge of using counters to create motion pictures. By using incremental mathematics,
Continuous February 16, Test Review What expression represents the zip car eligibility rules of at least 18 years old and no incidents?
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Keyboard and Events. What about the keyboard? Keyboard inputs can be used in many ways---not just for text The boolean variable keyPressed is true if.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
© Calvin College, For a number of years I have been familiar with the observation that the quality of programmers is a decreasing function of the.
Review Loops – Condition – Index Functions – Definition – Call – Parameters – Return value.
Graphic Basics in C ATS 315. The Graphics Window Will look something like this.
B. RAMAMURTHY Simulating Motion and Implementing Animation.
CIS 3.5 Lecture 2.2 More programming with "Processing"
2-D Shapes, Color, and simple animation. 7 Basic Shapes Ellipse :: ellipse() Arc :: arc() Line :: line() Point :: point() Rectangle :: rect() Triangle.
Lesson Two: Everything You Need to Know
Animation Pages Function Function Definition Calling a function Parameters Return type and return statement.
Often being different. Control flow By default Java (and therefore Processing) executes lines of a program one after the other –Doesn’t matter what happened.
Repetition 8/9/2009. Learning to Program -- Suggestions Spend a lot of time fiddling around with code –Programming is something you have to learn by doing.
Computer Science I Recap: variables & functions. Images. Pseudo-random processing.
CS305j Introduction to Computing Simple Graphics 1 Topic 11 Simple Graphics "What makes the situation worse is that the highest level CS course I've ever.
Continuous. Flow of Control Programs can broadly be classified as being –Procedural Programs are executed once in the order specified by the code varied.
Review Expressions and operators Iteration – while-loop – for-loop.
Test Review. General Info. All tests will be comprehensive. You will be tested more on your understanding of code as opposed to your ability to write.
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
Sound and more Animations
Processing Lecture.3 Loop and animation
Some of Chap 17.
high-level operations on pictures
Classwork: Examine and enhance falling drop(s) or make your own.
Chapter 14, Translate & Rotate
Building Java Programs
Lesson One: The Beginning Chapter 1: Pixels Learning Processing Daniel Shiffman Presentation by Donald W. Smith Graphics from
20 minutes maximum exhibits
Chapter 7 Functions.
Basic Graphics Drawing Shapes 1.
Computation as an Expressive Medium
Chapter 7 Functions.
Exam1 Review CSE113 B.Ramamurthy 11/29/2018 B.Ramamurthy.
Programming for Artists
Chapter 10 Algorithms.
Chapter 5, Conditionals Brief Notes
Chapter 10 Algorithms.
More programming with "Processing"
Writing Functions.
Chapter 8 Objects.
Lecture 7: Introduction to Processing
IAT 265 Lecture 2: Java Loops, Arrays Processing setup, draw, mouse Shapes Transformations, Push/Pop.
Just a question for Mac users:
Exam1 Review CSE113 B.Ramamurthy 4/17/2019 B.Ramamurthy.
LCC 6310 Computation as an Expressive Medium
Loops & Nested Loops CSE 120 Winter 2019
Chapter 10 Algorithms.
Chapter 4, Variables Brief Notes
Computation as an Expressive Medium
Trigonometry & Random March 2, 2010.
Continuous & Random September 21, 2009.
Loops and Iteration CS 21a: Introduction to Computing I
Chapter 13, Math A few Examples.
Agenda for Unit 5: Control Structures
Chapter 9 Arrays.
Presentation transcript:

Chapter 6 Loops (iteration)

Chapter Goals Learn the concept of iteration Learn two types of loops: while() and for() functions Learn about scope of variables: global & local Use iteration to create complex graphics

Before we begin looping! Take 2 minutes to create the following rectangles that are: 20 pixels wide 5 pixels in between Takes up height of 100px window.

Don’t Quit! What if you needed this in a design?

Programming languages employ three types of loops Programming languages employ three types of loops. We will focus on for() and while(). for() is a shorthand for while()

Anatomy of while() while (expression) { //statements } For example: size(500,200); noFill(); int x = 0; while (x<width) { ellipse(x,100,100,100); x = x+50; Still another way of describing the full program: Variable for initial condition When should loop stop? What should happen each time through the loop?

Do one of these challenges 1. Create slide 3 example as a while() statement. 2. Create triangle that repeats across window.

Take a Look Open Example 6-3. Very simple, but pay attention to each variable. E.g. Note how y & len are used together. Note how x & spacing are used together. Try modifying a few variables.

Before moving on… ______________________ Do exercise 6.1a without looking at solution. Note that my remix involves new concept – nested while(). For exercise 6.1b, look at solution if you wish, but remix.

Anatomy of for() for(initialization; test; update) { //statements } An example: for (int i=0; i<120; i=i+20) { rect(i, 10, 15, 8); println(i);

Comparison of repetition No iteration While() For() size(100,200); line(0, 10, 100, 10); line(0, 20, 100, 20); line(0, 30, 100, 30); line(0, 40, 100, 40); line(0, 50, 100, 50); line(0, 60, 100, 60); line(0, 70, 100, 70); line(0, 80, 100, 80); line(0, 90, 100, 90); line(0, 100, 100, 100); line(0, 110, 100, 110); line(0, 120, 100, 120); line(0, 130, 100, 130); line(0, 140, 100, 140); line(0, 150, 100, 150); line(0, 160, 100, 160); line(0, 170, 100, 170); line(0, 180, 100, 180); line(0, 190, 100, 190); int i = 10; while (i < 200) { line(0, i, 100, i); i = i + 10; } for (int i=10; i<200; i=i+10) {

Exercise 6-2, page 102. Rewrite Exer 6-1 as a for loop. Remix: Then fill colors to dark random colors rather than w. Remix: Then to keep from flashing, use noLoop() in setup(). void setup() { size(200,200); background(255); } void draw() { float w = 200; while (w > 0) { stroke(0); fill(w); //fill(random(0,255), random(0,255), random(0,255)); ellipse(width/2, height/2, w, w); w = w - 20; Answer: float w=200; void setup() { size(200,200); //noLoop(); } void draw() { background(255); for (float w = 200; w>0; w = w-20 ) { stroke(0); fill(w); //fill(random(100), random(100), random(100)); ellipse(width/2, height/2, w, w); }//end draw

Brief Challenge Copy/paste Ravenel_bridge_starter code and do something else with it. E.g., draw towers, or piers, or anchorage, or pavement! //charleston bridge drawing starter int x1= 150; //top horiz position of 1st span int y1= 80; int x2= 0; //bottom horiz position of 1st span int y2= 200; int spacer = 15; //to spread out bottom of lines size(700,400); //first span of bridge for(x2=0; x2<=300; x2+= spacer) { line(x1, y1, x2, y2) ; } //second span of bridge for (x2 =350; x2<= 650; x2+=spacer) {//650 is width of both spans line(x1+350, y1, x2, y2);

Exercise 6-3 Open B because it is visually interesting. Note how you can simply try different associations and formulas to see what happens.   Open A, I had made a typo, which led me to an even more interesting remix. size(300,300); background(255); stroke(#0033ff); noFill(); int h = height/2; int i = 0; while(i <10) { ellipse(width/2, h, i*20, i+20); i++; h += 10 ; }

Nested Loops To make a nested loop, simply place a for statement inside another. size(480,120); background(0); noStroke(); for(int y = 0; y<=height; y += 40) { for(int x = 0; x<=width; x +=40) { fill(250, 100, 100 ); ellipse(x, y, 40,40); } Another version of same nested loop. size(480,120); background(0); noStroke(); for(int y = 0; y<=height; y += 40) { for(int x = 0; x<=width; x +=40) { fill(250, 100, 100 ); //fill salmon ellipse(x, y, 40,40); //green circle covering every other salmon circle fill(6, 200, 100 ); ellipse(x+40, y, 40,40); x += 40; } This is off-topic. I just thought I’d share the inverted round corner. rect( 40, 50 , 20, 20,-20);

Another Nested loop size(400,400); int xVal = 20; //this is the increment; please mess around with it int yVal = 20; for(int y= 10; y <= height; y= y + yVal ) { for(int x= 10; x <= width; x= x + xVal ) { ellipse(x, y, 10, 10); }

Variable Scope Variables can be declared anywhere in the sketch. The concept is called scope Global variables are accessible anywhere in the sketch. Local variables are limited to the code block in which they reside. Example: if() or for() function where a variable is declared for that block. Try example 6-7 and note that “x” is not accessible outside of draw() block. Advice on handling scope: Try to place variables where they are used because it is more efficient and less confusing. Some programmers, however, do like to see most of their variables at top. So it depends.

We probably won’t get to this, but take a look and see how it works Fuzzy line size(300,200); background(0); int totalPts = 200; float steps = totalPts + 1; stroke(#ffffff); strokeWeight(2); for(int i = 1; i<steps; i++) { point((width/steps)* i , (height/2) + random(-5, 5)); } Funnel size(300,300); background(0); int totalPts = 300; float steps = totalPts + 1; stroke(255); float rand = 0; for(int i = 1; i<steps; i++) { point((width/steps)*i, (height/2) + random(-rand, rand)); rand += .1; } Wavy dots //RUN THIS ABOUT 10 TIMES. size(300,300); background(0); int totalPts = 300; float steps = totalPts + 1; stroke(255); strokeWeight(3); float rand = 0; for(int i = 1; i<steps; i++) { point((width/steps)*i, (height/2) + random(-rand, rand)); rand += random(-5, 5); }

Delete the fill and the triangle Life is like a box of chocolates. You never know what you’re gonna get. You can alter or remove almost any line of code and achieve remarkable difference. Eg. Delete fill on line 16 Delete noStroke Delete the fill and the triangle Delete noLoop and place fill(random(255)); in for() See for more info: http://btk.tillnagel.com/tutorials/rotation-translation-matrix.html /*originally started out with creating a spiral, which would mainly happen by translating, then incrementing the x axis and rotate(.1) However i did a remix into this abstraction */ void setup(){ size(500,500); background(0); //frameRate(5 ); } void draw(){ noLoop(); translate(width/2, height/2); for (float i = 0; i <200; i++) { //strokeWeight(.25); fill(255,random(200), random(1,100), 15);//barely visible big circles rotate(3); ellipse(i, 0,100, 100); //unplanned triangle noStroke(); fill(255,random(200), random(1,100) ); triangle(i-5, 5, 10, 15, i+5, 8); }}

The if() is like that grade program that we did in chap 5 */ //overall goal: in rare cases, paint using yellow color void setup() { size(470, 200); //delete this to better track what's happening background(191,11,44); noLoop(); //delete for animation } void draw() { for(int x=20; x<width-20 ; x+=25) { //extra 20 to frame properly for(int y=20; y<height-20; y = y+25) { int num = int(random(0,100)); //var type converted to integer; note the localness //if the random generated # is >90, fill yellow //note that if() is embedded inside of for() if(num>90) { fill(255,255, 0); //if the random is between 50-90, fill black }else if(num>50) { fill(0); //if random is between 0-49, fill nothing }else { noFill(); ellipse(x, y, 20, 20); print(num + "\n"); //ask me about this }//end of for }//end draw /* this is a remix from funprogramming.org number 18. Please look at it also. /*This program is simply a grid, but it allows 3 different fill colors based on whether random # is between 90-100, then 50-89, or 0-49 The if() is like that grade program that we did in chap 5 */