Ws-htmlets/js2020/Galov$Shilov/html.html

Материал из Department of Theoretical and Applied Mechanics
Перейти к: навигация, поиск

Описание

Движение частицы в гравитационном и электромагнитном полях

Исполнители:Галов Владислав,Шилов Михаил,Борисенков Богдан

Визуализация

Код программы

Код программы на языке JavaScript:
  1 window.addEventListener("load", main, false);
  2 function main(){
  3 	var ctx = canv.getContext('2d');
  4 	var h = canv.height;
  5 	var w = canv.width;
  6 	
  7 	var E = 10; var Mod = 100000000; var Ma1 = 100000000; var Ma2 = 50000000;
  8 	var radod = 100;
  9 	var rad1 = 100; var rad2 = 50;
 10 	var rad = 10;
 11 	var x; var y; var m;
 12 	var vx = 0; var vy = 0; var aex = 0; var aey = 0; var atsx = 0; var atsy = 0;
 13 	var axg = 0; var ayg = 0;
 14 	var Flx = 0; var Fly = 0; var xo; var yo;
 15 	
 16 	var fps = 60;
 17 	var dt = 1/fps;
 18 	
 19 	canv.onclick = function(event){
 20 		dt = 1/fps;
 21 		var rect = canv.getBoundingClientRect();
 22 		x = event.clientX - rect.left;
 23 		y = event.clientY - rect.top;
 24 	}
 25 	
 26 	function phys1(){
 27 		m = document.getElementById('polz').value;
 28 		var radf = document.getElementsByName('field');
 29 		var radn = document.getElementsByName('kolvo');
 30 		var rads = document.getElementsByName('sput')
 31 		var radd = document.getElementsByName('direct');
 32 		var radc = document.getElementsByName('ch');
 33 		if (radf[0].checked==true){
 34 		if (rads[1].checked==true){
 35 			if (radn[0].checked==true){
 36 				var rod = Math.sqrt((x-250)*(x-250)+(y-250)*(y-250));
 37 				var cosod = (250-x)/rod;
 38 				var sinod = (250-y)/rod;
 39 				var fg = m*Mod/(rod*rod);
 40 				var fgx = fg*cosod; var fgy = fg*sinod;
 41 				axg = fgx/m; ayg = fgy/m;
 42 				x = x+3*axg*dt*dt/2;
 43 				y = y+3*ayg*dt*dt/2;
 44 			}
 45 			if (radn[1].checked==true){
 46 				var r1 = Math.sqrt((x-350)*(x-350)+(y-350)*(y-350));
 47 				var r2 = Math.sqrt((x-100)*(x-100)+(y-100)*(y-100));
 48 				var cos1 = (350-x)/r1;
 49 				var cos2 = (100-x)/r2;
 50 				var sin1 = (350-y)/r1;
 51 				var sin2 = (100-y)/r2;
 52 				var fg1 = m*Ma1/(r1*r1);
 53 				var fg2 = m*Ma2/(r2*r2);
 54 				var fg1x = fg1*cos1; var fg2x = fg2*cos2;
 55 				var fg1y = fg1*sin1; var fg2y = fg2*sin2;
 56 				axg = (fg1x+fg2x)/m; ayg = (fg1y+fg2y)/m;
 57 				x = x+3*axg*dt*dt/2;
 58 				y = y+3*ayg*dt*dt/2;
 59 				if (r1<rad1+rad)dt=0;
 60 				if (r2<rad2+rad)dt=0;
 61 				}
 62 		}
 63 		if (rads[0].checked==true){
 64 			if (radn[0].checked==true){
 65 				var rsod = Math.sqrt((x-250)*(x-250)+(y-250)*(y-250));
 66 				var cossod = (250-x)/rsod;
 67 				var sinsod = (250-y)/rsod;
 68 				var v = Math.sqrt(0.1*Mod/rsod);
 69 				vx = v*sinsod; vy = v*(-cossod);
 70 				ac = v*v/rsod;
 71 				acx = ac*cossod; acy = ac*sinsod;
 72 				x = x+vx*dt+acx*dt*dt/2;
 73 				y = y+vy*dt+acy*dt*dt/2;
 74 			}
 75 			if (radn[1].checked==true){
 76 				var rs1 = Math.sqrt((x-350)*(x-350)+(y-350)*(y-350));
 77 				var rs2 = Math.sqrt((x-100)*(x-100)+(y-100)*(y-100));
 78 				if (rs1<=rs2+70){
 79 					var coss1 = (350-x)/rs1;
 80 					var sins1 = (350-y)/rs1;
 81 					var v = Math.sqrt(0.1*Ma1/rs1);
 82 					vx = v*sins1; vy = v*(-coss1);
 83 					ac = v*v/rs1;
 84 					acx = ac*coss1; acy = ac*sins1;
 85 					x = x+vx*dt+acx*dt*dt/2;
 86 					y = y+vy*dt+acy*dt*dt/2;
 87 				}
 88 				else{
 89 					var coss2 = (100-x)/rs2;
 90 					var sins2 = (100-y)/rs2;
 91 					var v = Math.sqrt(0.1*Ma2/rs2);
 92 					vx = -v*sins2; vy = v*coss2;
 93 					ac = v*v/rs2;
 94 					acx = ac*coss2; acy = ac*sins2;
 95 					x = x+vx*dt+acx*dt*dt/2;
 96 					y = y+vy*dt+acy*dt*dt/2;
 97 				}
 98 			}
 99 		}
100 		}
101 		if (radf[1].checked==true){
102 			if (radd[0].checked==true && radc[0].checked==true ||
103 			radd[1].checked==true && radc[1].checked==true){
104 				atsy = vx/m;
105 				atsx = (-vy)/m;
106 			}
107 			else{
108 				atsy = (-vx)/m;
109 				atsx = vy/m;
110 			}
111 			var radd2 = document.getElementsByName('direct2');
112 			if (radd2[0].checked==true){aex = E/m;}
113 			if (radd2[1].checked==true){aex = -E/m;}
114 			x = x+vx*dt+(aex+atsx)*dt*dt/2;
115 			y = y+vy*dt+atsy*dt*dt/2;
116 			vx = vx+(aex+atsx)*dt;
117 			vy = vy+atsy*dt;
118 			if (x<-10 || y<-10 || x>w+10 || y>h+10) {vx = 0; vy = 0; atsx = 0; atsy = 0;};
119 		}
120 	}
121 	
122 	function draw1(){
123 		ctx.clearRect(0,0,500,500);
124 		ctx.lineWidth="1";
125 		var radf = document.getElementsByName('field');
126 		var radn = document.getElementsByName('kolvo');
127 		var radd = document.getElementsByName('direct');
128 		var radc = document.getElementsByName('ch');
129 		if (radf[0].checked==true){
130 			if (radn[0].checked==true){
131 				ctx.beginPath();
132 				ctx.arc(250,250,radod,0,2*Math.PI);
133 				ctx.stroke();
134 				ctx.fillStyle="grey";
135 				ctx.fill();
136 				var rod = Math.sqrt((x-250)*(x-250)+(y-250)*(y-250));
137 				if (rod<=radod+rad){
138 					var sinod = Math.abs(y-250)/rod;
139 					var cosod = Math.abs(x-250)/rod;
140 					if (x>250){x = 250+(radod+rad)*cosod;}
141 					else{x = 250-(radod+rad)*cosod;}
142 					if (y>250){y = 250+(radod+rad)*sinod;}
143 					else{y = 250-(radod+rad)*sinod;}
144 				}
145 				ctx.beginPath();
146 				ctx.arc(x,y,rad,0,2*Math.PI);
147 				ctx.stroke(); ctx.fillStyle="black";
148 				ctx.fill();
149 			}
150 			if (radn[1].checked==true){
151 				ctx.beginPath();
152 				ctx.arc(350,350,rad1,0,2*Math.PI);
153 				ctx.stroke();
154 				ctx.fillStyle="brown";
155 				ctx.fill();
156 				ctx.beginPath();
157 				ctx.arc(100,100,rad2,0,2*Math.PI);
158 				ctx.stroke();
159 				ctx.fillStyle="purple";
160 				ctx.fill();
161 				var r1 = Math.sqrt((x-350)*(x-350)+(y-350)*(y-350));
162 				var r2 = Math.sqrt((x-100)*(x-100)+(y-100)*(y-100));
163 				if (r1<=rad1+rad){
164 					var sin1 = Math.abs(y-350)/r1;
165 					var cos1 = Math.abs(x-350)/r1;
166 					if (x>350){x = 350+(rad1+rad)*cos1;}
167 					else{x = 350-(rad1+rad)*cos1;}
168 					if (y>350){y = 350+(rad1+rad)*sin1;}
169 					else{y = 350-(rad1+rad)*sin1;}
170 				}
171 				if (r2<=rad2+rad){
172 					var sin2 = Math.abs(y-100)/r2;
173 					var cos2 = Math.abs(x-100)/r2;
174 					if (x>100){x = 100+(rad2+rad)*cos2;}
175 					else{x = 100-(rad2+rad)*cos2;}
176 					if (y>100){y = 100+(rad2+rad)*sin2;}
177 					else{y = 100-(rad2+rad)*sin2;}
178 				}
179 				ctx.beginPath();
180 				ctx.arc(x,y,rad,0,2*Math.PI);
181 				ctx.stroke(); ctx.fillStyle="black";
182 				ctx.fill();
183 			}
184 		}
185 		if (radf[1].checked==true){
186 			ctx.beginPath();
187 			ctx.arc(x,y,rad,0,2*Math.PI);
188 			ctx.stroke(); ctx.fillStyle="black";
189 			ctx.fill();
190 			ctx.beginPath();
191 			ctx.arc(30,30,15,0,2*Math.PI);
192 			ctx.strokeStyle="grey";
193 			ctx.stroke();
194 			if (radd[1].checked==true){
195 				ctx.beginPath();
196 				ctx.moveTo(19.4,19.4);
197 				ctx.lineTo(40.6,40.6);
198 				ctx.stroke();
199 				ctx.beginPath();
200 				ctx.moveTo(19.4,40.6);
201 				ctx.lineTo(40.6,19.4);
202 				ctx.stroke();
203 			}
204 			else{
205 				ctx.beginPath();
206 				ctx.arc(30,30,1,0,2*Math.PI);
207 				ctx.fillStyle="grey";
208 				ctx.fill();
209 				ctx.stroke();
210 			}
211 			ctx.beginPath();
212 			ctx.moveTo(x-5,y);
213 			ctx.lineTo(x+5,y);
214 			if (radc[0].checked==true){
215 				ctx.moveTo(x,y-5);
216 				ctx.lineTo(x,y+5);
217 			}
218 			ctx.strokeStyle="white";
219 			ctx.lineWidth="3";
220 			ctx.stroke();
221 		}
222 	}
223 	
224 	function control(){
225 		phys1();
226 		draw1();
227 	}
228 	
229 	var radf = document.getElementsByName('field');
230 	if (radf[0].checked==true){setInterval(control, 100/fps);}
231 	if (radf[1].checked==true){setInterval(control, 0.1/fps);}
232 }