Date post: | 14-Dec-2015 |
Category: |
Documents |
Upload: | courtney-carter |
View: | 223 times |
Download: | 1 times |
Programming for Programming for ArtistsArtists
ART 315ART 315
Dr. J. R. ParkerDr. J. R. Parker
Art/Digital Media LabArt/Digital Media Lab
Lec 13 Fall 2010Lec 13 Fall 2010
Functions – last time we looked at how to define a function of our own.
A function is a module, a small section of code that computes something useful in the context of our problem. It may take parameters, can define its own variables, and can be called (invoked) from anywhere in the program.
What now?
Functions should have a single well-defined thing that they do.
Built-in functions are a good example:abs()ceil()constrain()dist()exp()floor()lerp()log()mag()map()max()
min()norm()pow()round()sq()sqrt()Trigonometryacos()asin()atan()atan2()cos()
degrees()radians()sin()tan()Randomnoise()random()
All of these do one thing
4
Function: max
Description: Determines the largest value in a sequence of numbers.
max(value1, value2, value 3)
Let’s write a max function for floats
5
Function: max
float mymax (float a, float b, float c)
Returns an integer, which willbe one of the three values passedto it.
Name of thefunction
Three parameters. This function finds the largestone and returns that asa value.
6
Function: max
float mymax (float a, float b, float c) { if ( (a>=b) && (a>=c) ) return a; if ( (b>=c) && (b>=a) ) return b; return c;}
RETURN means use this value as the value of this function this time and go back to where it was called.
7
Function: max
How do we know that it works?
Well, lets give it some numbers and print out the answer.
8
Function: max
Let’s give it some numbers and print out the answer.
void draw ( ){ float a=21, b=63, c=12; print ("Max of "); print (a); print (" "); print(b); print (" "); print (c); print (" is "); println (mymax(a, b, c));}
float mymax (float a, float b, float c){ if ( (a>=b) && (a>=c) ) return a; if ( (b>=c) && (b>=a) ) return b; return c;}
10
Function: maxLet’s give it some numbers and print
out the answer.void draw ( ){ float a=21, b=63, c=12; print ("Max of "); a = random (-100, 100); print (a); print (" "); b = random(-100, 100); print(b); print (" "); c = random(-100, 100); print (c); print (" is "); println (mymax(a, b, c));}
float mymax (float a, float b, float c){ if ( (a>=b) && (a>=c) ) return a; if ( (b>=c) && (b>=a) ) return b; return c;}
12
Function: distance
float distance (float x1, float y1, float x2, flat y2) { float d=0.0; d = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2); d = sqrt (d); return d;}
Calculates the distance between point (X1,Y1) and the point (X2,Y2). It calculates the hypotenuse of the right triangle.
x1,y1
x2,y2
void draw ( ){ float x1,x2,y1,y2; print ("Distance from ("); x1 = random (0, 100); y1=random(0,100); print (x1); print (","); y1 = random(0, 100); print(y1); print (") to ("); x2 = random (0, 100); y2=random(0,100); print (x2); print (","); print(y2); print (" is "); println (distance(x1,y1,x2,y2));}
float distance (float x1, float y1, float x2, float y2) { float d=0.0; d = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2); d = sqrt (d); return d;}
15
Functions
VOID functions do not return a single value, but do perform calculations that are needed for the overall program.
Changes are made to global variables or to parameters.
Allows us to organize the program into smaller units.
A basic unit
void drawFraction(int gray,int x1,int y1,int x2,int y2,int x3, int y3, int x4, int y4){ stroke(gray); quad(x1,y1,x2,y2,x3,y3,x4,y4);}
20
Functions//creating block method through initialising multiple drawFraction methods.
void block(int var,int xpos,int ypos)
{
drawFraction(gray+var,100+xpos,100+ypos,300+xpos,100+ypos,300+xpos,300+ypos,100+xpos,300+ypos);
drawFraction(gray+var,90+xpos,110+ypos,290+xpos,110+ypos,310+xpos,290+ypos,110+xpos,290+ypos);
drawFraction(gray+var,80+xpos,120+ypos,280+xpos,120+ypos,320+xpos,280+ypos,120+xpos,280+ypos);
drawFraction(gray+var,70+xpos,130+ypos,270+xpos,130+ypos,330+xpos,270+ypos,130+xpos,270+ypos);
drawFraction(gray+var,60+xpos,140+ypos,260+xpos,140+ypos,340+xpos,260+ypos,140+xpos,260+ypos);
drawFraction(gray+var,50+xpos,150+ypos,250+xpos,150+ypos,350+xpos,250+ypos,150+xpos,250+ypos);
drawFraction(gray+var,40+xpos,160+ypos,240+xpos,160+ypos,360+xpos,240+ypos,160+xpos,240+ypos);
drawFraction(gray+var,30+xpos,170+ypos,230+xpos,170+ypos,370+xpos,230+ypos,170+xpos,230+ypos);
drawFraction(gray+var,20+xpos,180+ypos,220+xpos,180+ypos,380+xpos,220+ypos,180+xpos,220+ypos);
drawFraction(gray+var,10+xpos,190+ypos,210+xpos,190+ypos,390+xpos,210+ypos,190+xpos,210+ypos);
drawFraction(gray+var,0+xpos,200+ypos,400+xpos,200+ypos,200+xpos,200+ypos,200+xpos,200+ypos);
drawFraction(gray+var,190+xpos,190+ypos,390+xpos,190+ypos,210+xpos,210+ypos,10+xpos,210+ypos);
drawFraction(gray+var,180+xpos,180+ypos,380+xpos,180+ypos,220+xpos,220+ypos,20+xpos,220+ypos);
drawFraction(gray+var,170+xpos,170+ypos,370+xpos,170+ypos,230+xpos,230+ypos,30+xpos,230+ypos);
drawFraction(gray+var,160+xpos,160+ypos,360+xpos,160+ypos,240+xpos,240+ypos,40+xpos,240+ypos);
drawFraction(gray+var,150+xpos,150+ypos,350+xpos,150+ypos,250+xpos,250+ypos,50+xpos,250+ypos);
drawFraction(gray+var,140+xpos,140+ypos,340+xpos,140+ypos,260+xpos,260+ypos,60+xpos,260+ypos);
drawFraction(gray+var,130+xpos,130+ypos,330+xpos,130+ypos,270+xpos,270+ypos,70+xpos,270+ypos);
drawFraction(gray+var,120+xpos,120+ypos,320+xpos,120+ypos,280+xpos,280+ypos,80+xpos,280+ypos);
drawFraction(gray+var,110+xpos,110+ypos,310+xpos,110+ypos,290+xpos,290+ypos,90+xpos,290+ypos);
}
21
FunctionsDetails of this particular function are less important
than the overall lesson:
That a complex drawing task can be broken into simpler, logical units using functions.
The parameters of the function can be used to draw, for example, repeated versions of a simple shape in many positions/colors over the image/canvas.
22
FunctionsNew one (probably simpler)
Let’s write a function that draws a black circle.
// Black circle.void blackCircle ( int x1, int y1 ){ stroke(0); fill (0); ellipseMode(CENTER); ellipse (x1,y1,circleSize, circleSize);} void whiteCircle ( int x1, int y1 )
{ stroke(0); fill (255); ellipseMode(CENTER); ellipse (x1,y1,circleSize, circleSize);}
And a white one.
23
FunctionsNow let’s draw a checkerboard (getting the idea??)
void board (){ int i,j; for (i=0; i<8; i++) { for (j=0; j<8; j++) { if (k==0) fill (139,69,19); else fill (139, 115, 85); k = 1-k; rect (40+i*30, 40+j*30, 30, 30); } k = 1-k; }}
24
FunctionsNow let’s draw a checkerboard (getting the idea??)
Squares are at 30 unitDistances starting at (40,40).
Now let’s put checkers on the board.White ones on top 3 rows, dark squares.
25
FunctionsNow let’s draw a checkerboard (getting the idea??)
void placeWhite (){ int i=0; for (i=0; i<4; i++) whiteCircle (70+i*60, 40); for (i=0; i<4; i++) whiteCircle (40+i*60, 70); for (i=0; i<4; i++) whiteCircle (70+i*60, 100);}
void placeBlack (){ int i=0; for (i=0; i<4; i++) blackCircle (40+i*60, 190); for (i=0; i<4; i++) blackCircle (70+i*60, 220); for (i=0; i<4; i++) blackCircle (40+i*60, 250);}