sweetcorn – LineExercises

int[] mouseCoordsX = new int[100];
int[] mouseCoordsY = new int[100];
PVector[] mouseCoords = new PVector[100];
PVector[] offsetCoords = new PVector[100];
int coordIndex = 0;
float growingNoise = 0;
boolean record;

import processing.svg.*;

void setup() {
  size(600, 600);
  stroke(255);
  strokeWeight(1);
  noFill();

  background(0);
}

void draw() {
}

void drawShakyLine() {
  growingNoise += 0.01;
  drawLivingLine(1, growingNoise);
}

void drawCircle(int mode) {
  float radius = (sqrt(pow(mouseX-(width/2), 2) + pow(mouseY - (height/2), 2)));

  if (mode == 0) {
    curveTightness(-0.7);
    beginShape();
    for (int i=0; i<2; i++) {
      curveVertex(radius + width/2, radius + height/2);
      curveVertex(radius + width/2, -radius + height/2);
      curveVertex(-radius + width/2, -radius + height/2);
      curveVertex(-radius + width/2, radius + height/2);
    }
    curveTightness(0);
    endShape();
  } else {
    push();
    translate(width/2 + 4*radius, height/2);
    float step = PI/12;
    for (float i=0; i<2*PI; i+=step) {
      rotate(step);
      translate(0, radius);
      line(0, 0, 0, -radius);
    }
    pop();
  }
}

void drawSpiral() {
  curveTightness(-0.7);
  beginShape();
  push();
  translate(width/2, height/2);
  float step = PI/12;
  for (float i=0; i<12*PI*mouseX/width; i+=step) {
    rotate(step);
    translate(0, i);
    line(0, 0, 0, -i);
  }
  pop();
}

void drawCalligraphicLine() {
  int lineSize = 10;

  beginShape();
  for (var i=lineSize; i<coordIndex-lineSize; i++) {

    float perpX = mouseCoords[i+lineSize].y - mouseCoords[i-lineSize].y;
    float perpY = mouseCoords[i-lineSize].x - mouseCoords[i+lineSize].x;

    float thickness = 200/sqrt(pow(perpX, 2) + pow(perpY, 2));

    float dist = sqrt(pow(perpX, 2) + pow(perpY, 2));

    perpX *= thickness/dist;
    perpY *= thickness/dist;

    offsetCoords[i] = new PVector(mouseCoords[i].x - perpX, mouseCoords[i].y - perpY);

    curveTightness(8);
    if (i%2 == 0) {
      curveVertex(offsetCoords[i].x, offsetCoords[i].y);
    } else {
      curveVertex(mouseCoords[i].x, mouseCoords[i].y);
    }
    curveTightness(0);
  }
  endShape();
}

void drawLineSeries() {
  push();
  translate(0, height/6);
  drawThickLine(1, width/6, 0, 5*width/6, 0);
  translate(0, height/6);
  drawThickLine(10, width/6, 0, 5*width/6, 0);
  translate(0, height/6);
  drawThickLine(20, width/6, 0, 5*width/6, 0);
  translate(0, height/6);
  drawThickLine(30, width/6, 0, 5*width/6, 0);
  translate(0, height/6);
  drawThickLine(40, width/6, 0, 5*width/6, 0);
  translate(0, height/6);
  drawThickLine(50, width/6, 0, 5*width/6, 0);
  pop();
}

void keyPressed() {
  if(key == 's') {
    record = true;
  }
}

void drawThickLine(int thickness, int x1, int y1, int x2, int y2) {
  int lineSize = 10;

  beginShape();
  for (var i=lineSize; i<coordIndex-lineSize; i++) {
    float perpX = mouseCoords[i+lineSize].y - mouseCoords[i-lineSize].y;
    float perpY = mouseCoords[i-lineSize].x - mouseCoords[i+lineSize].x;

    float dist = sqrt(pow(perpX, 2) + pow(perpY, 2));

    perpX *= thickness/dist;
    perpY *= thickness/dist;

    offsetCoords[i] = new PVector(mouseCoords[i].x - perpX, mouseCoords[i].y - perpY);

    curveTightness(8);
    if (i%2 == 0) {
      curveVertex(offsetCoords[i].x, offsetCoords[i].y);
    } else {
      curveVertex(mouseCoords[i].x, mouseCoords[i].y);
    }
    curveTightness(0);
  }
  endShape();
}

void mouseMoved() {
  background(0);
  
  if (record) {
    // Note that #### will be replaced with the frame number. Fancy!
    beginRecord(SVG, "frame-####.svg");
  }

  //drawDashedLine(); // 1
  //drawLivingLine(0, 0); // 2.1
  //drawLivingLine(1, 0); // 2.2
  //drawShakyLine(); // 3
  //drawCircle(0); // 4.1
  //drawCircle(1); // 4.2
  //drawSpiral(); // 5
  //drawLivingLine(2, 0); // 6
  //drawThickLine(0, 50); // 7.1
  //drawThickLine(1, 50); // 7.2
  //drawCalligraphicLine(); // 8
  drawLineSeries();

  if (coordIndex < 99) {
    coordIndex++;
  }

  mouseCoordsX[coordIndex] = mouseX;
  mouseCoordsY[coordIndex] = mouseY;

  mouseCoords[coordIndex] = new PVector(mouseX, mouseY);

  for (var i=0; i<coordIndex; i++) {
    mouseCoordsX[i] = mouseCoordsX[i+1];
    mouseCoordsY[i] = mouseCoordsY[i+1];

    mouseCoords[i] = mouseCoords[i+1];
  }
  
  if (record) {
    endRecord();
    record = false;
  }
}

void drawLivingLine(int mode, float noise) {
  beginShape();
  if (mode == 0) {
    for (var i=0; i < coordIndex; i++) {
      vertex(mouseCoordsX[i] + noise*randomGaussian(), mouseCoordsY[i] + noise*randomGaussian());
    }
  } else if (mode == 1) {
    for (var i=0; i < coordIndex; i++) {
      vertex(mouseCoords[i].x + noise*randomGaussian(), mouseCoords[i].y + noise*randomGaussian());
    }
  } else if (mode == 2) {
    for (var i=0; i < coordIndex; i++) {
      vertex(mouseCoords[i].x + noise*randomGaussian(), mouseCoords[i].y + noise*randomGaussian());
    }
    endShape();
    beginShape();
    int offset = 100;
    int lineSize = 10;
    for (var i=lineSize; i < coordIndex-lineSize; i++) {
      float perpX = mouseCoords[i+lineSize].y - mouseCoords[i-lineSize].y;
      float perpY = mouseCoords[i-lineSize].x - mouseCoords[i+lineSize].x;

      float dist = sqrt(pow(perpX, 2) + pow(perpY, 2));

      perpX *= offset/dist;
      perpY *= offset/dist;

      vertex(mouseCoords[i].x - perpX, mouseCoords[i].y - perpY);
    }
  }
  endShape();
}

void drawThickLine(int mode, int thickness) {
  int lineSize = 10;

  beginShape();
  for (var i=lineSize; i<coordIndex-lineSize; i++) {
    float perpX = mouseCoords[i+lineSize].y - mouseCoords[i-lineSize].y;
    float perpY = mouseCoords[i-lineSize].x - mouseCoords[i+lineSize].x;

    float dist = sqrt(pow(perpX, 2) + pow(perpY, 2));

    perpX *= thickness/dist;
    perpY *= thickness/dist;

    offsetCoords[i] = new PVector(mouseCoords[i].x - perpX, mouseCoords[i].y - perpY);

    if (mode == 0) {
      curveTightness(8);
      if (i%2 == 0) {
        curveVertex(offsetCoords[i].x, offsetCoords[i].y);
      } else {
        curveVertex(mouseCoords[i].x, mouseCoords[i].y);
      }
      curveTightness(0);
    } else if (mode == 1) {
      for (int j=0; j<thickness; j+=thickness/20) {
        if (j%thickness/40 == 0) {
          vertex(offsetCoords[i].x + j, offsetCoords[i].y);
        } else {
          vertex(mouseCoords[i].x, mouseCoords[i].y);
        }
      }
    }
  }
  endShape();
}

void drawDashedLine() {
  push();
  rotate(atan2(mouseY, mouseX));
  for (var i=0; i<sqrt(pow(mouseY, 2) + pow(mouseX, 2)); i+=20) {
    line(i, 0, i+10, 0);
  }
  pop();
}