GarrettRauck-Project-07

sketch

//Garrett Rauck
//Section C
//grauck@andrew.cmu.edu
//Assignment-07-Project-Composition with Curves

/////////////////////////////////
// DECLARE VARIABLES
/////////////////////////////////
//canvas vars
var canvasWidth, canvasHeight, nRows, nCols, cellSizeX, cellSizeY;
//event vars
var mx, my; //constrained mouseX and mouseY
//curve vars
var nPts, scaleFactor, falloffFactor;
//color

/////////////////////////////////
// HELPER FNS
/////////////////////////////////
function drawConchoidOfDeSluzeCurve(cx, cy, a) {
	//Conchoid of de Sluze
	//http://mathworld.wolfram.com/ConchoidofdeSluze.html

	var x, y;

	var scaleFactor = map(my, 0, canvasHeight, 1, 10);

	noFill();
	stroke(255);
	beginShape();
	for (var i = 0; i < nPts; i++) {
		var t = map(i, 0, nPts, 0, TWO_PI);
		// compute raw curve vals
		x = (1/cos(t) + a*cos(t)) * cos(t); // x = (sec t + a cos t) cos t
		y = (1/cos(t) + a*cos(t)) * sin(t); // x = (sec t + a cos t) sin t
		// scale vals
		x *= scaleFactor;
		y *= scaleFactor;
		// position in canvas
		x += cx;
		y += cy;
		// create vertex
		vertex(x, y);
	}
	endShape();
}

/////////////////////////////////
// EVENTS
/////////////////////////////////

/////////////////////////////////
// RUN!
/////////////////////////////////
function setup() {
	// INIT VARS
	nPts = 75;
	falloffFactor = 0.6;
	//canvas
	canvasWidth = canvasHeight = 640;
	nRows = 4;
	nCols = 4;
	cellSizeX = canvasWidth/nCols;
	cellSizeY = canvasHeight/nRows;

	// CANVAS SETUP
    createCanvas(canvasWidth, canvasHeight);
}

function draw() {
	background(0); //update background

	//constrain mouse position to canvas
	mx = constrain(mouseX, 0, canvasWidth);
	my = constrain(mouseY, 0, canvasHeight);
	//get a value of outer-most curve
	var aMax = map(mx, 0, canvasWidth, -50, 50);

	//create grid
	for (row = 0; row < nRows; row++) {
		for (col = 0; col < nCols; col++) {
			var cx = col*cellSizeX + cellSizeX/2;
			var cy = row*cellSizeY + cellSizeY/2;
			//create nested Conchoid of de Sluze curves at grid pt
			for (a = aMax; abs(a) > 1; a *= falloffFactor) {
				//draw curve centered at pt
				drawConchoidOfDeSluzeCurve(cx, cy, a);
	        }
		}
	}
	
}

I chose one of the curves off of the Wolfram web site and used the mouse position to manipulate the scale of the curve and the order, or “a” variable, of the curve. For loops is are used to create the nested curves of different orders, as well as to create the overall array of curves for the composition.

Leave a Reply