For the in class exercise, I decided to create a flower.
One of the most interesting functions I used was rotate(), as this is what allowed for the animation in addition to translate() which allowed me to set the center point to be wherever the cursor’s position is. The for() function facilitated the simplification and efficiency of the code so that I would not have to repeat the petals shapes 9 separate times to create 9 petals. I remembered I could do this only after I had already drawn three petals 3 separate times, so at least I was able to save the time it would have taken to draw the other 6 (subtracting, of course, the time it took for me to write the for() function).
Here is the code:
void setup() {
size(600, 600);
smooth();
frameRate(60);
noStroke();
}
void draw() {
background(200,200,2000);
smooth();
// set center point
translate(mouseX, mouseY);
//make the flower rotate with frameCount
rotate(radians(frameCount + mouseY + mouseX));
// draw 9 petals, rotating after each one
fill(2000,200,2000);
for (int i = 0; i < 9; i++) {
ellipse(0, -50, 25, 100);
rotate(radians(40));
}
// center circle
fill(198,255,137);
ellipse(0, 0, 55, 55);
}
For the homework exercise, I followed the instructions to create a centered circle that grows and shrinks continuously while its color changes as well simultaneously. The animation includes the arrow keys which, when pressed, will move the circle around the screen/canvas. Finally, I added the edges of the screen/canvas as the borders so that the circle could not totally disappear from moving it too far with the arrow keys. Using color mode HSB was something I had not previously done and was very fun and entertaining to learn, as we have not focused much on the ways we could create different colors and color combinations in class or in recitation. I found the keyPressed() function to be quite challenging at first as well as setting the borders but once I was shown how to do the first one with the help of a classmate, I was able to write the rest more fluidly.
The homework exercise code:
int circleX;
int circleY;
int radius;
int circle
Speed;
int growSpeed;
float c;
float size = 300;//create a variable in order to be able to change the value of something
boolean pulse = false; //create a boolean variable in order to define the state of increase/decrease
void setup() {
size(600, 600);
colorMode(HSB); //set the color mode to HSB, so that the scope of float variable c is between 0 and 255
smooth();
frameRate(60);//set frameRate to 60 so that the code runs smoothly and at that speed
circleX = width/2;
circleY = height/2;
radius = 25;
circleSpeed = 10;
growSpeed = 4;
}
void draw() {
background(255);
if (c >= 255) {
c=0;
} else {
c++;
}
//change the color
strokeWeight(25);
noFill();
stroke(c, 255, 255);
ellipse(circleX, circleY, radius*2, radius*2);
//circle size gets bigger and smaller in a loop
radius = radius + growSpeed;
if(radius > 150) {
growSpeed = -3;
}
if(radius < 5) {
growSpeed = 3;
}
}
//move the circle by pressing on any of the arrow keys
void keyPressed() {
if ( (keyCode == LEFT) && (circleX > radius*2) ) //set the condition that pressing on the left arrow key and that the x coordinate of the circle has to be greater than the radius
{
circleX = circleX – circleSpeed;
}
if ( (keyCode == RIGHT) && (circleX < width-radius*2) )
{
circleX = circleX + circleSpeed;
}
if ( (keyCode == UP) && (circleY > radius*2) )
{
circleY = circleY – circleSpeed;
}
if ( (keyCode == DOWN) && (circleY < height-radius*2) )
{
circleY = circleY + circleSpeed;
}
}