Creative Coding - Week 1 - Exercise 3
Sketch Exercise 3 - as supplied.
Sample code was supplied that allows you to draw on the following canvas. You can draw by holding the left mouse button down and moving your mouse. To erase the drawing, press 'r'.
> View source code for preceding sketch.
/*
* Creative Coding
* Week 1, 03 - Draw your name! (part 3)
* by Indae Hwang and Jon McCormack
* Copyright (c) 2014 Monash University
* This program allows you to draw using the mouse.
* Press 's' to save your drawing as an image.
* Press 'r' to erase all your drawing and start with a blank screen
*
*/
// variables to store the delay and target counts
int delayCount;
int targetCount;
// setup function
void setup() {
size(500, 500);
background(255);
delayCount = 0;
targetCount = (int) random(5, 50); // set target count to a random integer between 10 and 50
}
// draw function
void draw() {
/* draw a rectangle on your mouse point while you pressing
the left mouse button*/
int style;
delayCount++; // increment delay count (shorthand for delayCount = delayCount + 1)
if (delayCount == targetCount) {
style = (int) random(4);
targetCount = (int) random(5, 20) ;
delayCount = 0;
}
else {
style = 0;
}
if (mousePressed) {
stroke(0);
fill(0);
// switch statement
switch(style) {
case 0:
// draw a point
point(mouseX, mouseY);
break;
case 1:
// draw a circle with random radius
float esize = random(1, 20);
ellipse(mouseX, mouseY, esize, esize);
break;
case 2:
// draw a random sized rectangle
float qsize = random(1, 10);
quad(mouseX-qsize, mouseY, mouseX, mouseY-qsize, mouseX+qsize, mouseY, mouseX, mouseY+qsize );
break;
case 3:
// draw a triangle with random size
float tsize = random(1, 5);
triangle(mouseX-tsize, mouseY+tsize, mouseX, mouseY-tsize, mouseX+tsize, mouseY+tsize);
break;
} // end of switch statement
}
// save your drawing when you press keyboard 's'
if (keyPressed == true && key=='s') {
saveFrame("yourName.jpg");
}
// erase your drawing when you press keyboard 'r'
if (keyPressed == true && key == 'r') {
background(255);
}
}
Sketch Exercise 3 - as modified.
I modified the supplied code to add some colour. As before, you can draw by holding the left mouse button down and moving your mouse. To erase the drawing, press 'r'.
> View source code for preceding sketch.
/*
* Creative Coding
* Week 1, 03 - Draw your name! (part 3)
* by Indae Hwang and Jon McCormack
* Copyright (c) 2014 Monash University
* Modified by Gerard Holden to add some colour.
* This program allows you to draw using the mouse.
* Press 's' to save your drawing as an image.
* Press 'r' to erase all your drawing and start with a blank screen
*
*/
// variables to store the delay and target counts
int delayCount;
int targetCount;
color[] col = new color[7];
// setup function
void setup() {
size(500, 500);
background(255);
delayCount = 0;
targetCount = (int) random(5, 50); // set target count to a random integer between 10 and 50
col[0] = #000000;
col[1] = #FF0000;
col[2] = #00FF00;
col[3] = #0000FF;
col[4] = #FFFF00;
col[5] = #FF00FF;
col[6] = #00FFFF;
}
// draw function
void draw() {
/* draw a rectangle on your mouse point while you pressing
the left mouse button*/
int style;
delayCount++; // increment delay count (shorthand for delayCount = delayCount + 1)
if (delayCount == targetCount) {
style = (int) random(4);
targetCount = (int) random(5, 20) ;
delayCount = 0;
}
else {
style = 0;
}
if (mousePressed) {
stroke(col[(int) random(7)]);
fill(col[(int) random(7)]);
// switch statement
switch(style) {
case 0:
// draw a point
point(mouseX, mouseY);
break;
case 1:
// draw a circle with random radius
float esize = random(1, 20);
ellipse(mouseX, mouseY, esize, esize);
break;
case 2:
// draw a random sized rectangle
float qsize = random(1, 10);
quad(mouseX-qsize, mouseY, mouseX, mouseY-qsize, mouseX+qsize, mouseY, mouseX, mouseY+qsize );
break;
case 3:
// draw a triangle with random size
float tsize = random(1, 5);
triangle(mouseX-tsize, mouseY+tsize, mouseX, mouseY-tsize, mouseX+tsize, mouseY+tsize);
break;
} // end of switch statement
}
// save your drawing when you press keyboard 's'
if (keyPressed == true && key=='s') {
saveFrame("yourName.jpg");
}
// erase your drawing when you press keyboard 'r'
if (keyPressed == true && key == 'r') {
background(255);
}
}
I submitted my name for this exercise.
