#Blog 12 Recitation 7: Functions and Arrays

Part 1: Grid Pattern

I want to draw fished because in my Chinese name there is a character “鲶”, which means catfish. And it brings a sense of freedom and wildness.

This is the final code:

One thing I learned is we don’t need to use translate function for creating a grid.

float x = 20;
float y = 30;
float stepX = 48;
float stepY = 20;

void setup() {
  size(800, 800);
  background(41,227,216);
}

void draw() {
for(int i=0;i<width;i+=150){
    for (int j=0;j<height;j+=60){
      drawFish(i, j, 4, color(150,183,181));
    }
  }
}

void drawFish(float u, float v, float s, color c) {
  stroke(22,55,106);
  fill(c);
  triangle(u, v, u+20, v+10, u+20, v-10);
  triangle(u+20, v+10, u+20, v-10, u+80, v);
  triangle(u+80, v, u+90, v+6, u+90, v-6);
  fill(255);
  circle(u+13, v-3, s); //eye
  circle(u-6, v, s*1.4); //bubble
  circle(u-6, v-10, s*1.8); //bubble
  circle(u-6, v-26, s*2.2); //bubble
}

Part 2: Moving Shapes

 

The new function I learned is scale and angle. With these, I can flip the fish horizontally.

This is the final code:

int n = 84;
float[] x = new float[n];
float[] y = new float[n];
float[] stepX = new float[n];
color[] c = new color[n];
int[] angle = new int[n];
void setup() {
  size(800, 800);
  background(41, 227, 216);
  for (int i=0; i< n; i++) {
    x[i] = random(width);
    y[i] = random(height);
    stepX[i] = random(-6, 6);
    c[i] = color(random(255), random(255), random(255));   
      if (stepX[i]>0) {
      angle[i]=-1;
    } else {
      angle[i]=1;
    }
  }
}

void draw() {
  background(41, 227, 216);

  for (int i=0; i< n; i++) {
    pushMatrix();
    translate(x[i], y[i]);
    scale(angle[i], 1);
    drawFish(0, 0, 4, c[i]);
    popMatrix();
    x[i]= x[i]+stepX[i];
    if (x[i]<0 || x[i]>width) {
      stepX[i] = -stepX[i];
      angle[i]=-angle[i];
    }
  }
}

void drawFish(float u, float v, float s, color c) {
  stroke(22, 55, 106);
  fill(c);
  triangle(u, v, u+20, v+10, u+20, v-10);
  triangle(u+20, v+10, u+20, v-10, u+80, v);
  triangle(u+80, v, u+90, v+6, u+90, v-6);
  fill(255);
  circle(u+13, v-3, s); //eye
  circle(u-6, v, s*1.4); //bubble
  circle(u-6, v-10, s*1.8); //bubble
  circle(u-6, v-26, s*2.2); //bubble
}

April 8, 2022, Jinqiao, Younian Liu

Leave a Reply

Your email address will not be published. Required fields are marked *