4-9 and 4-11
This commit is contained in:
176
4-11.html
Normal file
176
4-11.html
Normal file
@@ -0,0 +1,176 @@
|
||||
<!--
|
||||
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-11
|
||||
</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;
|
||||
}
|
||||
|
||||
.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;
|
||||
}
|
||||
|
||||
.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">Mode 1</button>
|
||||
<div class="dropdown-content">
|
||||
<button onclick="num(0)">Mode 1</button>
|
||||
<button onclick="num(1)">Mode 2</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 wd = 1;
|
||||
var fd = 20;
|
||||
var m = 1;
|
||||
var t = 0;
|
||||
var dt = 0.1;
|
||||
var xPos = 0;
|
||||
var yPos = 0;
|
||||
|
||||
|
||||
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;
|
||||
|
||||
setTimeout(Update, 1000/60)
|
||||
}
|
||||
|
||||
function Draw()
|
||||
{
|
||||
ctx.beginPath();
|
||||
ctx.arc(xPos, yPos, 100, 0, Math.PI * 2);
|
||||
ctx.stroke();
|
||||
var x1 = -fd * (2 * w * w - wd * wd) * Math.cos(wd * t) /
|
||||
(wd * wd * (4 * w * w - wd * wd) * m);
|
||||
var y1 = Math.sqrt(10000 - x1 * x1);
|
||||
var t1 = Math.atan(y1 / x1);
|
||||
var x2 = -fd * 2 * Math.cos(wd * t) /
|
||||
(wd * wd * (4 * w * w - wd * wd) * m);
|
||||
var y2 = -Math.sqrt(10000 - x2 * x2);
|
||||
var t2 = Math.atan(y2 / x2);
|
||||
|
||||
if (x1 < 0 || y1 < 0)
|
||||
{
|
||||
t1 += Math.PI;
|
||||
}
|
||||
if (x2 < 0)
|
||||
{
|
||||
t2 -= Math.PI;
|
||||
}
|
||||
ctx.beginPath();
|
||||
ctx.arc(xPos + x1, yPos + y1, 10, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
ctx.beginPath();
|
||||
ctx.arc(xPos + x2, yPos + y2, 10, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
drawSpring(1, 3, Math.abs(t2 - t1), -10, 20, Math.min(t1, t2) - Math.PI / 2 + 0.2, 105, 0.1, xPos, yPos);
|
||||
drawSpring(1, 3, Math.PI * 2 - Math.abs(t2 - t1) -0.1, -10, 20, Math.max(t1, t2) - Math.PI / 2 + 0.2, 105, 0.1, xPos, yPos);
|
||||
}
|
||||
|
||||
function drawSpring(a, b, tSize, rSize, numLoops, t, r, pStep, x, y)
|
||||
{
|
||||
var pStart = Math.PI / 2;
|
||||
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);
|
||||
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);
|
||||
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>
|
||||
172
4-9.html
Normal file
172
4-9.html
Normal file
@@ -0,0 +1,172 @@
|
||||
<!--
|
||||
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-9
|
||||
</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;
|
||||
}
|
||||
|
||||
.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;
|
||||
}
|
||||
|
||||
.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">Mode 1</button>
|
||||
<div class="dropdown-content">
|
||||
<button onclick="num(0)">Mode 1</button>
|
||||
<button onclick="num(1)">Mode 2</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 = 20;
|
||||
var mode = 0;
|
||||
var t = 0;
|
||||
var dt = 0.1;
|
||||
var xPos = 0;
|
||||
var yPos = 0;
|
||||
|
||||
function num(n)
|
||||
{
|
||||
mode = n;
|
||||
}
|
||||
|
||||
|
||||
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()
|
||||
{
|
||||
Draw();
|
||||
t += dt;
|
||||
setTimeout(Update, 1000/60)
|
||||
}
|
||||
|
||||
function Draw()
|
||||
{
|
||||
if (mode == 0)
|
||||
{
|
||||
Clear(ctx);
|
||||
ctx.beginPath();
|
||||
ctx.arc(xPos + 50 + amp * Math.sin(w * t), yPos, 5, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
ctx.beginPath();
|
||||
ctx.arc(xPos - 50 + amp * (Math.sqrt(3) - 1) * Math.sin(w * t) / 2, yPos, 5 * Math.sqrt(2), 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
drawSpring(1, 3, 100 + amp * (Math.sqrt(3) - 1) * Math.sin(w * t) / 2, 10, 5, xPos - 150, yPos, 0.1, 0);
|
||||
drawSpring(1, 3, 100 - amp * (Math.sqrt(3) - 1) * Math.sin(w * t) / 2+ amp * Math.sin(w * t), 10, 5, xPos - 50 + amp * (Math.sqrt(3) - 1) * Math.sin(w * t) / 2, yPos, 0.1, 0);
|
||||
drawSpring(1, 3, 100 - amp * Math.sin(w * t) , 10, 5, xPos + 50 + amp * Math.sin(w * t), yPos, 0.1, 0);
|
||||
} else if (mode == 1)
|
||||
{
|
||||
Clear(ctx);
|
||||
ctx.beginPath();
|
||||
ctx.arc(xPos + 50 + amp * Math.sin(w * t), yPos, 5, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
ctx.beginPath();
|
||||
ctx.arc(xPos - 50 + amp * (-Math.sqrt(3) - 1) * Math.sin(w * t) / 2, yPos, 5 * Math.sqrt(2), 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
drawSpring(1, 3, 100 + amp * (-Math.sqrt(3) - 1) * Math.sin(w * t) / 2, 10, 5, xPos - 150, yPos, 0.1, 0);
|
||||
drawSpring(1, 3, 100 - amp * (-Math.sqrt(3) - 1) * Math.sin(w * t) / 2+ amp * Math.sin(w * t), 10, 5, xPos - 50 + amp * (-Math.sqrt(3) - 1) * Math.sin(w * t) / 2, yPos, 0.1, 0);
|
||||
drawSpring(1, 3, 100 - amp * Math.sin(w * t) , 10, 5, xPos + 50 + amp * Math.sin(w * t), yPos, 0.1, 0);
|
||||
}
|
||||
}
|
||||
|
||||
function drawSpring(a, b, tSize, rSize, numLoops, x, y, tStep, deltaY)
|
||||
{
|
||||
var tStart = Math.PI / 2;
|
||||
var tEnd = (3 * Math.PI / 2) + (2 * Math.PI * numLoops);
|
||||
var xEnd = a * tEnd - b * Math.sin(tEnd);
|
||||
var yEnd = a + b;
|
||||
var xFactor = xSize / xEnd;
|
||||
var yFactor = ySize / yEnd;
|
||||
var xShift = x - a * (Math.PI / 2) + b * Math.sin(Math.PI / 2);
|
||||
var yShift = y - a + b * Math.cos(Math.PI / 2);
|
||||
var slope = deltaY / xSize;
|
||||
for (p = tStart; p <= tEnd; p += tStep)
|
||||
{
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(xShift + xFactor * (a * p - b * Math.sin(p)), yShift + yFactor * (a - b * Math.cos(p)) + slope * (a * p - b * Math.sin(p)));
|
||||
ctx.lineTo(xShift + xFactor * (a * (p + tStep) - b * Math.sin(p + tStep)), yShift + yFactor * (a - b * Math.cos(p + tStep)));
|
||||
ctx.stroke();
|
||||
}
|
||||
}
|
||||
|
||||
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>
|
||||
Reference in New Issue
Block a user