Académique Documents
Professionnel Documents
Culture Documents
Medium
Lab 1: If, For, Draw, Setup and
Coffee
Joshua Cuneo
What will we go over?
B i administrative
Boring d i i i stuffff
Variables and drawing primitives
Instruction flow types, i.e. for, if...
A little style
“draw()” looping function for animation
S
Simple l mouse interaction
Stop me if you haven’t covered this yet!!!
Anyone Can Program
;#q#n+,/+k#;
;#q#n+ /+k#;*+ +,//'rr :
q#'+d'K#!/+k#;q#'r}eKK
){nl]!/n{n#'; r{#w'r nc{nl
nw' \ iwk{KK{nl]!/w{%'l
:{nl]'/*{q#'ld;r'}{nlwb!/*
-{}rw]'/+,}##'*}#nc,',#
=
How This Lab Works
9:00 11:00
Objects:
Built on primitives
Ex. String – sequence of characters
More
Mo e info on drawing
d a ing primitives
p imiti es
RTFM!
http://www.processing.org/reference/index.ht
p // p g g/ /
ml
Function descriptions can be really helpful once you
learn how to read them...
Instruction Flow 1: Linear
Do this
Th
Then do
d this
thi
Then do this
Then do this
etc....
Inst ction Flow
Instruction Flo 2: Conditional
if something is tr
true
e
do this
otherwise, if something else is true
do this
otherwise,
do this
Inst ction Flow
Instruction Flo 2: Conditional
int power = 0;
if(power
(p > 10)
)
{
fire_laser();
}
else
l if(i > 0) // h t if this
//what thi was j
just
t if?
{
charge_laser();
}
else
{
turn_on_laser();
}
Inst ction Flow
Instruction Flo 3:
3 Looping
Wh t if you had
What h d 1000 lines
li to
t draw?
d ?
Inst ction Flow
Instruction Flo 3: Looping
Ch k the
Checks th condition
diti before
b f each
h repetition
titi off the
th code.
d
While Loops
Be careful of infinite loops
= BAD
BAD*
*Most of the time
For
Fo Loops
These two loops are functionally the same:
(i += 10 is equivalent to i = i + 10)
N t d LLoops
Nested
This means that the inside loop will run all the way
through,
g , then i will increment,, and the inside loop
p will
run again.
N t d LLoops
Nested
i
for(int i = 10; i <= 100; i += 10)
{
for(int j = 10; j <= 100; j += 10)
{
point(i, j); j
}
}
N t d LLoops
Nested
i
for(int i = 10; i <= 100; i += 10)
{
for(int j = 10; j <= 100; j += 10)
{
point(i, j); j
}
}
N t d LLoops
Nested
for(int
( i = 10;
; i <= 100;
; i += 10)
) i
{
for(int j = 10; j <= 100; j += 10)
{
point(i j);
point(i,
}
j
}
i’s 10, j’s 10. Draws a point at (10, 10). Simple enough.
N t d LLoops
Nested
Don't go overboard
this_is_my_really_cool_function_and_yo
ure an idiot if you dont like it()
ure_an_idiot_if_you_dont_like_it()
A Few More Words About Style
Formatting
Put curly brackets on their own line
Indent interior blocks of code
C
Comment t your code
d
This makes your code easier to read and follow
float a = 200;
if (a < 0) {
g ;
a = height;
}
line(0, a, width, a);
}
The d
draw()
a () function
f nction
Simple example:
float a = 200;
void draw()
{
background(255);
2
a = a - 1;
if (a < 0) {
a = height;
}
line(0, a, width, a);
}
The d
draw()
a () function
f nction
Simple example:
fl
float a = 200;
200
Important! The variable declaration
void draw() must be outside the loop function, or
{
else it will be reset everyy time draw()
() is
background(255);
2
a = a - 1;
called.
if (a < 0) {
a = height; (Try putting it inside draw() to see what
} happens)
line(0, a, width, a);
}
The d
draw()
a () function
f nction
Simple example:
float a = 200;
Simple example:
float a = 200;
void setup()
{
size(640, 480);
framerate(30);
}
Tracking
T acking Mouse
Mo se Position
Questions?