Code operation "Harmonic Motion and the Fourier Series"
3.5 Simple Harmonic Motion - Nature of Code
createCanvas(400,400);//create a canvas element in the document and set its dimensions in pixels.
}
function draw(){//the function to be executed continuously is declared.
background(0, 10);
translate(200,200);//Moving the object.
fill(252, 238, 33);//the color of the object is defined.
let r = sin(angle); //The variable r, which is the result, will store the operation that is being performed, which is the sine multiplying by the angle. Otherwise if we multiply it by 200, that is, let r = sin(angle)*200; the value oscillates between negative 200 and 200 and when executing, the circle does a certain action that opens and closes within it quickly and this happens due to the value that was placed in the angle variable with respect to time. Otherwise let r=sin(angle), -1,1,0,200); a certain range is declared with the values of -1 and 1 with a mapping that between a range between 0 and 200 line(0,0,0,y)// a small line is drawn from the center that will be accompanied on its path by the circle.
circle(0, 0, r * 2);// the object is defined with its respective coordinates, the variable multiplied by 2.
let increment = TWO_PI/60;//an increment variable is declared that will be equal to 2 pi divided by 60.
angle += 0.1;// the variable that is our angle increases slowly with respect to time, in this there is an inflation, if it is required to decrease as the deflation increases, that is, angle+=0.2;
angle += increment;// increases the value with respect to the operation.
console.log(frameRate()); //allows the frame rate, that is, the behavior of the circle with an amplitude and a point.
}
Coding Challenge #125: Fourier Series
let time = 0;Variable is declared
let wave = [];declaration of an empty array.
function setup(){ //declare the function to be executed once.
createCanvas(600,400);//create a canvas element in the document and set its dimensions in pixels.
}
function draw(){//the function to be executed continuously is declared.
backgroud(0);
translate(200,200);//Moving the object.
let radius=100;//Variable is declared, circle radius
let x=0;//Variable is declared
let y=0;//Variable is declared
for(let i=0; i<2; i++){//cycle declaration
let prevx=x;//Variable is declared
let prevy=y;//Variable is declared
let n=1*2+1;//Variable is declared
let radius = 50*(4/(n*PI));//Variable is declared
x+=radius*cos(n*time);//the variable that is x increases with respect to the operation.
y+=radius*sin(n*time);//the variable that is x increases with respect to the operation.
wave.unshift(y);//just save the value in y in particular.
stroke(255);//statement of the coup.
noFill();//no circle fill
ellipse(0,0,radius*2);//declaration of the ellipse of coordinates and radius multiplied by 2.
fill(255);//
line(prevx, prevy, radius*2);//
ellipse(x,y,8);//
let x = radius * cos(time);//Variable is declared
let x =(4 / PI) * cos(1*time);//Variable is declared
let y = radius * sin(time);//Variable is declared
let y =(4 / PI) * sin(1*time);//Variable is declared
wave.unshift(y);//
fill(255);//
line(0,0,x,y);//
ellipse(x,y,8);//
translate(200,0);////Moving the object.
line(x - 200, y, 0, wave[0]);//
beginShape();//
noFill();//no circle fill
for (let i = 0; i < wave.length; i++){//cycle declaration
vertex (i, wave[i]);
}
endShape();//is the parther of beginShape() and can only be called after beginShape().
time +=0.05;//
if(wave.length > 500){//
wave.pop();//
}
}
Comentarios
Publicar un comentario