Problem 3-3
This commit is contained in:
@@ -154,26 +154,6 @@
|
||||
ctx.beginPath();
|
||||
ctx.arc(xPos - 20, yPos - (100 - y1), Math.PI * Math.sqrt(m1), 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
/*ctx.beginPath();
|
||||
ctx.arc(xPos + lx / 2, yPos, pulleyR, 0, Math.PI * 2);
|
||||
ctx.stroke();
|
||||
ctx.beginPath();
|
||||
ctx.arc(xPos - lx / 2, yPos, pulleyR, 0, Math.PI * 2);
|
||||
ctx.stroke();
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(xPos + lx / 2 + pulleyR, yPos);
|
||||
ctx.lineTo(xPos + lx / 2 + pulleyR + r * Math.sin(theta), yPos - r * Math.cos(theta));
|
||||
ctx.stroke();
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(xPos - lx / 2 - pulleyR, yPos);
|
||||
ctx.lineTo(xPos - lx / 2 - pulleyR, yPos - (250 - r));
|
||||
ctx.stroke();
|
||||
ctx.beginPath();
|
||||
ctx.arc(xPos + lx / 2 + pulleyR + r * Math.sin(theta), yPos - r * Math.cos(theta), 5, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
ctx.beginPath();
|
||||
ctx.arc(xPos - lx / 2 - pulleyR, yPos - (250 - r), 5, 0, Math.PI * 2);
|
||||
ctx.fill();*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -91,10 +91,10 @@
|
||||
calcAccelerations();
|
||||
//Relative position of canvas
|
||||
var xPos = 0;
|
||||
var yPos = 70;
|
||||
var yPos = 90;
|
||||
//Moves and scales canvas
|
||||
ctx.translate(ctx.canvas.width / 2, ctx.canvas.height / 2)
|
||||
ctx.scale(2, -2);
|
||||
ctx.scale(1.5, -1.5);
|
||||
|
||||
function calcAccelerations()
|
||||
{
|
||||
@@ -144,15 +144,15 @@
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
if (75 - y2 - y1 <= 20)
|
||||
if (100 - y2 - y1 <= 20)
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
if (75 - y3 - y1 <= 20)
|
||||
if (100 - y3 - y1 <= 20)
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
if (75 + y1 <= 35)
|
||||
if (100 + y1 <= 35)
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
@@ -176,52 +176,32 @@
|
||||
ctx.stroke();
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(xPos - 20, yPos);
|
||||
ctx.lineTo(xPos - 20, yPos - (75 - y1));
|
||||
ctx.lineTo(xPos - 20, yPos - (100 - y1));
|
||||
ctx.stroke();
|
||||
ctx.beginPath();
|
||||
ctx.arc(xPos - 20, yPos - (75 - y1), Math.PI * Math.sqrt(m1), 0, Math.PI * 2);
|
||||
ctx.arc(xPos - 20, yPos - (100 - y1), Math.PI * Math.sqrt(m1), 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(xPos + 20, yPos);
|
||||
ctx.lineTo(xPos + 20, yPos - (75 + y1));
|
||||
ctx.lineTo(xPos + 20, yPos - (100 + y1));
|
||||
ctx.stroke();
|
||||
ctx.beginPath();
|
||||
ctx.arc(xPos + 20, yPos - (75 + y1), 20, 0, Math.PI * 2);
|
||||
ctx.arc(xPos + 20, yPos - (100 + y1), 20, 0, Math.PI * 2);
|
||||
ctx.stroke();
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(xPos, yPos - (75 + y1));
|
||||
ctx.lineTo(xPos, yPos - (150 - y2));
|
||||
ctx.moveTo(xPos, yPos - (100 + y1));
|
||||
ctx.lineTo(xPos, yPos - (200 - y2));
|
||||
ctx.stroke();
|
||||
ctx.beginPath();
|
||||
ctx.arc(xPos, yPos - (150 - y2), Math.PI * Math.sqrt(m2), 0, Math.PI * 2);
|
||||
ctx.arc(xPos, yPos - (200 - y2), Math.PI * Math.sqrt(m2), 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(xPos + 40, yPos - (75 + y1));
|
||||
ctx.lineTo(xPos + 40, yPos - (150 - y3));
|
||||
ctx.moveTo(xPos + 40, yPos - (100 + y1));
|
||||
ctx.lineTo(xPos + 40, yPos - (200 - y3));
|
||||
ctx.stroke();
|
||||
ctx.beginPath();
|
||||
ctx.arc(xPos + 40, yPos - (150 - y3), Math.PI * Math.sqrt(m3), 0, Math.PI * 2);
|
||||
ctx.arc(xPos + 40, yPos - (200 - y3), Math.PI * Math.sqrt(m3), 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
/*ctx.beginPath();
|
||||
ctx.arc(xPos + lx / 2, yPos, pulleyR, 0, Math.PI * 2);
|
||||
ctx.stroke();
|
||||
ctx.beginPath();
|
||||
ctx.arc(xPos - lx / 2, yPos, pulleyR, 0, Math.PI * 2);
|
||||
ctx.stroke();
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(xPos + lx / 2 + pulleyR, yPos);
|
||||
ctx.lineTo(xPos + lx / 2 + pulleyR + r * Math.sin(theta), yPos - r * Math.cos(theta));
|
||||
ctx.stroke();
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(xPos - lx / 2 - pulleyR, yPos);
|
||||
ctx.lineTo(xPos - lx / 2 - pulleyR, yPos - (250 - r));
|
||||
ctx.stroke();
|
||||
ctx.beginPath();
|
||||
ctx.arc(xPos + lx / 2 + pulleyR + r * Math.sin(theta), yPos - r * Math.cos(theta), 5, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
ctx.beginPath();
|
||||
ctx.arc(xPos - lx / 2 - pulleyR, yPos - (250 - r), 5, 0, Math.PI * 2);
|
||||
ctx.fill();*/
|
||||
}
|
||||
|
||||
|
||||
@@ -229,7 +209,7 @@
|
||||
c.width = window.innerWidth;
|
||||
c.height = window.innerHeight;
|
||||
ctx.translate(c.width / 2, c.height / 2);
|
||||
ctx.scale(2, -2);
|
||||
ctx.scale(1.5, -1.5);
|
||||
}, true);
|
||||
|
||||
Update();
|
||||
|
||||
139
problem3-3.html
Normal file
139
problem3-3.html
Normal file
@@ -0,0 +1,139 @@
|
||||
<!--
|
||||
This work is licensed under CC BY-NC-ND 4.0
|
||||
Link to license: http://creativecommons.org/licenses/by-nc-nd/4.0/
|
||||
Attribute to Russell Georgi
|
||||
-->
|
||||
<html>
|
||||
<head>
|
||||
<title>
|
||||
Problem 3-3
|
||||
</title>
|
||||
<style>
|
||||
html, body {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
margin: 0px;
|
||||
border: 0;
|
||||
overflow: hidden;
|
||||
display: block;
|
||||
}
|
||||
|
||||
canvas {
|
||||
position: absolute;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<canvas id="myCanvas" width="1" height="1" style="border:1px solid #ffffff;">
|
||||
Your browser does not support the HTML5 canvas tag.</canvas>
|
||||
|
||||
<script>
|
||||
var c = document.getElementById("myCanvas");
|
||||
var ctx = c.getContext("2d");
|
||||
c.width = window.innerWidth;
|
||||
c.height = window.innerHeight;
|
||||
//Time step
|
||||
var dt = 0.002;
|
||||
var t = 0;
|
||||
var g = 200;
|
||||
var n = 10;
|
||||
var positions = new Array(n);
|
||||
var velocities = new Array(n);
|
||||
var accelerations = new Array(n);
|
||||
//Relative position of canvas
|
||||
var xPos = 0;
|
||||
var yPos = 200;
|
||||
var dist = 75;
|
||||
//Moves and scales canvas
|
||||
ctx.translate(ctx.canvas.width / 2, ctx.canvas.height / 2)
|
||||
ctx.scale(1, -1);
|
||||
|
||||
function Reset()
|
||||
{
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
positions[i] = 0;
|
||||
velocities[i] = 0;
|
||||
accelerations[i] = g * (2 * (2 ** i) - 1) / (2 * (2 ** i));
|
||||
}
|
||||
}
|
||||
Reset();
|
||||
|
||||
function Clear(ctx)
|
||||
{
|
||||
ctx.clearRect(-c.width, -c.height, c.width * 2, c.height * 2);
|
||||
}
|
||||
|
||||
function Update()
|
||||
{
|
||||
Clear(ctx);
|
||||
Draw();
|
||||
t += dt;
|
||||
if (t > 0.7)
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
velocities[i] += accelerations[i] * dt;
|
||||
positions[i] += velocities[i] * dt;
|
||||
}
|
||||
setTimeout(Update, 1000/60);
|
||||
}
|
||||
|
||||
function Draw()
|
||||
{
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
if (i == 0)
|
||||
{
|
||||
ctx.beginPath();
|
||||
ctx.arc(xPos + 20 * i, yPos - dist * i, 20, 0, Math.PI * 2);
|
||||
ctx.stroke();
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(xPos + 20 * i - 20, yPos - dist * i);
|
||||
ctx.lineTo(xPos + 20 * i - 20, yPos - dist * i - dist + positions[i]);
|
||||
ctx.stroke();
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(xPos + 20 * i + 20, yPos - dist * i);
|
||||
ctx.lineTo(xPos + 20 * i + 20, yPos - dist * i - dist - positions[i]);
|
||||
ctx.stroke();
|
||||
ctx.beginPath();
|
||||
ctx.arc(xPos + 20 * i - 20, yPos - dist * i - dist + positions[i], 5, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
} else
|
||||
{
|
||||
ctx.beginPath();
|
||||
ctx.arc(xPos + 20 * i, yPos - dist * i - positions[i - 1], 20, 0, Math.PI * 2);
|
||||
ctx.stroke();
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(xPos + 20 * i - 20, yPos - dist * i - positions[i - 1]);
|
||||
ctx.lineTo(xPos + 20 * i - 20, yPos - dist * i - dist - positions[i - 1] + positions[i]);
|
||||
ctx.stroke();
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(xPos + 20 * i + 20, yPos - dist * i - positions[i - 1]);
|
||||
ctx.lineTo(xPos + 20 * i + 20, yPos - dist * i - dist - positions[i]);
|
||||
ctx.stroke();
|
||||
ctx.beginPath();
|
||||
ctx.arc(xPos + 20 * i - 20, yPos - dist * i - dist - positions[i - 1] + positions[i], 5, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
window.addEventListener('resize', function(event) {
|
||||
c.width = window.innerWidth;
|
||||
c.height = window.innerHeight;
|
||||
ctx.translate(c.width / 2, c.height / 2);
|
||||
ctx.scale(1, -1);
|
||||
}, true);
|
||||
|
||||
Update();
|
||||
|
||||
</script>
|
||||
</body>
|
||||
<p xmlns:cc="http://creativecommons.org/ns#" style="font-size: 1vw; bottom: 0px; position: absolute;">
|
||||
This work is licensed under
|
||||
<a href="http://creativecommons.org/licenses/by-nc-nd/4.0/?ref=chooser-v1" target="_blank" rel="license noopener noreferrer" style="display:inline-block;">CC BY-NC-ND 4.0<img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/cc.svg?ref=chooser-v1"><img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/by.svg?ref=chooser-v1"><img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/nc.svg?ref=chooser-v1"><img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/nd.svg?ref=chooser-v1"></a></p>
|
||||
</html>
|
||||
Reference in New Issue
Block a user