Module 01 Processing Recap. CS 106 Winter 2018

Similar documents
Module 01 Processing Recap

Module 05 User Interfaces. CS 106 Winter 2018

Pick a number. Conditionals. Boolean Logic Relational Expressions Logical Operators Numerical Representation Binary. CS Conditionals 1

Using Methods. Methods that handle events. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Conditional Events. Mouse events and Operators. Dr. Siobhán Drohan Mairead Meagher. Produced by:

Variables. location where in memory is the information stored type what sort of information is stored in that memory

CISC 1600 Lecture 3.1 Introduction to Processing

1 Getting started with Processing

Watch the following for more announcements

A B C D CS105 03a Interaction

Interaction Design A.A. 2017/2018

CISC 1600, Lab 2.2: Interactivity in Processing

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Variables One More (but not the last) Time with feeling

[ the academy_of_code] Senior Beginners

Variables and Control Structures. CS 110 Eric Eaton

Appendix: Common Errors

Final Exam Winter 2013

CISC 1600, Lab 3.2: Interactivity in Processing

Kimberly Nguyen Professor Oliehoek Introduction to Programming 8 September 2013

CS 101 Media (Images, Video, etc) Lecture 20

Using Methods. Writing your own methods. Dr. Siobhán Drohan Mairead Meagher. Produced by: Department of Computing and Mathematics

CST112--Functions Page 1

(Inter)Ac*ve Scripts. Sta*c Program Structure 1/26/15. Crea+ve Coding & Genera+ve Art in Processing 2 Ira Greenberg, Dianna Xu, Deepak Kumar

Solution Notes. COMP 151: Terms Test

Basic Computer Programming (Processing)

Miscellaneous Stuff That Might Be Important.

Bits and Bytes. How do computers compute?

Functions. Functions. nofill(); point(20, 30); float angle = map(i, 0, 10, -2, 2); parameters return values

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

mith College Computer Science CSC103 How Computers Work Week 6 Fall 2017 Dominique Thiébaut

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Evaluating Logical Expressions

What is a variable? a named location in the computer s memory. mousex mousey. width height. fontcolor. username

mith College Computer Science CSC103 How Computers Work Week 7 Fall 2017 Dominique Thiébaut

CSE120 Wi18 Final Review

GRAPHICS & INTERACTIVE PROGRAMMING. Lecture 1 Introduction to Processing

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

+ Questions about Assignment 5?

CST112 Looping Statements Page 1

CS 106 Winter 2016 Craig S. Kaplan. Module 01 Processing Recap. Topics

Chapter 12: Functions Returning Booleans and Collision Detection

Exploring Processing

What is a variable? A named locajon in the computer s memory. A variable stores values

if / if else statements

The way I feel about music is that there is no right and wrong. Only true and false. Fiona Apple. true false false

1 Getting started with Processing

What can we do with Processing? Let s check. Natural Language and Dialogue Systems Lab Guest Image. Remember how colors work.

Pointers and scanf() Steven R. Bagley

Introduction to Processing

Recall that creating or declaring a variable can be done as follows:

CISC 1600, Lab 3.1: Processing

CISC 1600 Lecture 2.2 Interactivity&animation in Processing

CISC 1600, Lab 2.1: Processing

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Repetition is the reality and the seriousness of life. Soren Kierkegaard

CISC 1600 Lecture 2.2 Interactivity&animation in Processing

Loops. Variable Scope Remapping Nested Loops. Donald Judd. CS Loops 1. CS Loops 2

CS 101 Functions. Lecture 15

Sten-SLATE ESP. Graphical Interface

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

We will start our journey into Processing with creating static images using commands available in Processing:

CSCE 110 Dr. Amr Goneid Exercise Sheet (6): Exercises on Structs and Dynamic Lists

Introduction to Processing. Sally Kong

CS110 Introduction to Computing Fall 2016 Practice Exam 1 -- Solutions

Object-Oriented Programming Concepts

CS 106 Winter Lab 05: User Interfaces

Question 1 (10 points) Write the correct answer in each of the following: a) Write a Processing command to create a canvas of 400x300 pixels:

Chapter 5. Condi.onals

Iteration in Programming

CS110 Introduction to Computing Fall 2016 Practice Exam 1

CMPT-166: Sample Midterm

5.1. Examples: Going beyond Sequence

Processing Assignment Write- Ups

CS 106 Winter 2016 Craig S. Kaplan. Module 08 Randomness and noise Topics

Review. Custom Objects. Comparing Declarations and Initializers Built PopGame. Classes Fields and Methods Instantiation using the "new" keyword

More C++ : Vectors, Classes, Inheritance, Templates

Interaction Design A.A. 2017/2018

CPSC Fall L01 Final Exam

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Khan Academy JavaScript Study Guide

Interaction Design A.A. 2017/2018

CS 201 Advanced Object-Oriented Programming Lab 10 - Recursion Due: April 21/22, 11:30 PM

1.8 Coordinate Geometry. Copyright Cengage Learning. All rights reserved.

Fast Introduction to Object Oriented Programming and C++

Chapter 4 Defining Classes I

COMP519 Web Programming Lecture 21: Python (Part 5) Handouts

Question 2. [5 points] Given the following symbolic constant definition

void mouseclicked() { // Called when the mouse is pressed and released // at the same mouse position }

CMPT-166: Sample Final Exam Answer Key

Methods (cont.) Chapter 7

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

CST112 Variables Page 1

1. Complete these exercises to practice creating user functions in small sketches.

G Programming Languages - Fall 2012

CS 106 Winter Lab 03: Input and Output

Topic 7: Algebraic Data Types

Algebra II. Slide 1 / 181. Slide 2 / 181. Slide 3 / 181. Conic Sections Table of Contents

CS/ENGRD 2110 SPRING 2018

Transcription:

Module 01 Processing Recap CS 106 Winter 2018

Processing is a language a library an environment

Variables A variable is a named value. It has a type (which can t change) and a current value (which can change).

Variables A declaration introduces a new variable, and optionally gives it an initial value. int a; float b = 6.28; boolean c = b > 19; Three declarations

Variables A declaration introduces a new variable, and optionally gives it an initial value. int a; float b = 6.28; boolean c = b > 19; Type

Variables A declaration introduces a new variable, and optionally gives it an initial value. int a; float b = 6.28; boolean c = b > 19; Name

Variables A declaration introduces a new variable, and optionally gives it an initial value. int a; float b = 6.28; boolean c = b > 19; Initial value

Variables Say a variable s name to read from it. Use assignment (=) to write to it. Processing includes many built-in names. True constants can t be changed. Some variables are meant to be readonly. Some are updated automatically, and are meant to be read repeatedly.

CQ What does this program print? int a = 17; void test( int a ) println( a + 5 ); void setup() func( 10 ); (A) 5 (B) 15 (C) 22 (D) a + 5 (E) Nothing

Scope Every declaration in Processing has a scope: the part of the program source code in which that declaration is valid. Usually either global or bounded by the nearest enclosing. Scope is a complicated topic. If in doubt, just avoid re-using the same names!

Control flow By default, Processing will execute statements in the order they re given. Control flow can modify that order.

Conditionals if( keypressed && key == ' ' ) ellipse( mousex, mousey, 20, 20 ); An if statement

Conditionals Condition if( keypressed && key == ' ' ) ellipse( mousex, mousey, 20, 20 );

Conditionals if( keypressed && key == ' ' ) ellipse( mousex, mousey, 20, 20 ); Body

Conditionals if( keypressed && key == ' ' ) ellipse( mousex, mousey, 20, 20 ); else rect( mousex, mousey, 20, 20 );

Conditionals if ( keypressed ) if ( key == 'e' ) ellipse( mousex, mousey, 20, 20 ); else if ( key == 'l' ) line( 10, 10, 100, 100 ); else rect( mousex, mousey, 20, 20 );

While loops int y = 0; while( y < height ) line( 0, y, width, y ); y = y + 10;

While loops int y = 0; Condition while( y < height ) line( 0, y, width, y ); y = y + 10;

While loops int y = 0; while( y < height ) line( 0, y, width, y ); y = y + 10; Body

While loops int y = 0; while( y < height ) line( 0, y, width, y ); y = y + 10; Update!

For loops for( int y = 0; y < height; y += 10 ) line( 0, y, width, y );

For loops Initializer for( int y = 0; y < height; y += 10 ) line( 0, y, width, y );

For loops Condition for( int y = 0; y < height; y += 10 ) line( 0, y, width, y );

For loops Update for( int y = 0; y < height; y += 10 ) line( 0, y, width, y );

For loops for( int y = 0; y < height; y += 10 ) line( 0, y, width, y ); Body

Functions A function gives a name to a computation. Benefits: Ease of (error-free) repetition. Encapsulation: hide the messy details. Abstraction: think about problem solving at a higher level. Establish a point of connection between parts of a program.

(x3,y3) (x2,y2) (x1,y1) Calculate the perimeter of a triangle.

Pythagorean theorem

(x3,y3) (x2,y2) (x1,y1) float e1 = sqrt( sq( x2 - x1 ) + sq( y2 - y1 ) ); float e2 = sqrt( sq( x3 - x2 ) + sq( y3 - y2 ) ); float e3 = sqrt( sq( x1 - x3 ) + sq( y1 - y3 ) ); float perim = e1 + e2 + e3;

float measure( float ax, float ay, float bx, float by ) return sqrt( sq( bx - ax ) + sq( by - ay ) );

Return type float measure( float ax, float ay, float bx, float by ) return sqrt( sq( bx - ax ) + sq( by - ay ) );

Function name float measure( float ax, float ay, float bx, float by ) return sqrt( sq( bx - ax ) + sq( by - ay ) );

Parameters float measure( float ax, float ay, float bx, float by ) return sqrt( sq( bx - ax ) + sq( by - ay ) );

float measure( float ax, float ay, float bx, float by ) return sqrt( sq( bx - ax ) + sq( by - ay ) ); Body

float measure( float ax, float ay, float bx, float by ) return sqrt( sq( bx - ax ) + sq( by - ay ) ); Return statement If a function s return type is not void, it has to contain one or more return statements.

ax bx cx dx answer float measure( float ax, float ay, float bx, float by ) return sqrt( sq( bx - ax ) + sq( by - ay ) );

(x3,y3) (x2,y2) (x1,y1) float e1 = sqrt( sq( x2 - x1 ) + sq( y2 - y1 ) ); float e2 = sqrt( sq( x3 - x2 ) + sq( y3 - y2 ) ); float e3 = sqrt( sq( x1 - x3 ) + sq( y1 - y3 ) ); float perim = e1 + e2 + e3;

(x3,y3) (x2,y2) (x1,y1) float measure( float ax, float ay, float bx, float by ) return sqrt( sq( bx - ax ) + sq( by - ay ) ); float e1 = measure( x1, y1, x2, y2 ); float e2 = measure( x2, y2, x3, y3 ); float e3 = measure( x3, y3, x1, y1 ); float perim = e1 + e2 + e3;

(x3,y3) (x2,y2) (x1,y1) float e1 = dist( x1, y1, x2, y2 ); float e2 = dist( x2, y2, x3, y3 ); float e3 = dist( x3, y3, x1, y1 ); float perim = e1 + e2 + e3;

float triangleperim( float x1, float y1, float x2, float y2, float x3, float y3 ) float e1 = dist( x1, y1, x2, y2 ); float e2 = dist( x2, y2, x3, y3 ); float e3 = dist( x3, y3, x1, y1 ); return e1 + e2 + e3;

Functions A function takes 0 or more parameters as input and returns 0 or 1 values as output. 0 parameters 1+ parameters No return value Universal command! Contingent command Return value Retrieve hidden information Calculate something

Hooks Processing knows about a few predetermined function names. If you define functions (hooks) with those names, Processing will call them at the right times. Examples: setup(), draw(), mousepressed(), keypressed() Some libraries add more hooks.

Arrays An array is a sequence of values, all of the same type, bundled into a single master value. float[] ts1 = -4.8, -4.79, -4.764, -4.762, -4.764, -4.824, /* 86 more numbers... */ -1.083, -1.2, -1.3, -1.41 ; float[] ts2 = -21.08933, -21.814, -22.542, -22.01667, -20.912, -21.564 /* 86 more numbers... */ -27.48999, -27.43200, -27.88466, -28.09467 ;

Arrays An array is a sequence of values, all of the same type, bundled into a single master value. Array type float[] ts1 = -4.8, -4.79, -4.764, -4.762, -4.764, -4.824, /* 86 more numbers... */ -1.083, -1.2, -1.3, -1.41 ;

Arrays An array is a sequence of values, all of the same type, bundled into a single master value. Initial value(s) float[] ts1 = -4.8, -4.79, -4.764, -4.762, -4.764, -4.824, /* 86 more numbers... */ -1.083, -1.2, -1.3, -1.41 ;

float[] ts1 = -4.8, -4.79, -4.764, -4.762, -4.764, -4.824, /* 86 more numbers... */ -1.083, -1.2, -1.3, -1.41 ; for( int idx = 0; idx < ts1.length; ++idx ) if( ts1[idx] > 0.0 ) println( "Where's my sunscreen?" );

float[] ts1 = -4.8, -4.79, -4.764, -4.762, -4.764, -4.824, /* 86 more numbers... */ -1.083, -1.2, -1.3, -1.41 ; Array size for( int idx = 0; idx < ts1.length; ++idx ) if( ts1[idx] > 0.0 ) println( "Where's my sunscreen?" );

float[] ts1 = -4.8, -4.79, -4.764, -4.762, -4.764, -4.824, /* 86 more numbers... */ -1.083, -1.2, -1.3, -1.41 ; for( int idx = 0; idx < ts1.length; ++idx ) if( ts1[idx] > 0.0 ) println( "Where's my sunscreen?" ); Element access

Classes and objects A class introduces a new type. Values of that type (instances) have their own state and behaviour.

class Circle float cx; float cy; float radius; Circle( float cxin, float cyin, float radiusin ) cx = cxin; cy = cyin; radius = radiusin; void draw() ellipse( cx, cy, 2*radius, 2*radius );

class Circle Type name float cx; float cy; float radius; Circle( float cxin, float cyin, float radiusin ) cx = cxin; cy = cyin; radius = radiusin; void draw() ellipse( cx, cy, 2*radius, 2*radius );

class Circle float cx; float cy; float radius; Fields (per-instance state) Circle( float cxin, float cyin, float radiusin ) cx = cxin; cy = cyin; radius = radiusin; void draw() ellipse( cx, cy, 2*radius, 2*radius );

class Circle float cx; float cy; float radius; Constructor (initialize state) Circle( float cxin, float cyin, float radiusin ) cx = cxin; cy = cyin; radius = radiusin; void draw() ellipse( cx, cy, 2*radius, 2*radius );

class Circle float cx; float cy; float radius; Circle( float cxin, float cyin, float radiusin ) cx = cxin; cy = cyin; radius = radiusin; Method (behaviour) void draw() ellipse( cx, cy, 2*radius, 2*radius );

Circle[] circs; void setup() circs = new Circle[10]; for ( int idx = 0; idx < circs.length; ++idx ) circs[idx] = new Circle( random(100), random(100), random(20) ); void draw() background( 255 ); for ( int idx = 0; idx < circs.length; ++idx ) circs[idx].draw();

null null is a special keyword that represents a nonexistent value for every class. It is the default value for variables of class type. It s illegal to access any fields or methods of null. void draw() background( 255 ); for ( int idx = 0; idx < circs.length; ++idx ) if ( circs[idx]!= null ) circs[idx].draw();

Point getintersection( Line l1, Line l2 ) if(... ) return new Point(...,... ); else return null;

What s this? In CS 106, you can think of the keyword this as meaning this sketch. Some external libraries want to know which sketch they re running in when you initialize them. import processing.video.*; Capture cam; void setup() cam = new Capture( this, 320, 240 ); cam.start();

The keyword this is actually a general feature of many object-oriented languages. It refers to the instance of a class that received a method call. It s relevant in Processing because the entire sketch is actually one big class, even though you never see that. The fact that you have to use this explicitly is a design flaw in Processing. Jacques Carelman, Coffeepot for Masochists