sketch_210218a

sketch_210218a.pde

final int N_CURVES = 50;
final int N_POINTS = 200;
float randomOffset = 0.5;

float[][] A;

void setup() {
  size(800, 800);
  
  float[][] scaleMat = {{2.0, 0.0}, {0.0, 1.0}};
  float[][] rotMat = matrixFromAngle(-PI / 6);
  A = multMatrix(rotMat, scaleMat);
}

void draw() {
  background(0);
  translate(width / 2.0, height / 2.0);
  
  fill(255, 64);
  noStroke();
  
  float a = 1.0;
  float b = 1.0;
  
  for (int n = 0; n < N_CURVES; n++) {
    float[][] R = matrixFromAngle(TWO_PI / N_CURVES * n + TWO_PI / 00.0 * frameCount);
    for (int k = 0; k < N_POINTS; k++) {
      float t = (TWO_PI / N_POINTS * k + TWO_PI / 60.0 * frameCount) % TWO_PI;
      float r = a * exp(b * t);
      
      float x = r * cos(t) + random(-randomOffset, randomOffset);
      float y = r * sin(t) + random(-randomOffset, randomOffset);
      PVector p = new PVector(x, y);
      PVector q = transform(multMatrix(A, R), p);
      
      float d = damping(t);
      float radius = 8.0 * d;
      
      fill(255 * d, 255 * d, 255, 200);
      circle(q.x, q.y, radius);
    }
  }
}

// http://w3e.kanazawa-it.ac.jp/math/physics/category/mechanics/masspoint_mechanics/damped_harmonic_motion/henkan-tex.cgi?target=/math/physics/category/mechanics/masspoint_mechanics/damped_harmonic_motion/dphm_overdamping.html
float damping(float t) {
  float lambda = 1.0;
  float eta = 0.3;
  float v0 = 4.0;
  
  float c1 = (lambda + eta + v0) / (2 * eta);
  float c2 = -(lambda - eta + v0) / (2 * eta);
  
  float d = exp(-lambda*t) * (c1*exp(eta*t) + c2*exp(-eta*t));
  
  return d;
}

PVector transform(float[][] A, PVector v) {
  float x = A[0][0] * v.x + A[0][1] * v.y;
  float y = A[1][0] * v.x + A[1][1] * v.y;
  return new PVector(x, y);
}

float[][] multMatrix(float[][] A, float[][] B) {
  float[][] C = new float[2][2];
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      for (int k = 0; k < 2; k++) {
        C[i][j] = C[i][j] + A[i][k] * B[k][j];
      }
    }
  }
  return C;
}

float[][] matrixFromAngle(float t) {
  float[][] R = {{cos(t), -sin(t)}, {sin(t), cos(t)}};
  return R;
}

void printMatrix(float[][] A) {
  for (int i = 0; i < A.length; i++) {
    for (int j = 0; j < A[i].length; j++) {
      print(String.format("%f, ", A[i][j]));
    }
    println();
  }
}