Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 11 Fall 2010.

Slides:



Advertisements
Similar presentations
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Advertisements

RAPTOR Syntax and Semantics By Lt Col Schorsch
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.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 07 Fall 2010.
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Six compound procedures and higher-order procedures.
IAT 800 Foundations of Computational Art and Design Lecture 2.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 8 Fall 2010.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Chapter 4: Loops and Files
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
New Tools And Workshop Mod & For Loops. Modulo Calculates the remainder (remember long division?) % Examples: 7 % 3 10 % 2 2 % 3 evaluates to 1 evaluates.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
By Chad Blankenbeker.  The for-loop is best used when you know how many times it is going to be looped  So if you know you want it to only loop 10 times,
Overview of Java Loops By: Reid Hunter. What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
Lesson Two: Everything You Need to Know
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
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.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
CSD 340 (Blum)1 For Loops See Beginning JavaScript (Paul Wilton) p. 87.
Programming for Art: Arrays – 2D ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 16 Fall 2010.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
Flow Control. Comments u Comments: /* This is a comment */ –Use them! –Comments should explain: v special cases v the use of functions (parameters, return.
Follow up from lab See Magic8Ball.java Issues that you ran into.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Slides by Evan Gallagher
Slides by Evan Gallagher
Discussion 4 eecs 183 Hannah Westra.
Loop Structures.
Repetition Structures (Loops)
Counted Loops.
Programming for Art: Algorithms
Programming for Artists
Lecture 7: Introduction to Processing
Computing Fundamentals
Building Java Programs
Lecture 8:The For Loop AP Computer Science Principles.
Programming for Art: Images
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
SSEA Computer Science: Track A
Seating “chart” Front - Screen rows Back DOOR.
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Presentation transcript:

Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 11 Fall 2010

Processing - Loops We know about variables and some kinds of expressions. We know about conditions/flow of control (IF-THEN-ELSE) Consider now how we could do something many times. Of course, each time step our program gets called, and we can draw things and update positions, speeds, and so on. Let’s write some code that changes each line in the display to a different color or grey value each step, so that it seems to slowly shift in hue or brightness.

Loops Here’s what I mean:

Loops How to do it: 1.Let i be the column under consideration. 2.Draw a vertical line with grey level I 3. Repeat for i= i

Loops We can use a new statement: while while (expression) statement The statement gets executed repeatedly so long as the expression is true (IE not equal to 0)

Loops The statement can be a bunch of statements within begin-end braces. while (expression) { statement; … statement; }

Loops So increase I by 1 until it becomes greater than 255 i = 0; // Initialize i while (i < 256) // Done yet?? { statement;// here I = 0, then 1, then 2, … statement; … i = i + 1;// Increment i }

Loops So increase I by 1 until it becomes greater than 255 i = 0; // Initialize i while (i < 256) // Done yet?? { stroke (i);// set stroke colour to i statement; … i = i + 1;// Increment i }

Loops So increase I by 1 until it becomes greater than 255 i = 0; // Initialize i while (i < 256) // Done yet?? { stroke (i); // set stroke colour to i line (i, 0, i, 256);// Draw a line, color i … i = i + 1;// Increment i }

Loops void (draw) { i = 0; // Initialize i while (i < 256) // Done yet?? { stroke (i); // set stroke colour to i line (i, 0, i, 256);// Draw a line, color i i = i + 1;// Increment i }

Loops int i; // counters void setup () { size (256,256); } void draw () { i = 0; // Start at the first row. while (i<256) // Repeat for all 256 rows { stroke(i); line (i,0,i,255); i += 1; }

Loops – make it move New problem: Make the grey level change by one each step. It will now be an animation where the greys change – it will have the illusion of the image moving. This pointer will slide right from 0 to the end. Levels will be changed

Loops – make it move New problem: Make the grey level change by one each step. It will now be an animation where the greys change – it will have the illusion of the image moving. This pointer will slide right from 0 to the end. Levels will be changed ……

Loops – make it move New problem: Make the grey level change by one each step. It will now be an animation where the greys change – it will have the illusion of the image moving ……

Loops – make it move New problem: Make the grey level change by one each step. It will now be an animation where the greys change – it will have the illusion of the image moving ……

Loops – make it move New problem: Make the grey level change by one each step. It will now be an animation where the greys change – it will have the illusion of the image moving ……

Loops – make it move New problem: Make the grey level change by one each step. It will now be an animation where the greys change – it will have the illusion of the image moving ……

Loops – make it move New problem: Make the grey level change by one each step. It will now be an animation where the greys change – it will have the illusion of the image moving ……

Loops – make it move New problem: Make the grey level change by one each step. It will now be an animation where the greys change – it will have the illusion of the image moving ……

Loops – make it move New problem: Make the grey level change by one each step. It will now be an animation where the greys change – it will have the illusion of the image moving ……

Loops – make it move New problem: Make the grey level change by one each step. It will now be an animation where the greys change – it will have the illusion of the image moving ……

This can be done with a loop 1.Start the program with the left column = 0 and the right one =255(white). 2.Each step (draw) change the start value (increase by 1). 3.Need a variable for the start value. Call it v. Otherwise, the program is as it was.

This can be done with a loop 1.Start the program with the left column = 0 and the right one =255(white). 2.Each step (draw) change the start value (increase by 1). 3.Need a variable for the start value. Call it v. And one for the current colour (call it s) Otherwise, the program is as it was.

Modify the program int i; void setup () { size (256,256); } void draw () { i = 0; while (i<256) { stroke(i); line (i,0,i,255); i += 1; } int i,s,v; void setup () { size (256,256); v=0; } void draw () { i = 0; s = v; while (i<256) { stroke(s); line (i,0,i,300); i = i + 1; s = s + 1; } v = v + 1; }

Here’s what it does

A bug! We keep adding 1 to the color. At some point it gets bigger than 255, and then is useless – there’s no white that is whiter than 255. So after a while, all parts of the image become white, and don’t seem to change after that. We can fix that by checking the current color and not letting it get bigger than 255 – set it to 0 instead and start over.

A Bug fix We keep adding 1 to the color. At some point it gets bigger than 255, and then is useless – there’s no white that is whiter than 255. So after a while, all parts of the image become white, and don’t seem to change after that. We can fix that by checking the current color and not letting it get bigger than 255 – set it to 0 instead and start over.

A Bug fix

void draw () { i = 0; s = v; while (i<256) { stroke(s); if (s>255) s = 0; line (i,0,i,300); i = i + 1; s = s + 1; } v = v + 1; if (v>255) v = 0; } Check colour – make Sure it stays less than 255 The same for start colour, or else at some point, when v>255, all images look the same (all start at 0)

The FOR statment The WHILE statement creates a very general loop, and is all we really need. On the other hand … the most common type of loop involves adding 1 to a variable each time through. A FOR loop does this.

The FOR statement i = 0; while (i<256) { i = i + 1; } for (i=0; i<256; i=i+1) { }

The FOR statement void draw () { i = 0; s = v; for (i=0; i<256; i=i+1) { stroke(s); if (s>255) s = 0; line (i,0,i,300); s = s + 1; } v = v + 1; if (v>255) v = 0; } This is the same program as before, but using a FOR statement instead of a WHILE A FOR statement can always be turned into a WHILE in a simple way.

Loops Let’s try a new program. It’ll be artistic. Start at some point, say 20,20 Draw a line to another point; say 200,200 Now modify the endpoint and start point in a regular way: startx = startx + 1 starty = starty + 10 endx = endx – 1 endy = endy-12

Loops Let’s try a new program. It’ll be artistic. Start at some point, say 20,20 Draw a line to another point; say 200,200 Now modify the endpoint and start point in a regular way: startx = startx + 1 starty = starty + 10 endx = endx – 1 endy = endy-12

Loops int startx=20, starty=20; int endx = 200, endy=200; int sx1, sx2, sy1, sy2; void setup () { size (256,256); } void draw () { stroke (255); sx1 = startx; sy1 = starty; sx2 = endx; sy2=endy; while ((sy1 =0)) { line (sx1,sy1,sx2,sy2); sx1 = sx1 + 1; sy1 = sy1 + 10; sx2 = sx2 - 1; sy2 = sy2 - 12; }

36 Loops Now animate the colour = change from a start value through the range. Each time DRAW is called, change the colour a little bit. Need a variable for current colour, and need to check to see that it does not get bigger than 255.

37 Loops int startx=20, starty=20; int endx = 200, endy=200; int sx1, sx2, sy1, sy2; int s = 128; void setup () { size (256,256 } void draw () { stroke (s); sx1 = startx; sy1 = starty; sx2 = endx; sy2=endy; while ((sy1 =0)) { line (sx1,sy1,sx2,sy2); sx1 = sx1 + 1; sy1 = sy1 + 10; sx2 = sx2 - 1; sy2 = sy2 - 12; } s = s + 1; if (s > 255) s = 0; }

38 Loops

39 Loops That’s okay, but not great. How about changing the colour of each line each time. I’ll bet it won’t look like what you imagine. Change the line colour by 5 every time a line is drawn.

40 Loops int startx=20, starty=20; int endx = 200, endy=200; int sx1, sx2, sy1, sy2; int s = 128; void setup () { size (256,256); } void draw () { sx1 = startx; sy1 = starty; sx2 = endx; sy2=endy; while ((sy1 =0)) { stroke (s); line (sx1,sy1,sx2,sy2); sx1 = sx1 + 1; sy1 = sy1 + 10; sx2 = sx2 - 1; sy2 = sy2 - 12; s = s + 5; if (s > 255) s = 0; }

41 ??

42 Loops Most of our programs will use loops. Practice with simple problems Try new things Use the print statements for easy to understand output.

43 Printing … void draw () { sx1 = startx; sy1 = starty; sx2 = endx; sy2=endy; while ((sy1 =0)) { stroke (s); line (sx1,sy1,sx2,sy2); sx1 = sx1 + 1; sy1 = sy1 + 10; sx2 = sx2 - 1; sy2 = sy2 - 12; s = s + 5; if (s > 255) s = 0; } n = n + 1; print ("Iteration "); print(n); print (" Color "); println(s); }

44 Printing …