Project-07 Curves

sketch

//Robert Rice
//rdrice
//Section C

var x=[];       //bucket of x-coordinates for the vertices
var y=[];       //bucket of y-coordinates for the vertices
var a = 26.5;   //The scale of the curve
var n = 200;    //The curve's "resolution". How many vertexes are used to form the curves.
var q = 3;      //the number of petals

var rStart = 0;
var angle = 0;  //variable that contains the slow, global rotation
var dr = .1;    //speed of slow rotation

function setup() {
    createCanvas(480, 480);
    background(220);
    angleMode(DEGREES);  //me dum dum no like radians
}

function draw() {
    background(0);
    translate(width/2, height/2);   //sets center of the drawng to the center of the canvas
    stroke(255);
    strokeWeight(1);
    noFill();

    q = map(mouseY, 0, height, 3, 30, true);
    a = map(mouseX, 0, width, -26.5, 26.5, true);
    rStart = map(mouseY, 0, height, 0, 360);

    for (i= 0; i < n; i++) {    //calculates the base curve x and y-values
        let t = -10 + (20/n)*i  //calculates 't' values that are used to evaluate x and y components
        x[i] = 3*a*((t**2) - 3);
        y[i] = a*t*((t**2) - 3);
    }

    for (let i = 0; i < q; i+=1) {
        petal(0, 0, rStart+angle+i*(360/q));
    }

    angle = angle + dr  //drives the rotation of the drawing
}

function petal(v, w, r) {
    push();
    translate(v, w);
    rotate(r);

    beginShape()
    for (i=0; i < n; i++) {
        vertex(x[i], y[i]);
    }
    endShape();
    pop(); //forms a petal at v(x-coord) and w(y-coord) with rotation r based on the curve defined in line 25
}

Leave a Reply