2 8 16
play

2/8/16 Odds and Ends Review Please submit any images - PDF document

2/8/16 Odds and Ends Review Please submit any images files you used along with Variable declaraJons your program Variable assignments Name


  1. 2/8/16 Odds ¡and ¡Ends ¡ Review ¡ • Please ¡submit ¡any ¡images ¡files ¡you ¡used ¡along ¡with ¡ • Variable ¡declaraJons ¡ ¡ your ¡program ¡ • Variable ¡assignments ¡ • Name ¡your ¡screenshot ¡something ¡very ¡obvious ¡– ¡like ¡ • Loops ¡ “screenshot.jpg” ¡ – CondiJon ¡ • Do ¡not ¡leave ¡any ¡files ¡sca@ered ¡in ¡your ¡Dropbox ¡ – index ¡ folder. ¡It ¡needs ¡to ¡be ¡in ¡an ¡assignment ¡folder ¡or ¡I ¡ • FuncJons ¡ won’t ¡know ¡which ¡assignment ¡it ¡belongs ¡to! ¡ – DefiniJon ¡ • Name ¡all ¡your ¡assignment ¡folders ¡well, ¡like ¡ – Call ¡ assignment01, ¡sketch01, ¡etc ¡ – Parameters ¡ Execu0on ¡ Parameterizing ¡a ¡shape ¡ • Statements ¡are ¡executed ¡one ¡at ¡a ¡Jme ¡in ¡the ¡ • Have ¡code ¡that ¡draws ¡something ¡with ¡a ¡bunch ¡of ¡ coordinates ¡ order ¡wri@en ¡ • Want ¡to ¡draw ¡the ¡same ¡thing ¡anywhere, ¡in ¡any ¡size ¡ and ¡repeat ¡any ¡number ¡of ¡Jmes ¡ • ExecuJon ¡order ¡ ¡ • How ¡is ¡a ¡shape ¡defined? ¡ – Globals ¡and ¡iniJalizaJons ¡ – a ¡reference ¡point ¡(center, ¡corner) ¡ – setup() ¡called ¡once ¡ – a ¡base ¡size ¡ – draw() ¡called ¡repeatedly ¡(unless ¡ noLoop() ¡is ¡called ¡in ¡ • To ¡move, ¡scale ¡and ¡repeat ¡ setup() ) ¡ – put ¡code ¡in ¡a ¡funcJon ¡ – If ¡any ¡mouse ¡or ¡keyboard ¡events ¡occur, ¡the ¡corresponding ¡ – x ¡and ¡y ¡increments ¡ funcJons ¡are ¡called ¡between ¡calls ¡to ¡ draw() ¡– ¡exact ¡Jming ¡ can ¡not ¡be ¡guaranteed. ¡ – scaling ¡factor ¡ Example: ¡any ¡size ¡and ¡place ¡door ¡ Let's ¡design ¡the ¡door ¡ ¡ • A ¡door ¡has ¡ • FuncJon ¡name? ¡ – a ¡plank ¡ – parameters ¡ – a ¡handle ¡ • a ¡plank ¡ – a ¡window ¡ – what ¡is ¡the ¡reference ¡point? ¡ – hinges ¡ • a ¡handle, ¡etc… ¡ – a ¡frame ¡ – what ¡is ¡it's ¡locaJon ¡relaJve ¡to? ¡ • How ¡do ¡you ¡move ¡all ¡parts ¡together? ¡ – what ¡about ¡its ¡size? ¡ • When ¡size ¡changes: ¡ – how ¡do ¡you ¡keep ¡parts ¡in ¡same ¡relaJve ¡locaJons? ¡ – What ¡happens ¡when ¡aspect ¡raJo ¡of ¡sketch ¡changes? ¡ 1

  2. 2/8/16 Iden0fy ¡Similar ¡Code ¡ manyShapesFunc0on2 ¡ void drawRandomShape(int choice) float x, y, w, h; { int totalShapeCount = 1000; void drawRandomRect() { int MAX_COL = 255, WHITE = 255; x = random(width); fill(random(255), random(255), random(255), int TRANSLUCENT = 50; y = random(height); 50); int BLACK = 0; w = random(MIN_D, MAX_D); x = random(width); int RECT_CHOICE = 1; h = random(MIN_D, MAX_D); y = random(height); int ELLIPSE_CHOICE = 2; Similar if(choice == ELLIPSE_CHOICE) int MIN_D = 5; int MAX_D = 100); w = random(5, 100); { // circle unit h = random(5, 100); fill(random(WHITE), void setup () { rect(x, y, w, h); int i = 0; TRANSLUCENT); } // other setup code here … ellipse(x, y, w, h); } stroke(WHITE, TRANSLUCENT); } while (i<totalShapeCount) { else { // RECT_CHOICE void drawRandomCircle() { drawRandomShape(RECT_CHOICE); fill(random(MAX_COL), fill(random(255), 50); i += 1; random(MAX_COL), } x = random(width); random(MAX_COL), stroke(BLACK, TRANSLUCENT); y = random(height); TRANSLUCENT); Similar for (i=0; i<totalShapeCount; w = random(5, 100); rect(x, y, w, h); i++) { h = random(5, 100); unit drawRandomShape(ELLIPSE_CHOICE); } ellipse(x, y, w, h); } } } } 7 ¡ 8 ¡ Func0ons ¡that ¡return ¡values ¡ Example ¡ • The ¡return ¡value ¡of ¡a ¡funcJon ¡is ¡the ¡output ¡of ¡a ¡ • What ¡is ¡the ¡value ¡of ¡ void setup () { int result; funcJon. ¡ result ¡in ¡each ¡line? ¡ result = A(2); result = B(1, 2); result = 10 + A(2); • A ¡funcJon ¡evaluates ¡to ¡its ¡return ¡value. ¡ result = A(2) + B(1, 2); result = B(A(2), B(B(1, 2), A(2))); • FuncJon ¡must ¡return ¡a ¡value ¡whose ¡type ¡matches ¡the ¡ } funcJon ¡declaraJon. ¡ int A(int x) { return x*2; ¡ } int B(int x, int y) { return_type function_name ( parameter_list ) { return x+y; } statements ; return value ; } Variable ¡Life0me ¡ Variable ¡Scope ¡ – Variables ¡cannot ¡be ¡referenced ¡before ¡they ¡are ¡ • The ¡region ¡of ¡code ¡in ¡which ¡a ¡parJcular ¡variable ¡ declared. ¡ is ¡accessible. ¡ • A ¡variable ¡is ¡created ¡and ¡iniJalized ¡when ¡a ¡ • To ¡a ¡first ¡approximaJon, ¡the ¡scope ¡of ¡a ¡secJon ¡ program ¡enters ¡the ¡block ¡in ¡which ¡it ¡is ¡declared. ¡ of ¡your ¡code ¡is ¡demarcated ¡by ¡{ ¡and ¡}. ¡ – FuncJons ¡ – FuncJons ¡ – Loops ¡ – Loops ¡ – CondiJonals ¡ – CondiJonals ¡ – FuncJon ¡parameters ¡ • A ¡variable ¡is ¡only ¡accessible/available ¡within ¡the ¡ • A ¡variable ¡is ¡destroyed ¡when ¡a ¡program ¡exists ¡ scope ¡in ¡which ¡it ¡is ¡declared. ¡ the ¡block ¡in ¡which ¡it ¡was ¡declared. ¡ 2

  3. 2/8/16 Global ¡variables ¡ Shadowing ¡ • Variables ¡that ¡are ¡declared ¡outside ¡of ¡any ¡scope ¡ • When ¡there ¡is ¡a ¡name ¡conflict ¡between ¡variables ¡ are ¡considered ¡globals ¡(versus ¡locals). ¡ of ¡different ¡scopes ¡ • Global ¡variables ¡should ¡be ¡declared ¡at ¡the ¡top ¡of ¡ int x = 10; void setup() { your ¡program. ¡ int x = 5; int y = x; • Do ¡not ¡sprinkle ¡them ¡between ¡funcJons! ¡ } • The ¡conflicJng ¡variables ¡can ¡not ¡have ¡different ¡ types ¡(or ¡it ’ s ¡considered ¡a ¡re-­‑declaraJon ¡and ¡is ¡ not ¡allowed) ¡ • When ¡shadowed, ¡smaller ¡(inner) ¡scopes ¡have ¡ precedence ¡over ¡larger ¡(outer) ¡scopes ¡ 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend