Files
ClassicalMechanicsPrograms/4-12.html

271 lines
11 KiB
HTML

<!--
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 4-12
</title>
<style>
html, body {
width: 100%;
height: 100%;
margin: 0px;
border: 0;
overflow: hidden;
display: block;
}
canvas {
position: absolute;
}
.dropbtn {
background-color: #04AA6D;
color: white;
padding: 16px;
font-size: 16px;
border: none;
}
.dropbtnm {
background-color: #04AA6D;
color: white;
padding: 16px;
font-size: 16px;
border: none;
}
.dropdown {
position: absolute;
display: inline-block;
}
.dropdown-content {
display: none;
position: relative;
background-color: #f1f1f1;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content button {
color: black;
padding: 12px 16px;
width: 100px;
text-decoration: none;
display: block;
}
#m {
left: 110px;
}
.dropdown-content a:hover {background-color: #ddd;}
.dropdown:hover .dropdown-content {display: block;}
.dropdown:hover .dropbtn {background-color: #3e8e41;}
</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>
<div class="dropdown">
<button class="dropbtn" id="btn">2 Masses</button>
<div class="dropdown-content">
<button onclick="num(2)">2 Masses</button>
<button onclick="num(3)">3 Masses</button>
<button onclick="num(4)">4 Masses</button>
<button onclick="num(5)">5 Masses</button>
<button onclick="num(6)">6 Masses</button>
<button onclick="num(7)">7 Masses</button>
<button onclick="num(8)">8 Masses</button>
</div>
</div>
<div class="dropdown" id="m">
<button class="dropbtnm" id="btnm">Mode 1</button>
<div class="dropdown-content">
<button onclick="mum(1)" id="btn1">Mode 1</button>
<button onclick="mum(2)" id="btn2">Mode 2</button>
<button onclick="mum(3)" id="btn3">Mode 3</button>
<button onclick="mum(4)" id="btn4">Mode 4</button>
<button onclick="mum(5)" id="btn5">Mode 5</button>
<button onclick="mum(6)" id="btn6">Mode 6</button>
<button onclick="mum(7)" id="btn7">Mode 7</button>
<button onclick="mum(8)" id="btn8">Mode 8</button>
</div>
</div>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
var w = 1;
var amp = 0.8 / n;
var n = 2;
var m = 1;
var t = 0;
var dt = 0.1;
var xPos = 0;
var yPos = 0;
function num(nn)
{
n = nn;
document.getElementById("btn").textContent = n.toString() + " Masses";
if (m > n)
{
m = n;
document.getElementById("btnm").textContent = "Mode " + m.toString();
}
}
function mum(mm)
{
m = mm;
document.getElementById("btnm").textContent = "Mode " + m.toString();
}
ctx.translate(ctx.canvas.width / 2, ctx.canvas.height / 2);
ctx.scale(1.5, 1.5);
function Clear(ctx)
{
ctx.clearRect(-c.width, -c.height, c.width * 2, c.height * 2);
}
function Update()
{
Clear(ctx);
Draw();
t += dt;
amp = 0.8 / n;
for (i = 1; i <= n; i++)
{
document.getElementById("btn" + i.toString()).style.visibility = "visible";
}
for (i = n + 1; i <= 8; i++)
{
document.getElementById("btn" + i.toString()).style.visibility = "hidden";
}
setTimeout(Update, 1000/60)
}
function Draw()
{
ctx.beginPath();
ctx.arc(xPos, yPos, 100, 0, Math.PI * 2);
ctx.stroke();
if (m % 2 == 0)
{
mn = Math.floor(m / 2);
console.log("cos" + mn.toString());
for (i = 0; i < n; i++)
{
if (2 * w * Math.sin(Math.PI * mn / n) > 0.0001)
{
var theta = Math.PI * 2 * i / n;
theta += Math.cos(i * 2 * Math.PI * mn / n) * amp * Math.cos(2 * w * Math.sin(Math.PI * mn / n) * t);
var theta2 = Math.PI * 2 * (i + 1) / n;
theta2 += Math.cos((i+1) * 2 * Math.PI * mn / n) * amp * Math.cos(2 * w * Math.sin(Math.PI * mn / n) * t);
ctx.beginPath();
ctx.arc(xPos + 100 * Math.cos(theta), yPos + 100 * Math.sin(theta), 10, 0, Math.PI * 2);
ctx.fill();
connectTwoTheta(theta, theta2);
} else
{
var theta = Math.PI * 2 * i / n;
theta += Math.cos(i * 2 * Math.PI * mn / n) * amp * w * t;
var theta2 = Math.PI * 2 * (i + 1) / n;
theta2 += Math.cos((i+1) * 2 * Math.PI * mn / n) * amp * w * t;
ctx.beginPath();
ctx.arc(xPos + 100 * Math.cos(theta), yPos + 100 * Math.sin(theta), 10, 0, Math.PI * 2);
ctx.fill();
connectTwoTheta(theta, theta2);
}
}
} else
{
mn = Math.floor(m / 2);
console.log("sin" + mn.toString());
for (i = 0; i < n; i++)
{
if (2 * w * Math.sin(Math.PI * mn / n) > 0.0001)
{
var theta = Math.PI * 2 * i / n;
theta += Math.sin(i * 2 * Math.PI * mn / n) * amp * Math.cos(2 * w * Math.sin(Math.PI * mn / n) * t);
var theta2 = Math.PI * 2 * (i + 1) / n;
theta2 += Math.sin((i+1) * 2 * Math.PI * mn / n) * amp * Math.cos(2 * w * Math.sin(Math.PI * mn / n) * t);
ctx.beginPath();
ctx.arc(xPos + 100 * Math.cos(theta), yPos + 100 * Math.sin(theta), 10, 0, Math.PI * 2);
ctx.fill();
connectTwoTheta(theta, theta2);
} else
{
var theta = Math.PI * 2 * i / n;
theta += Math.cos(i * 2 * Math.PI * mn / n) * amp * w * t;
var theta2 = Math.PI * 2 * (i + 1) / n;
theta2 += Math.cos((i+1) * 2 * Math.PI * mn / n) * amp * w * t;
ctx.beginPath();
ctx.arc(xPos + 100 * Math.cos(theta), yPos + 100 * Math.sin(theta), 10, 0, Math.PI * 2);
ctx.fill();
connectTwoTheta(theta, theta2);
}
}
}
}
function connectTwoTheta(theta1, theta2)
{
if (theta1 > theta2)
{
theta2 += Math.PI * 2;
}
drawSpring(1, 3, Math.abs(theta1 - theta2), -10, Math.ceil(50 / n), theta1, 105, 0.1, xPos, yPos);
}
function drawSpring(a, b, tSize, rSize, numLoops, t, r, pStep, x, y)
{
var pStart = 0;
var pEnd = (3 * Math.PI / 2) + (2 * Math.PI * numLoops);
var tEnd = a * pEnd - b * Math.sin(pEnd);
var rEnd = a + b;
var tFactor = tSize / tEnd;
var rFactor = rSize / rEnd;
var tShift = t /*- a * (Math.PI / 2) + b * Math.sin(Math.PI / 2) - Math.PI/ 2*/;
var rShift = r - a + b * Math.cos(Math.PI / 2);
for (p = pStart; p <= pEnd; p += pStep)
{
var theta = tShift + tFactor * (a * p - b * Math.sin(p));
var radius = rShift + rFactor * (a - b * Math.cos(p));
ctx.beginPath();
ctx.moveTo(x + radius * Math.cos(theta), y + radius * Math.sin(theta));
theta = tShift + tFactor * (a * (p + pStep) - b * Math.sin((p + pStep)));
radius = rShift + rFactor * (a - b * Math.cos(p + pStep));
ctx.lineTo(x + radius * Math.cos(theta), y + radius * Math.sin(theta));
ctx.stroke();
}
}
window.addEventListener('resize', function(event) {
c.width = window.innerWidth;
c.height = window.innerHeight;
ctx.translate(c.width / 2, c.height / 2);
}, 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>