Astroid
sketch
//Brody Ploeger
//Section C
//jploeger@andrew.cmu.edu
//Project-07
var nPoints = 150
function setup() {
createCanvas(480, 480);
background(220);
//text("p5.js vers 0.9.0 test.", 10, 15);
}
function draw() {
background(0);
push();
translate(width/2,height/2)
//astroid 1
stroke('orange')
noFill()
astroid(0,0);
//astroid 2
stroke('lightblue')
noFill()
astroid2(0,0);
pop();
}
function astroid(x,y,q,w){
//https://mathworld.wolfram.com/Astroid.html
var x;
var y;
var q = map(mouseX,0,width,0,5);
var a = constrain(mouseX,0,width/2);
var b = constrain(mouseY,0,height/2);
var w = constrain(mouseY,0,height);
//shape
beginShape();
for (var i = 0; i<nPoints; i++){
var t = map(i,0,nPoints,0,TWO_PI)
x= a*pow(cos(t*w),3)
y= b*pow(sin(t*w),3)
rotate(radians(q))
vertex(x,y);
}
endShape(CLOSE);
}
function astroid2(x,y,q,w){
//https://mathworld.wolfram.com/Astroid.html
var x;
var y;
var q = map(mouseX,0,width,0,5);
var a = constrain(mouseX,0,width/2);
var b = constrain(mouseY,0,height/2);
var w = constrain(mouseX,0,height);
//shape
beginShape();
for (var i = 0; i<nPoints; i++){
var t = map(i,0,nPoints,0,TWO_PI)
x= a*pow(cos(t*w),3)
y= b*pow(sin(t*w),3)
rotate(radians(q))
vertex(x,y);
}
endShape(CLOSE);
}