xxxxxxxxxx
185
let waves = [];
let waves1;
let leverPoint = {
x: 0,
y: 0
};
let inPointLoc = {
x: 0,
y: 0
}
let outPointLoc = {
x: 0,
y: 0
}
let widthSeparation = 100;
let leftWaveWavelength = 5;
let dotLocation = 0;
let ballSpeed = 0.5;
function setup() {
createCanvas(1200, 200);
leverPoint.x = width / 2;
leverPoint.y = height / 2;
//set inpoint location x
inPointLoc.x = width / 2 - widthSeparation;
//set out point location x
outPointLoc.x = width / 2 + widthSeparation;
ellipseMode(CENTER);
}
function draw() {
background(200);
//Draw the left sine wave part
drawSinWave();
//Draw the middle lever part
separationLine(widthSeparation);
drawLever(leverPoint.x, 5);
//Draw the moving Point
drawPoint(dotLocation);
drawLineToLever();
//Draw balance
drawLeftPoint();
drawBalance();
drawOutPoint()
//Draw the right amplified part
}
//middle lever part function
function separationLine(s) {
stroke(0);
line(width / 2 - s, 0, width / 2 - s, height);
line(width / 2 + s, 0, width / 2 + s, height);
}
function drawLever(centre, separation) {
stroke(0);
fill(0);
triangle(centre, leverPoint.y, centre + separation, height, centre - separation, height);
}
function mouseDragged() {
if (mouseY > leverPoint.y) {
if (mouseX < width / 2 + widthSeparation && mouseX > width / 2 - widthSeparation) {
leverPoint.x = mouseX;
}
}
}
//left sin wave part function
function drawSinWave() {
stroke(255);
for (let i = 0; i < width / 2 - widthSeparation; i++) {
// point(i, theWaveProcess(i, leftWaveWavelength));
if (i < width / 2 - widthSeparation - 1) {
line(i, theWaveProcess(i), i + 1, theWaveProcess(i + 1));
}
}
}
function theWaveProcess(inputI) {
return height / 2 + cos(inputI / leftWaveWavelength) * 20;
}
function drawPoint(locationX) {
noStroke();
ellipse(locationX, theWaveProcess(locationX), 5);
dotLocation += ballSpeed;
if (dotLocation >= width / 2 - widthSeparation) {
dotLocation = 0;
}
}
function drawLineToLever() {
stroke(0);
line(dotLocation, theWaveProcess(dotLocation), width / 2 - widthSeparation, theWaveProcess(dotLocation));
}
function drawLeftPoint() {
inPointLoc.y = theWaveProcess(dotLocation);
rect(inPointLoc.x, inPointLoc.y, 5, 5);
}
function drawBalance() {
outPointLoc.y = findOutPointY();
line(inPointLoc.x, inPointLoc.y, outPointLoc.x, outPointLoc.y);
//line(inPointLoc.x,inPointLoc.y,leverPoint.x,leverPoint.y);
}
function findOutPointY() {
let outPointY;
outPointY = ((inPointLoc.y - leverPoint.y) / (inPointLoc.x - leverPoint.x) * (outPointLoc.x - leverPoint.x) + height / 2);
return outPointY;
}
function drawOutPoint() {
rect(outPointLoc.x, outPointLoc.y, 5, 5);
}
class Wave {
constructor(x, y) {
this.x = x;
this.y = y
}
finished() {
if (x > width) {
return true;
} else {
false;
}
}
show() {
rect(this.x, this.y, 50, 50);
}
move() {
this.x++;
}
}