Задача Д.24,вариант 5 из Задачника Яблонского
Условие задачи
Определить частоты малых свободных колебаний и формы главных колебаний системы с двумя степенями свободы, пренебрегая силами сопротивления, массами пружин и моментами инерции скручиваемых валов.
= 5 кг; = 2 кг; = 8 Н⁄см; = 6 Н⁄см; = 7 Н⁄см; = 0,5 м.
Программа
Код программы
<syntaxhighlight lang="javascript" line start="1" enclose="div"> <!DOCTYPE html> <head> <script src = "http://tm.spbstu.ru/htmlets/Belousova/Statika/three.js"> </script> <script src ="http://tm.spbstu.ru/htmlets/Belousova/Statika/stats.min.js"> </script> <script src ="http://tm.spbstu.ru/htmlets/Belousova/Statika/OrbitControls.js"> </script> <script src ="http://tm.spbstu.ru/htmlets/Belousova/Statika/dat.gui.js"> </script> <style> body { margin:0; overflow:hidden; } </style> <body>
<canvas id="canvas1"></canvas> <canvas id="canvas2"></canvas> <script> var f1 = 0, f2 = 0;
var renderer, scene, camera, stats, axes; var control, controls, controls1, spotLight; var stick1, stick2, leftBlock, rightBlock, topBlock, botBlock, spring1, spring2, spring3; var dt = 0.01; var g = 9.8;
var m1 = 4; var c1 = 8; var c2 = 6; var c3 = 7; var canvas = document.getElementById('canvas1'); var ctx = canvas.getContext("2d"); ctx.canvas.width = window.innerWidth/2.1; ctx.canvas.height = window.innerHeight/2; canvas = document.getElementById('canvas2'); ctx = canvas.getContext("2d"); ctx.canvas.width = window.innerWidth/2.1; ctx.canvas.height = window.innerHeight/2 var width = ctx.canvas.width; var height = ctx.canvas.height; var x_max = Math.PI/2; var t_max = 5; var x = []; var y = []; var t = []; var i = 1; var q =1; var scale_t = (width-40)/t_max; var scale_x = (height-40)/x_max; x[0]=0; y[0]=0; t[0]=0; function drawAxes() { ctx.beginPath(); ctx.moveTo(20,20); ctx.lineTo(20,height-20); ctx.moveTo(20,height/2); ctx.lineTo(width-20,height/2); ctx.stroke(); }
canvas = document.getElementById('canvas1'); ctx = canvas.getContext("2d"); drawAxes(); canvas = document.getElementById('canvas2'); ctx = canvas.getContext("2d"); drawAxes(); function drawPlot() { canvas = document.getElementById('canvas1'); ctx = canvas.getContext("2d"); if(controls1.t<t_max){ x[i] = f1; t[i] = controls1.t; i++; ctx.clearRect(0, 0, width, height); drawAxes(); ctx.beginPath(); ctx.moveTo(20,height/2); for (var j=0; j<i; j++) { ctx.lineTo(20+scale_t*t[j],height/2-scale_x*x[j]); } ctx.stroke(); } else { for(var j =0;j<i;j++) { x[j]=x[j+1]; t[j]=t[j+1]; } x[i] = f1; t[i] = controls1.t; ctx.clearRect(0, 0, width, height); drawAxes(); ctx.beginPath(); ctx.moveTo(20,height/2); for (var j=0; j<i; j++) { ctx.lineTo(20+scale_t*(t[j]-t[0]),height/2-scale_x*x[j]); } ctx.stroke(); } canvas = document.getElementById('canvas2'); ctx = canvas.getContext("2d"); if(controls1.t<t_max){ y[q] = f2; t[q] = controls1.t; q++; ctx.clearRect(0, 0, width, height); drawAxes(); ctx.beginPath(); ctx.moveTo(20,height/2); for (var j=0; j<q; j++) { ctx.lineTo(20+scale_t*t[j],height/2-scale_x*y[j]); } ctx.stroke(); } else { for(var j =0;j<q;j++) { y[j]=y[j+1]; } y[q] = f2; t[q] = controls1.t; ctx.clearRect(0, 0, width, height); drawAxes(); ctx.beginPath(); ctx.moveTo(20,height/2); for (var j=0; j<q; j++) { ctx.lineTo(20+scale_t*(t[j]-t[0]),height/2-scale_x*y[j]); } ctx.stroke(); } }
function init() { scene = new THREE.Scene(); // создаем сцену camera = new THREE.PerspectiveCamera(45,window.innerWidth/window.innerHeight*2,0.1,1000); // создаем камеру
renderer = new THREE.WebGLRenderer(); renderer.setClearColor(0XEEEEEE,1); renderer.setSize(window.innerWidth,window.innerHeight/2); renderer.shadowMap.enabled=true;
control = new THREE.OrbitControls(camera,renderer.domElement);
controls = new function() // создаем переключатели, позволяющие изменять входные параметры { this.A1 = 0;
this.A2 = 0; this.beta1 = 0; this.beta2 = 0; this.animate = false;
this.reset = function() { controls1.t = 0;
i=1; q=1; x = []; y = []; t = []; x[0]=0; y[0]=0; t[0]=0; ctx.clearRect(0, 0, width, height); drawAxes();
ReDraw(); } }
controls1=new function() // вывод полученных в ходе решения задачи значений { this.t = 0.0; }
var gui = new dat.GUI(); // позволяем менять каждый из параметров в определенном диапазоне, в случае изменения вызываем функцию, перестраивающую выводимую на экран картинку
gui.add(controls,'A1', 0,Math.PI/48).onChange(controls.reset); gui.add(controls,'A2', 0,Math.PI/48).onChange(controls.reset); gui.add(controls,'beta1', -Math.PI/2,Math.PI/2).onChange(controls.reset); gui.add(controls,'beta2', -Math.PI/2,Math.PI/2).onChange(controls.reset); gui.add(controls,'animate');
gui.add(controls, 'reset'); gui.add(controls1, 't').listen();
ambientLight=new THREE.AmbientLight(0x000000); scene.add(ambientLight); document.getElementById("WebGL").appendChild(renderer.domElement); // Camera camera.position.x = 0; camera.position.y = 0; camera.position.z = 40; camera.lookAt(scene.position); // Ligth spotLight = new THREE.SpotLight( 0xffffff ); spotLight.position.set( -40, 80, 50 ); spotLight.castShadow = true; scene.add(spotLight );
//неподвижные объекты
var geometry = new THREE.BoxGeometry( 1, 21, 6 );
var material = new THREE.MeshLambertMaterial( {color: 0xE6FF79} );
var leftBlock = new THREE.Mesh( geometry, material );
leftBlock.position.x = -20;
scene.add(leftBlock);
var rightBlock = new THREE.Mesh( geometry, material );
rightBlock.position.x = 20;
scene.add( rightBlock );
var geometry = new THREE.BoxGeometry( 41, 1, 6 );
var botBlock = new THREE.Mesh( geometry, material );
botBlock.position.y = 10;
scene.add( botBlock );
var topBlock = new THREE.Mesh( geometry, material );
topBlock.position.y = -10;
scene.add( topBlock );
stats = initStats(); Draw(); renderScene();
window.addEventListener('resize',onResize,false); };
function ReDraw() // функция, перерисовывающая всю картинку {
scene.remove(stick1); scene.remove(stick2); scene.remove(spring1); scene.remove(spring2); scene.remove(spring3);
Draw();
drawPlot();
}
function Draw() // рисуем картинку {
//Рычаги var geometry = new THREE.BoxGeometry(1,12,1); var material = new THREE.MeshLambertMaterial( {color: 0xFF6F60} ); stick1 = new THREE.Mesh( geometry, material ); stick1.position.x = 10+Math.sin(f2)*12/2; stick1.position.y = 10-Math.cos(f2)*12/2; stick1.rotation.z = f2; scene.add(stick1); stick2 = new THREE.Mesh( geometry, material ); stick2.position.x = -10-Math.sin(f1)*12/2; stick2.position.y = -10+Math.cos(f1)*12/2; stick2.rotation.z = f1; scene.add(stick2); //пружины var material = new THREE.MeshLambertMaterial( { color: 0x394E5D } ); function CustomSinCurve( scale ) { THREE.Curve.call( this ); this.scale = ( scale === undefined ) ? 1 : scale; } CustomSinCurve.prototype = Object.create( THREE.Curve.prototype ); CustomSinCurve.prototype.constructor = CustomSinCurve; CustomSinCurve.prototype.getPoint = function ( t ) { var tx = -20+(stick2.position.x-Math.sin(f1)*4+20)*t; var ty = (stick2.position.y+Math.cos(f1)*4)*t+Math.sin(c1*15*t)/2; var tz = 0; return new THREE.Vector3( tx, ty, tz ).multiplyScalar( this.scale ); }; var path = new CustomSinCurve( 1 ); var geometry = new THREE.TubeGeometry( path, 256, 0.05, 8, false ); spring1 = new THREE.Mesh( geometry, material ); scene.add( spring1 ); function CustomSinCurve( scale ) { THREE.Curve.call( this ); this.scale = ( scale === undefined ) ? 1 : scale; } CustomSinCurve.prototype = Object.create( THREE.Curve.prototype ); CustomSinCurve.prototype.constructor = CustomSinCurve; CustomSinCurve.prototype.getPoint = function ( t ) { var tx = 20+(stick1.position.x+Math.sin(f2)*4-20)*t; var ty = (stick1.position.y-Math.cos(f2)*4)*t+Math.sin(c2*15*t)/2; var tz = 0; return new THREE.Vector3( tx, ty, tz ).multiplyScalar( this.scale ); }; var path = new CustomSinCurve( 1 ); var geometry = new THREE.TubeGeometry( path, 256, 0.05, 8, false ); spring2 = new THREE.Mesh( geometry, material ); scene.add( spring2 ); function CustomSinCurve( scale ) { THREE.Curve.call( this ); this.scale = ( scale === undefined ) ? 1 : scale; } CustomSinCurve.prototype = Object.create( THREE.Curve.prototype ); CustomSinCurve.prototype.constructor = CustomSinCurve; CustomSinCurve.prototype.getPoint = function ( t ) { var tx = stick1.position.x+Math.sin(f2)*4+(stick2.position.x-Math.sin(f1)*4-stick1.position.x-Math.sin(f2)*4)*t; var ty = stick1.position.y-Math.cos(f2)*4+(stick2.position.y+Math.cos(f1)*4-stick1.position.y+Math.cos(f2)*4)*t+Math.sin(c3*15*t)/2; var tz = 0; return new THREE.Vector3( tx, ty, tz ).multiplyScalar( this.scale ); }; var path = new CustomSinCurve( 1 ); var geometry = new THREE.TubeGeometry( path, 256, 0.05, 8, false ); spring3 = new THREE.Mesh( geometry, material ); scene.add( spring3 );
renderer.render(scene,camera); }
function renderScene() // редерим сцену { if (controls.animate) { controls1.t+=dt;
f1 = controls.A1*Math.sin(15.7*controls1.t+controls.beta1)+controls.A2*Math.sin(40.9*controls1.t+controls.beta2); f2 = 1.05*controls.A1*Math.sin(15.7*controls1.t+controls.beta1)-4.22*controls.A2*Math.sin(40.9*controls1.t+controls.beta2);
ReDraw(); }
stats.update(); requestAnimationFrame(renderScene); renderer.render(scene,camera); };
function initStats() { var stats=new Stats(); stats.setMode(0); stats.domElement.style.position='0px'; stats.domElement.style.left='0px'; stats.domElement.style.top='0px'; document.getElementById("Stats-output").appendChild(stats.domElement); return stats; };
function onResize() { camera.aspect=window.innerWidth/window.innerHeight*2; camera.updateProjectionMatrix(); renderer.setSize(window.innerWidth,window.innerHeight/2);
canvas = document.getElementById('canvas1'); ctx = canvas.getContext("2d"); ctx.canvas.width = window.innerWidth/2.1; ctx.canvas.height = window.innerHeight/2; canvas = document.getElementById('canvas2'); ctx = canvas.getContext("2d"); ctx.canvas.width = window.innerWidth/2.1; ctx.canvas.height = window.innerHeight/2 width = ctx.canvas.width; height = ctx.canvas.height; scale_t = (width-40)/t_max; scale_x = (height-40)/x_max; ctx.clearRect(0,0,width,height); drawAxes();
}
window.onload = init;
</script> </body> </head>
Используемые библиотеки
- three.js
- CurveExtras1.js
- stats.min.js
- dat.gui.js
- OrbitControls.js