Ws-htmlets/js2020/Galov$Shilov/html.html
Описание
Движение частицы в гравитационном и электромагнитном полях
Исполнители:Галов Владислав,Шилов Михаил,Борисенков Богдан
Визуализация
Код программы
window.addEventListener("load", main, false); function main(){ var ctx = canv.getContext('2d'); var h = canv.height; var w = canv.width;
var E = 10; var Mod = 100000000; var Ma1 = 100000000; var Ma2 = 50000000; var radod = 100; var rad1 = 100; var rad2 = 50; var rad = 10; var x; var y; var m; var vx = 0; var vy = 0; var aex = 0; var aey = 0; var atsx = 0; var atsy = 0; var axg = 0; var ayg = 0; var Flx = 0; var Fly = 0; var xo; var yo;
var fps = 60; var dt = 1/fps;
canv.onclick = function(event){ dt = 1/fps; var rect = canv.getBoundingClientRect(); x = event.clientX - rect.left; y = event.clientY - rect.top; }
function phys1(){ m = document.getElementById('polz').value; var radf = document.getElementsByName('field'); var radn = document.getElementsByName('kolvo'); var rads = document.getElementsByName('sput') var radd = document.getElementsByName('direct'); var radc = document.getElementsByName('ch'); if (radf[0].checked==true){ if (rads[1].checked==true){ if (radn[0].checked==true){ var rod = Math.sqrt((x-250)*(x-250)+(y-250)*(y-250)); var cosod = (250-x)/rod; var sinod = (250-y)/rod; var fg = m*Mod/(rod*rod); var fgx = fg*cosod; var fgy = fg*sinod; axg = fgx/m; ayg = fgy/m; x = x+3*axg*dt*dt/2; y = y+3*ayg*dt*dt/2; } if (radn[1].checked==true){ var r1 = Math.sqrt((x-350)*(x-350)+(y-350)*(y-350)); var r2 = Math.sqrt((x-100)*(x-100)+(y-100)*(y-100)); var cos1 = (350-x)/r1; var cos2 = (100-x)/r2; var sin1 = (350-y)/r1; var sin2 = (100-y)/r2; var fg1 = m*Ma1/(r1*r1); var fg2 = m*Ma2/(r2*r2); var fg1x = fg1*cos1; var fg2x = fg2*cos2; var fg1y = fg1*sin1; var fg2y = fg2*sin2; axg = (fg1x+fg2x)/m; ayg = (fg1y+fg2y)/m; x = x+3*axg*dt*dt/2; y = y+3*ayg*dt*dt/2; if (r1<rad1+rad)dt=0; if (r2<rad2+rad)dt=0; } } if (rads[0].checked==true){ if (radn[0].checked==true){ var rsod = Math.sqrt((x-250)*(x-250)+(y-250)*(y-250)); var cossod = (250-x)/rsod; var sinsod = (250-y)/rsod; var v = Math.sqrt(0.1*Mod/rsod); vx = v*sinsod; vy = v*(-cossod); ac = v*v/rsod; acx = ac*cossod; acy = ac*sinsod; x = x+vx*dt+acx*dt*dt/2; y = y+vy*dt+acy*dt*dt/2; } if (radn[1].checked==true){ var rs1 = Math.sqrt((x-350)*(x-350)+(y-350)*(y-350)); var rs2 = Math.sqrt((x-100)*(x-100)+(y-100)*(y-100)); if (rs1<=rs2+70){ var coss1 = (350-x)/rs1; var sins1 = (350-y)/rs1; var v = Math.sqrt(0.1*Ma1/rs1); vx = v*sins1; vy = v*(-coss1); ac = v*v/rs1; acx = ac*coss1; acy = ac*sins1; x = x+vx*dt+acx*dt*dt/2; y = y+vy*dt+acy*dt*dt/2; } else{ var coss2 = (100-x)/rs2; var sins2 = (100-y)/rs2; var v = Math.sqrt(0.1*Ma2/rs2); vx = -v*sins2; vy = v*coss2; ac = v*v/rs2; acx = ac*coss2; acy = ac*sins2; x = x+vx*dt+acx*dt*dt/2; y = y+vy*dt+acy*dt*dt/2; } } } } if (radf[1].checked==true){ if (radd[0].checked==true && radc[0].checked==true || radd[1].checked==true && radc[1].checked==true){ atsy = vx/m; atsx = (-vy)/m; } else{ atsy = (-vx)/m; atsx = vy/m; } var radd2 = document.getElementsByName('direct2'); if (radd2[0].checked==true){aex = E/m;} if (radd2[1].checked==true){aex = -E/m;} x = x+vx*dt+(aex+atsx)*dt*dt/2; y = y+vy*dt+atsy*dt*dt/2; vx = vx+(aex+atsx)*dt; vy = vy+atsy*dt; if (x<-10 || y<-10 || x>w+10 || y>h+10) {vx = 0; vy = 0; atsx = 0; atsy = 0;}; } }
function draw1(){ ctx.clearRect(0,0,500,500); ctx.lineWidth="1"; var radf = document.getElementsByName('field'); var radn = document.getElementsByName('kolvo'); var radd = document.getElementsByName('direct'); var radc = document.getElementsByName('ch'); if (radf[0].checked==true){ if (radn[0].checked==true){ ctx.beginPath(); ctx.arc(250,250,radod,0,2*Math.PI); ctx.stroke(); ctx.fillStyle="grey"; ctx.fill(); var rod = Math.sqrt((x-250)*(x-250)+(y-250)*(y-250)); if (rod<=radod+rad){ var sinod = Math.abs(y-250)/rod; var cosod = Math.abs(x-250)/rod; if (x>250){x = 250+(radod+rad)*cosod;} else{x = 250-(radod+rad)*cosod;} if (y>250){y = 250+(radod+rad)*sinod;} else{y = 250-(radod+rad)*sinod;} } ctx.beginPath(); ctx.arc(x,y,rad,0,2*Math.PI); ctx.stroke(); ctx.fillStyle="black"; ctx.fill(); } if (radn[1].checked==true){ ctx.beginPath(); ctx.arc(350,350,rad1,0,2*Math.PI); ctx.stroke(); ctx.fillStyle="brown"; ctx.fill(); ctx.beginPath(); ctx.arc(100,100,rad2,0,2*Math.PI); ctx.stroke(); ctx.fillStyle="purple"; ctx.fill(); var r1 = Math.sqrt((x-350)*(x-350)+(y-350)*(y-350)); var r2 = Math.sqrt((x-100)*(x-100)+(y-100)*(y-100)); if (r1<=rad1+rad){ var sin1 = Math.abs(y-350)/r1; var cos1 = Math.abs(x-350)/r1; if (x>350){x = 350+(rad1+rad)*cos1;} else{x = 350-(rad1+rad)*cos1;} if (y>350){y = 350+(rad1+rad)*sin1;} else{y = 350-(rad1+rad)*sin1;} } if (r2<=rad2+rad){ var sin2 = Math.abs(y-100)/r2; var cos2 = Math.abs(x-100)/r2; if (x>100){x = 100+(rad2+rad)*cos2;} else{x = 100-(rad2+rad)*cos2;} if (y>100){y = 100+(rad2+rad)*sin2;} else{y = 100-(rad2+rad)*sin2;} } ctx.beginPath(); ctx.arc(x,y,rad,0,2*Math.PI); ctx.stroke(); ctx.fillStyle="black"; ctx.fill(); } } if (radf[1].checked==true){ ctx.beginPath(); ctx.arc(x,y,rad,0,2*Math.PI); ctx.stroke(); ctx.fillStyle="black"; ctx.fill(); ctx.beginPath(); ctx.arc(30,30,15,0,2*Math.PI); ctx.strokeStyle="grey"; ctx.stroke(); if (radd[1].checked==true){ ctx.beginPath(); ctx.moveTo(19.4,19.4); ctx.lineTo(40.6,40.6); ctx.stroke(); ctx.beginPath(); ctx.moveTo(19.4,40.6); ctx.lineTo(40.6,19.4); ctx.stroke(); } else{ ctx.beginPath(); ctx.arc(30,30,1,0,2*Math.PI); ctx.fillStyle="grey"; ctx.fill(); ctx.stroke(); } ctx.beginPath(); ctx.moveTo(x-5,y); ctx.lineTo(x+5,y); if (radc[0].checked==true){ ctx.moveTo(x,y-5); ctx.lineTo(x,y+5); } ctx.strokeStyle="white"; ctx.lineWidth="3"; ctx.stroke(); } }
function control(){ phys1(); draw1(); }
var radf = document.getElementsByName('field'); if (radf[0].checked==true){setInterval(control, 100/fps);} if (radf[1].checked==true){setInterval(control, 0.1/fps);}
}