Jenni Lee — Project 07 — Curves

sketch

/* Jenni Lee
Section E
jennife5@andrew.cmu.edu
Project - 07
*/


var a = 60;

var r = 255,
  g = 0,
  b = 0;

var curveType = 0; // draw different curve type depending on mouse click

function setup() {
  createCanvas(480, 480);
  frameRate(15);
  angleMode(RADIANS);
}

function draw() {

  background(255);
  if (curveType == 0) {
    drawEpitrochoidCurves();
  } else {
    drawHypocycloidPedalCurve();
  }
}

function drawEpitrochoidCurves() {

  a = map(mouseX, 0, width, 20, 120); // a is the radius of the inner circle
  a = constrain(a, 20, 120);
  var ratioB = floor(map(mouseY, 0, height, 2, 20)); // randomize ratioB to get 
  //inner circle radius when mouse pressed
  var ratioH = floor(map(mouseY, 0, height, 1, 6)); // randomize ratioH to get 
  //crossing radius when mouse pressed

  var b = a / ratioB;
  var h = ratioH * b;

  var t = 0.0;
  stroke(r, g, b);
  strokeWeight(2);
  beginShape(LINES);
  for (var i = 0; i < 1600; i++) {
    var x = (a + b) * cos(t) - h * cos((a + b) * t / b);
    var y = (a + b) * sin(t) - h * sin((a + b) * t / b);
    vertex(x + width / 2, y + height / 2);
    t += 0.008;
  }
  endShape();
}

function drawHypocycloidPedalCurve() {
  a = map(mouseX, 0, width, 20, 240); // a is the radius of the inner circle, 
  //depending on mouseX position  
  a = constrain(a, 20, 240);
  var t = 0.0;
  var n = floor(map(mouseY, 0, height, 3, 24)); // # of paddles, from 3 to 24 
  //depending on mouseY position
  n = constrain (n, 3, 24);
  beginShape(LINES);
  stroke(0, 0, 0);
  strokeWeight(2);
  for (var i = 0; i < 1600; i++) {
    var x = a * ((n - 1) * cos(t) + cos((n - 1) * t)) / n;
    var y = a * ((n - 1) * sin(t) - sin((n - 1) * t)) / n;
    vertex(x + width / 2, y + height / 2);
    t += 0.008;
  }
  endShape();

  stroke(r, g, b);
  strokeWeight(2);
  beginShape(LINES);
  for (var i = 0; i < 2000; i++) {
    var x = a * (n - 2) * (cos(t) - cos((1 - n) * t)) / (2 * n);
    var y = a * (n - 2) * cos(t * (1 - n / 2)) * sin(n * t / 2) / n;
    vertex(x + width / 2, y + height / 2);
    t += 0.008;
  }
  endShape();
}

function mousePressed() {
  curveType = 1 - curveType;
  r = random(0, 255);
  g = random(0, 255);
  b = random(0, 255);
}

// first curve: 
// Epitrochoid curves/equation
// http://mathworld.wolfram.com/Epitrochoid.html
// x	=	(a+b)cos(t)-h*cos((a+b)/b*t)	
// y	=	(a+b)sin(t)-h*sin((a+b)/b*t)

// second curve:
// Hypocycloid Pedal Curve
// http://mathworld.wolfram.com/HypocycloidPedalCurve.html
/*
The pedal curve for an n-cusped hypocycloid

x	=	a((n-1)cost+cos[(n-1)t])/n	
y	=	a((n-1)sint-sin[(n-1)t])/n	

with pedal point at the origin is the curve

x_p	=	a((n-2){cost-cos[(1-n)t]})/(2n)	
y_p	=	a((n-2)cos[t(1-1/2n)]sin(1/2nt))/n.
*/

This project was entertaining for me because I enjoy browsing/analyzing the artwork of other artists/designers, so implementing different curves created by others was really fun. I used the curves/equation for the epitrochoid and the hypocloid pedal curve. This project required a bit of math, so it was a nice memory-refresher of high school math.

Leave a Reply