Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 13 Fall 2010.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

Arithmetic Calculations
User Defined Functions
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Methods Java 5.1 A quick overview of methods
An Introduction to Verilog-A: Transitioning from Verilog
Perkovic, Chapter 7 Functions revisited Python Namespaces
 Functions breakdown: Functions purpose Modularity Declaring and defining a function Calling a function Parameter passing Returning a value Reusability.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Modular Programming With Functions
Building Java Programs
1 MATH METHODS THAT RETURN VALUES. 2 JAVA'S MATH CLASS.
Code Elements and Processing Coordinate System. Code elements: pages Comments: are documentation notes that are ignored by the computer but are.
© Calvin College, Being abstract is something profoundly different from being vague... The purpose of abstraction is not to be vague, but to create.
Return values.
Excel Part I Basics and Simple Plotting Section 008 Fall 2013 EGR 105 Foundations of Engineering I.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 24 / 2010 Instructor: Michael Eckmann.
Lesson Three: Organization
EGR 105 Foundations of Engineering I Session 3 Excel – Basics through Graphing Fall 2008.
1 Python Chapter 2 © Samuel Marateck, After you install the compiler, an icon labeled IDLE (Python GUI) will appear on the screen. If you click.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
Microsoft® Small Basic The Math Object Estimated time to complete this lesson: 1 hour.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
Python  By: Ben Blake, Andrew Dzambo, Paul Flanagan.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Programming Training Main Points: - Python Statements - Problems with selections.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
* * 0 Chapter 6 Java Methods. * * 0 Method Syntax [access specifier][qualifier] return type method name(argument list) Access specifier public – method.
Variables and Arithmetic. From last class More drawing functions: strokeWeight(n); // higher the n value broader the stroke fill(k) ; // single parameter.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 11 Fall 2010.
1 Math Expressions and Operators. 2 Some C++ Operators Precedence OperatorDescription Higher ( )Function call +Positive - Negative *Multiplication / Division.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Functions. Functions are named blocks of code. Functions allow complex programs to be broken down into smaller, simpler tasks. Functions allow commonly.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
Calculations Chapter 11 Library of math functions, and constants cos, sin, tan, abs, min, max, log, random, sqrt, pow, exp Constants.PI,.E Use care with.
Review Loops – Condition – Index Functions – Definition – Call – Parameters – Return value.
Functions & Libraries. Introduction Functions – Modules of code Uses – Abstraction Build complex tasks with simpler ones Don't worry about details at.
Section 6.1 Notes Special Angles of the Unit Circle in degrees and radians.
Getting Started with MATLAB (part 3) 1. Algebra, 2. Trig 3. The keyword ans 4. Clean up and suppress output: finalizing the software’s presentation 1.
Elementary C++. Procedural Programming Split your problem into simpler parts then solve each part separately Recognize common parts and solve them only.
PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Functional Abstraction Reduces Complexity.
Review Expressions and operators Iteration – while-loop – for-loop.
Let’s Learn 3. Modules Saenthong School, January – February 2016
Matlab Tutorial Iman Moazzen First Session – September 11, 2013.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A = PI * radius ** 2 return A def circumference(diameter):
CS 106 Introduction to Computer Science I 10 / 10 / 2007 Instructor: Michael Eckmann.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
Functions. 2 Modularity What is a function? A named block of code Sometimes called a ‘module’, ‘method’ or a ‘procedure’ Some examples that you know are:
CSE 110: Programming Language I Afroza Sultana UB 1230.
Some Useful MATLAB Functions
Chapter 9: Value-Returning Functions
Chapter 6 - Functions modular programming general function format
Lesson 5 Functions I A function is a small program which accomplishes a specific task. For example, we invoke (call) the function, sqrt(x), in the library.
Stage 11: Artist: Nested Loops
Working with Text and Numbers in Java
User Defined Functions
Programming for Artists
CS005 Introduction to Programming
Terminal-Based Programs
Stage 21: Artist: Patterns
Presentation transcript:

Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 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() min() norm() pow() round() sq() sqrt() Trigonometry acos() asin() atan() atan2() cos() degrees() radians() sin() tan() degrees() radians() sin() tan() Random noise() 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 will be one of the three values passed to it. Name of the function Three parameters. This function finds the largest one and returns that as a 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; }

9 Function: max Let’s give it some numbers and print out the answer.

10 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 "); 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; }

11 Function: max Let’s give it some numbers and print out the answer.

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.

Modularity in Drawing

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 Functions Details 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 Functions New 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 Functions Now 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 Functions Now let’s draw a checkerboard (getting the idea??) Squares are at 30 unit Distances starting at (40,40). Now let’s put checkers on the board. White ones on top 3 rows, dark squares.

25 Functions Now 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); }

26 Checkerboard void draw() { board(); placeWhite(); placeBlack (); }

27 Principles Functions are used to create logical modules Complex programs are built from these simpler units. Functions should implement a simple operation. Functions need to be appropriately named.