Files
ClassicalMechanicsPrograms/5-29.html
2021-09-28 18:48:30 -07:00

206 lines
7.5 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 5-29
</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>
<script>
class Vector2 {
constructor (x, y) {
this.x = x;
this.y = y;
}
scale(n)
{
this.x *= n;
this.y *= n;
}
get len()
{
return this.calcLen();
}
calcLen()
{
return (Math.sqrt(this.x * this.x + this.y * this.y));
}
}
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
var l = 100;
var r = 50;
var ppl = 2.0;
var g = 10;
var n = 500;
//var f = 1;
var chainPositions = new Array(1);
var chainPrevPositions = new Array(1);
var t = 0;
var dt = 0.0125;
var xPos = 0;
var yPos = 100;
ctx.translate(ctx.canvas.width / 2, ctx.canvas.height / 2);
ctx.scale(1.5, -1.5);
chainPositions[0] = new Vector2(r, 0);
chainPrevPositions[0] = new Vector2(r, 0);
for (i = ppl; i < r * Math.PI; i += ppl)
{
chainPositions.push(new Vector2(r * Math.cos(i / r), -r * Math.sin(i / r)));
chainPrevPositions.push(new Vector2(r * Math.cos(i / r), -r * Math.sin(i / r)));
}
for (i = 0; i < l; i += ppl)
{
chainPositions.push(new Vector2(-r, i));
chainPrevPositions.push(new Vector2(-r, i));
}
function Clear(ctx)
{
ctx.clearRect(-c.width, -c.height, c.width * 2, c.height * 2);
}
function Update()
{
Clear(ctx);
Draw();
t += dt;
for (i = 1; i < chainPositions.length; i++)
{
positionBeforeUpdate = chainPositions[i];
chainPositions[i] = new Vector2(2 * chainPositions[i].x - chainPrevPositions[i].x, 2 * chainPositions[i].y - chainPrevPositions[i].y);
chainPositions[i].y -= g * dt * dt;
chainPrevPositions[i] = positionBeforeUpdate;
/*stickCentre = new Vector2((chainPositions[i - 1].x + chainPositions[i].x) / 2, (chainPositions[i - 1].y + chainPositions[i].y) / 2);
stickDir = new Vector2(chainPositions[i - 1].x - chainPositions[i].x, chainPositions[i - 1].y - chainPositions[i].y);
stickDir = new Vector2(stickDir.x / stickDir.calcLen(), stickDir.y / stickDir.calcLen());
if (i != 1)
{
chainPositions[i - 1] = new Vector2(stickCentre.x + stickDir.x * ppl / 2, stickCentre.y + stickDir.y * ppl / 2);
}
chainPositions[i] = new Vector2(stickCentre.x - stickDir.x * ppl / 2, stickCentre.y - stickDir.y * ppl / 2);*/
}
for (j = 0; j < n; j++)
{
//i = Math.floor(Math.random()*(chainPositions.length - 1));
i = j % (chainPositions.length - 1);
stickCentre = new Vector2((chainPositions[i].x + chainPositions[i + 1].x) / 2, (chainPositions[i].y + chainPositions[i + 1].y) / 2);
stickDir = new Vector2(chainPositions[i].x - chainPositions[i + 1].x, chainPositions[i].y - chainPositions[i + 1].y);
stickDir = new Vector2(stickDir.x / stickDir.calcLen(), stickDir.y / stickDir.calcLen());
if (i != 0)
{
chainPositions[i] = new Vector2(stickCentre.x + stickDir.x * ppl / 2, stickCentre.y + stickDir.y * ppl / 2);
}
chainPositions[i + 1] = new Vector2(stickCentre.x - stickDir.x * ppl / 2, stickCentre.y - stickDir.y * ppl / 2);
}
setTimeout(Update, 500/60);
}
function Draw()
{
ctx.beginPath();
ctx.moveTo(chainPositions[0].x + xPos, chainPositions[0].y + yPos);
for (i = 1; i < chainPositions.length; i++)
{
ctx.lineTo(chainPositions[i].x + xPos, chainPositions[i].y + yPos);
}
ctx.stroke();
}
function vectorAdd(v1, v2)
{
return new Vector2(v1.x + v2.x, v1.y + v2.y);
}
window.addEventListener('resize', function(event) {
c.width = window.innerWidth;
c.height = window.innerHeight;
ctx.translate(c.width / 2, c.height / 2);
ctx.scale(1.5, -1.5);
}, 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>