Computation as an Expressive Computation as an Expressive MediumMediumMediumMediumLab 1: If, For, Draw, Setup andLab 1: If, For, Draw, Setup andCoffeeCoffeeJoshua CuneoJoshua CuneoWhat will we go over?What will we go over?What will we go over?What will we go over?Bi dii i ffBi dii i ffBoring administrative stuff Boring administrative stuff Variables and drawing primitivesVariables and drawing primitives Instruction flow types, i.e. for, if...Instruction flow types, i.e. for, if...A little styleA little styleA little styleA little style “draw()” looping function for animation“draw()” looping function for animationSlSlSimple mouse interactionSimple mouse interaction Stop me if you haven’t covered this yet!!!Stop me if you haven’t covered this yet!!!Anyone Can ProgramAnyone Can ProgramAnyone Can ProgramAnyone Can Program Just writing instructionsJust writing instructionsI’ll make this as fun as I canI’ll make this as fun as I canIll make this as fun as I canIll make this as fun as I can Stop me if you get confused!Stop me if you get confused!Why Do We Care?Why Do We Care?Why Do We Care?Why Do We Care?Why Do We Care?Why Do We Care?Why Do We Care?Why Do We Care?;#q#n+ /+k#;*+/'r:;#q#n+,/+k#; +,/r :q#'+d'K#!/+k#;q#'r}eKK){nl]!/n{n#'; r{#w'r nc{nlnw' \ iwk{KK{nl]!/w{%'l:{nl]'/*{q#'ld;r'}{nlwb!/*-{}rw]'/+,}##'*}#nc,',#=How This Lab WorksHow This Lab WorksHow This Lab WorksHow This Lab Works9:00 11:00Snarky Waggle-Head Lecture WorkshopTA Office Ho sTA Office Ho sTA Office HoursTA Office HoursTT1122::3030WW1133DM LabDM LabT T 11--22::3030, W , W 11--33, DM Lab., DM Lab. + Some Fridays+ Some FridaysNOT S tNOT S t44NOT Sept. NOT Sept. 44 [email protected]@gatech.eduWhat have you gone over?What have you gone over?ygyg Window size and coordinate systemWindow size and coordinate system(0,0) x Commenting codeCommenting code VariablesVariables Drawing primitivesDrawing primitives pointpointlinelineylineline rectrect ellipseellipsepp TriangleTriangle if if StatemenetsStatemenets What else?What else?Va iablesVa iablesVariablesVariables Variables are placeholders for your dataVariables are placeholders for your dataRemember middle school algebra?Remember middle school algebra?Remember middle school algebra?Remember middle school algebra?int i; // declares a new variable ii = 1; // assigns the value of 1 to the new // variableint j = i + 1; // declares a new variable j, and// assigns it to i +1which is2// assigns it to i + 1, which is 2.Va iable T pesVa iable T pesVariable TypesVariable Types Primitives:Primitives: booleanboolean, c, charhar, byte, short, , byte, short, iintnt, long, f, long, floatloat, double, double Objects:Objects: Built on primitives Built on primitives Ex. String Ex. String –– sequence of characterssequence of charactersMo e info on d a ing p imiti esMo e info on d a ing p imiti esMore info on drawing primitivesMore info on drawing primitives RTFM!RTFM! http://www.processing.org/reference/index.hthttp://www.processing.org/reference/index.htp// p g g/ /p// p g g/ /mlml Function descriptions can be really helpful once you Function descriptions can be really helpful once you learn how to read them...learn how to read them...Instruction FlowInstruction Flow11:Linear:LinearInstruction Flow Instruction Flow 11: Linear: Linear Most common flow typeMost common flow typeDo thisTh d thiThen do thisThen do thisThen do thisetc....Inst ction FloInst ction Flo22ConditionalConditionalInstruction Flow Instruction Flow 22: Conditional: Conditionalif something is tr eif something is truedo thisotherwise, if something else is trueotherwise, if something else is truedo thisotherwise,do thisInst ction FloInst ction Flo22ConditionalConditionalInstruction Flow Instruction Flow 22: Conditional: Conditionalint power = 0;if(power > 10) (p){fire_laser();}lif(i>0)// h t if thi j t if?else if(i > 0) //what if this was just if?{charge_laser();}else {turn_on_laser();}}Inst ction Flo 3 LoopingInst ction Flo 3 LoopingInstruction Flow 3: LoopingInstruction Flow 3: Loopingwhile something is truedo this repeatedly until that pysomething is falseInst ction Flo 3 LoopingInst ction Flo 3 LoopingInstruction Flow 3: LoopingInstruction Flow 3: Looping Want to draw five lines, evenlyWant to draw five lines, evenly--spaced spaced apart?apart?ppline(10, 10, 10, 200);line(20, 10, 20, 200);line(30, 10, 30, 200);...Wh t if h d1000li t d ?What if you had 1000 lines to draw?Inst ction FloInst ction Flo33LoopingLoopingInstruction Flow Instruction Flow 33: Looping: Looping Want to draw five lines, evenlyWant to draw five lines, evenly--spaced spaced apart?apart?What if we could run thisppline(10, 10, 10, 200);line(20, 10, 20, 200);What if we could run this same line of code multiple times, only changing these two numbers?line(30, 10, 30, 200);...While LoopsWhile LoopsWhile LoopsWhile Loopsline(10, 10, 10, 200);line(20, 10, 20, 200);3030int i = 10;line(30, 10, 30, 200);line(40, 10, 40, 200);line(50, 10, 50, 200);line(60, 10, 60, 200);while(i <= 100) {line(i, 10, i, 200);i=i+10;vs(,,,);line(70, 10, 70, 200);line(80, 10, 80, 200);line(90, 10, 90, 200);li (10010100200)i = i + 10;}line(100, 10, 100, 200);Ch k th diti b f h titi f th dChecks the condition before each repetition of the code.While LoopsWhile LoopsWhile LoopsWhile Loops Be careful of infinite loopsBe careful of infinite loopswhile(true) {...}int i = 10;while(i > 0) {}{line(i, 10, i, 200);i = i + 10;}= BAD*= BAD*Most of the timeFo LoopsFo LoopsFor LoopsFor Loops These two loops are functionally the same:These two loops are functionally the same:int i = 10; for(int i = 10; i <= 100; i += 10) {while(i <= 100) {line(i, 10, i, 200);i+=10;line(i, 10, i, 200);}i +10;}(i += 10 is equivalent to i = i + 10)NtdLNtdLNested LoopsNested Loopsfor(int i = 10; i <= 100; i += 10) {{for(int j = 10; j <= 100; j += 10) {point(i, j);}} This means that the inside loop will run all the way through, then iwill increment, and the inside loop will g,,prun again.NtdLNtdLNested LoopsNested LoopsiSets i to 10. That’s all for now.for(int i = 10; i <= 100; i += 10) {for(int j = 10; j <= 100; j += 10) i{point(i, j);}}j}NtdLNtdLNested LoopsNested LoopsiSets j to 10. That’s all for
View Full Document