review n parameterizing a shape n have a default size
play

+ + Review n Parameterizing a shape n have a default size frame for - PDF document

2/10/16 + + Review n Parameterizing a shape n have a default size frame for your shape to fit in. n alter position relative to the reference point and scale n alter size relative to scale. n instead of scale n use a scaled


  1. 2/10/16 ¡ + + Review n Parameterizing a shape n have a default size frame for your shape to fit in. n alter position relative to the reference point and scale n alter size relative to scale. n instead of scale n use a scaled reference size frame n Functions and named constants improve readability, reusability, and scalability of your code. Scope, Tracing, and Basic Trigonometry n Variable Lifetime and Scope n global variables int v1 = 1; Shadowing ¡ n What ¡is ¡printed? ¡ void setup() { int v2 = 2; n When ¡there ¡is ¡a ¡name ¡conflict ¡between ¡variables ¡of ¡ n What ¡happens ¡if ¡the ¡second ¡v3 ¡ for (int v3=3; v3 <= 3; v3++) { different ¡scopes ¡ int v4 = 4; declara?on ¡is ¡removed? ¡ println("------setup------"); println(v1); int x = 10; println(v2); n What ¡would ¡happen ¡if ¡the ¡v5 ¡ void setup() { println(v3); println(v4); print ¡statement ¡is ¡executed? ¡ int x = 5; //println(v5); int y = x; } n What ¡would ¡happen ¡if ¡ } int v3 = 6; commented ¡statements ¡in ¡ println(v3); n The ¡conflic?ng ¡variables ¡can ¡not ¡have ¡different ¡types ¡ aFunc?on ¡were ¡called? ¡ aFunction(v2); (or ¡it's ¡considered ¡a ¡re-­‑declara?on ¡and ¡is ¡not ¡ } allowed) ¡ void aFunction(int v5) { println("------aFunction------"); println(v1); n When ¡shadowed, ¡smaller ¡(inner) ¡scopes ¡have ¡ //println(v2); //println(v3); precedence ¡over ¡larger ¡(outer) ¡scopes ¡ //println(v4); println(v5); } + Example ¡ + Code ¡tracing ¡ n scopeLines ¡ n We ¡learn ¡to ¡read ¡code ¡by ¡execu?ng ¡the ¡code ¡line ¡by ¡line ¡ n Do ¡not ¡jump ¡ahead ¡ n Do ¡exactly ¡what ¡the ¡code ¡says, ¡step ¡by ¡step ¡ n Keep ¡a ¡diagram ¡of ¡all ¡variables ¡and ¡update ¡them ¡ accordingly ¡ n Mistakes ¡are ¡almost ¡always ¡due ¡to ¡skipping ¡steps ¡ 1 ¡

  2. 2/10/16 ¡ + Trace ¡this ¡ + Nested ¡loops ¡ 1 int n = 365; n for(...){ n You ¡can ¡put ¡a ¡loop ¡within ¡a ¡ for(...){ 2 int sum = 0; loop ¡ } 3 int digit; n Nes?ng ¡levels ¡are ¡unlimited, ¡ } but ¡in ¡prac?ce ¡programmers ¡ n while(...){ while(...){ rarely ¡go ¡beyond ¡3 ¡ 4 while(n>0) { } n Two ¡loops ¡nested ¡is ¡very ¡ 5 digit = n%10; } common, ¡especially ¡when ¡ n for(...){ 6 sum += digit; dealing ¡with ¡naturally ¡2-­‑ while(...){ 7 n /= 10; dimensional ¡structures ¡(grids) ¡ } 8 } } n while(...){ for(...){ 9 println(sum); } } 7 ¡ + Nested ¡ for ¡ + Nested ¡ for ¡ int i, j, end = 10; int i, j, end = 10; for (i = 1; i <= end; i++) { for (i = 1; i <= end; i++) { for (j = i; j <= end; j++) { for (j = 1; j <= i; j++) { print("*"); print("*"); } } println(); println(); } } 9 ¡ 10 ¡ + Examples ¡ + Basics ¡of ¡Trigonometry ¡assuming ¡ ¡ right/up ¡axes ¡ n indexTile ¡(one ¡loop) ¡ h n indexTile ¡(loop ¡with ¡nested ¡Loop) ¡ (hypotenuse) o (opposite) q a (adjacent) 2 ¡

  3. � � � 2/10/16 ¡ + Basics ¡of ¡Trigonometry ¡assuming ¡ ¡ + Basics ¡of ¡Trigonometry ¡assuming ¡ ¡ right/up ¡axes ¡ right/up ¡axes ¡ h h (hypotenuse) (hypotenuse) o o = h * sin(q) (opposite) (opposite) Recall: Recall: a^2 + o^2 = h^2 a^2 + o^2 = h^2 sin (q) = o/h q q a a = h * cos(q) cos (q) = a/h (adjacent) (adjacent) + Defini?on ¡ + Trigonometry ¡on ¡a ¡unit ¡circle ¡ 90 ° n sin (q) = o/h Recall: � x^2 + y^2 = r^2 � n o = h*sin (q) p r = 1 � (1*cos(q))^2 + � r (1*sin(q))^2 � = 1^2 � y n cos (q) = a/h or � q 0 ° n a = h*cos (q) x origin cos^2(q) + � sin^2(q) = � 1 � n tangent (q) = o/a = sin (q)/ cos (q) + Trigonometry ¡on ¡Processing ¡unit ¡circle ¡ + Trigonometry ¡on ¡a ¡unit ¡circle ¡ 90 ° p r origin x 0 ° q q 0 ° y r p 90 ° 3 ¡

  4. 2/10/16 ¡ + Trigonometry ¡on ¡a ¡unit ¡circle ¡ Drawing ¡points ¡along ¡a ¡circle ¡ ¡ 90 ° int steps = 8; int radius = 20; float angle = 2*PI/steps; for (int i=0; i<steps; i++) { float x = cos(angle*i)*radius; float y = sin(angle*i)*radius; q 0 ° // draw a point every 1/8th of a circle ellipse(x, y, 10, 10); } ¡ + Examples + Example: cyclical change n points on a circle n Drawing a sine wave n overlapping ellipses on a circle n Using sine to manipulate height of an object n spokes n Using cosine to manipulate width of an object n polygon n nested version (star) 4 ¡

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