Class Notes (835,495)
Canada (509,212)
COMP 1005 (11)
Lecture

Draw_Function_and_Simulation.doc

7 Pages
127 Views
Unlock Document

Department
Computer Science
Course
COMP 1005
Professor
Jason Hinek
Semester
Winter

Description
The draw function. • Unusual function which introduces a time element into the code. • Called automatically from after setup() and is like an infinite loop. It executes the code inside the function perpetually. You never call the draw function explicitely. • Can be stopped by stopping the program or calling the noLoop() function from within the function. • The number of times n the draw() function executes in each second may be controlled with the frameRate() function. • The default frame rate is 60 frames per second but you can e.g. make it say 30 frames per second as follows: frameRate(30) If you can’t call the draw functions how do you pass parameters to it? You use global parameters. Example: A moving circle in one dimension. //MOVING CIRCLE in ONE DIMENSION // Make these variables Global int x= 20; int y =250; int radius=20; void setup(){ size(500,500); framerate (60); } // THE DRAW FUNCTION void draw(){ drawcircle( ); } void drawcircle(){ background(0); // set background to zero…wipes out previous circle // to give the illusion of motion ellipse(x, y, radius, radius); //when circle gets to the right hand side reset it to the left hand side if(x>480){ x=20;} else {x=x+1;} } Example 2 //MOVING CIRCLE in TWO DIMENSIONS // Global Variables int x= 20; int y =250; int vx=3; int vy = 6; int radius=20; // setup Function void setup(){ size(500,500); // note that default framerate = 60 frameRate(120); } void draw(){ drawcircle( ); } void drawcircle(){ background(0); ellipse(x, y, radius, radius); if ((x<0)||(x>500)){vx = -vx;} if ((y<0)|| (y>500)){vy = -vy;} x=x+vx; y=y+vy; } Example 3 // Moving Circles in two dimensions using Angles // Here we do not think in terms of speed but the angle of motion //Global variable declaration float x= 0; float y =500; float radius=10; float angle = -3.14159/6.0; void setup(){ size(500,500); drawcircle( ); } void draw(){ drawcircle( ); } void drawcircle(){ background(0); ellipse(x, y, radius, radius); if((x>500) ||(x<0)||(y>500) ||(y<0) ){ angle = 3.14159 - angle ; x=x+1*cos(angle); y=y+1*sin(angle);} else {x=x+1*cos(angle); y=y+1*sin(angle);} } Using the Mouse An example of event driven code. The program is driven b
More Less

Related notes for COMP 1005

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit