Ws-htmlets/js2020/Galov$Shilov/html.html
Материал из Department of Theoretical and Applied Mechanics
Версия от 12:59, 2 июня 2020; 176.56.6.178 (обсуждение)
Описание
Движение частицы в гравитационном и электромагнитном полях
Исполнители: Галов Владислав, Шилов Михаил, Борисенков Богдан
Визуализация
Код программы
Код программы на языке JavaScript:
1 window.addEventListener("load", main, false);
2
3 function main(){
4 var ctx = canv.getContext('2d');
5
6 var h = canv.height;
7
8 var w = canv.width;
9
10 var E = 10; var Mod = 100000000; var Ma1 = 100000000; var Ma2 = 50000000;
11
12 var radod = 100;
13
14 var rad1 = 100;
15
16 var rad2 = 50;
17
18 var rad = 10;
19
20 var x; var y; var m;
21
22 var vx = 0; var vy = 0; var aex = 0; var aey = 0; var atsx = 0; var atsy = 0;
23
24 var axg = 0; var ayg = 0;
25
26 var Flx = 0; var Fly = 0; var xo; var yo;
27
28 var fps = 60;
29
30 var dt = 1/fps;
31
32
33 canv.onclick = function(event){
34
35 dt = 1/fps;
36
37 var rect = canv.getBoundingClientRect();
38
39 x = event.clientX - rect.left;
40
41 y = event.clientY - rect.top;
42
43
44 }
45
46
47
48 function phys1(){
49
50 m = document.getElementById('polz').value;
51
52 var radf = document.getElementsByName('field');
53
54 var radn = document.getElementsByName('kolvo');
55
56 var rads = document.getElementsByName('sput')
57
58 var radd = document.getElementsByName('direct');
59
60 var radc = document.getElementsByName('ch');
61
62 if (radf[0].checked==true){
63
64 if (rads[1].checked==true){
65
66 if (radn[0].checked==true){
67
68 var rod = Math.sqrt((x-250)*(x-250)+(y-250)*(y-250));
69
70 var cosod = (250-x)/rod;
71
72 var sinod = (250-y)/rod;
73
74 var fg = m*Mod/(rod*rod);
75
76 var fgx = fg*cosod; var fgy = fg*sinod;
77
78 axg = fgx/m; ayg = fgy/m;
79
80 x = x+3*axg*dt*dt/2;
81
82 y = y+3*ayg*dt*dt/2;
83
84 }
85
86 if (radn[1].checked==true){
87
88 var r1 = Math.sqrt((x-350)*(x-350)+(y-350)*(y-350));
89
90 var r2 = Math.sqrt((x-100)*(x-100)+(y-100)*(y-100));
91
92 var cos1 = (350-x)/r1;
93
94 var cos2 = (100-x)/r2;
95
96 var sin1 = (350-y)/r1;
97
98 var sin2 = (100-y)/r2;
99
100 var fg1 = m*Ma1/(r1*r1);
101
102 var fg2 = m*Ma2/(r2*r2);
103
104 var fg1x = fg1*cos1; var fg2x = fg2*cos2;
105
106 var fg1y = fg1*sin1; var fg2y = fg2*sin2;
107
108 axg = (fg1x+fg2x)/m; ayg = (fg1y+fg2y)/m;
109
110 x = x+3*axg*dt*dt/2;
111
112 y = y+3*ayg*dt*dt/2;
113
114 if (r1<rad1+rad)dt=0;
115
116 if (r2<rad2+rad)dt=0;
117
118 }
119
120 }
121
122 if (rads[0].checked==true){
123
124 if (radn[0].checked==true){
125
126 var rsod = Math.sqrt((x-250)*(x-250)+(y-250)*(y-250));
127
128 var cossod = (250-x)/rsod;
129
130 var sinsod = (250-y)/rsod;
131
132 var v = Math.sqrt(0.1*Mod/rsod);
133
134 vx = v*sinsod; vy = v*(-cossod);
135
136 ac = v*v/rsod;
137
138 acx = ac*cossod; acy = ac*sinsod;
139
140 x = x+vx*dt+acx*dt*dt/2;
141
142 y = y+vy*dt+acy*dt*dt/2;
143
144 }
145
146 if (radn[1].checked==true){
147
148 var rs1 = Math.sqrt((x-350)*(x-350)+(y-350)*(y-350));
149
150 var rs2 = Math.sqrt((x-100)*(x-100)+(y-100)*(y-100));
151
152 if (rs1<=rs2+70){
153
154 var coss1 = (350-x)/rs1;
155
156 var sins1 = (350-y)/rs1;
157
158 var v = Math.sqrt(0.1*Ma1/rs1);
159
160 vx = v*sins1; vy = v*(-coss1);
161
162 ac = v*v/rs1;
163
164 acx = ac*coss1; acy = ac*sins1;
165
166 x = x+vx*dt+acx*dt*dt/2;
167
168 y = y+vy*dt+acy*dt*dt/2;
169
170 }
171
172 else{
173
174 var coss2 = (100-x)/rs2;
175
176 var sins2 = (100-y)/rs2;
177
178 var v = Math.sqrt(0.1*Ma2/rs2);
179
180 vx = -v*sins2; vy = v*coss2;
181
182 ac = v*v/rs2;
183
184 acx = ac*coss2; acy = ac*sins2;
185
186 x = x+vx*dt+acx*dt*dt/2;
187
188 y = y+vy*dt+acy*dt*dt/2;
189
190 }
191
192 }
193
194 }
195
196 }
197
198 if (radf[1].checked==true){
199
200 if (radd[0].checked==true && radc[0].checked==true ||
201
202 radd[1].checked==true && radc[1].checked==true){
203
204 atsy = vx/m;
205
206 atsx = (-vy)/m;
207
208 }
209
210 else{
211
212 atsy = (-vx)/m;
213
214 atsx = vy/m;
215
216 }
217
218 var radd2 = document.getElementsByName('direct2');
219
220 if (radd2[0].checked==true){aex = E/m;}
221
222 if (radd2[1].checked==true){aex = -E/m;}
223
224 x = x+vx*dt+(aex+atsx)*dt*dt/2;
225
226 y = y+vy*dt+atsy*dt*dt/2;
227
228 vx = vx+(aex+atsx)*dt;
229
230 vy = vy+atsy*dt;
231
232 if (x<-10 || y<-10 || x>w+10 || y>h+10) {vx = 0; vy = 0; atsx = 0; atsy = 0;};
233
234 }
235
236 }
237
238
239
240 function draw1(){
241
242 ctx.clearRect(0,0,500,500);
243
244 ctx.lineWidth="1";
245
246 var radf = document.getElementsByName('field');
247
248 var radn = document.getElementsByName('kolvo');
249
250 var radd = document.getElementsByName('direct');
251
252 var radc = document.getElementsByName('ch');
253
254 if (radf[0].checked==true){
255
256 if (radn[0].checked==true){
257
258 ctx.beginPath();
259
260 ctx.arc(250,250,radod,0,2*Math.PI);
261
262 ctx.stroke();
263
264 ctx.fillStyle="grey";
265
266 ctx.fill();
267
268 var rod = Math.sqrt((x-250)*(x-250)+(y-250)*(y-250));
269
270 if (rod<=radod+rad){
271
272 var sinod = Math.abs(y-250)/rod;
273
274 var cosod = Math.abs(x-250)/rod;
275
276 if (x>250){x = 250+(radod+rad)*cosod;}
277
278 else{x = 250-(radod+rad)*cosod;}
279
280 if (y>250){y = 250+(radod+rad)*sinod;}
281
282 else{y = 250-(radod+rad)*sinod;}
283
284 }
285
286 ctx.beginPath();
287
288 ctx.arc(x,y,rad,0,2*Math.PI);
289
290 ctx.stroke(); ctx.fillStyle="black";
291
292 ctx.fill();
293
294 }
295
296 if (radn[1].checked==true){
297
298 ctx.beginPath();
299
300 ctx.arc(350,350,rad1,0,2*Math.PI);
301
302 ctx.stroke();
303
304 ctx.fillStyle="brown";
305
306 ctx.fill();
307
308 ctx.beginPath();
309
310 ctx.arc(100,100,rad2,0,2*Math.PI);
311
312 ctx.stroke();
313
314 ctx.fillStyle="purple";
315
316 ctx.fill();
317
318 var r1 = Math.sqrt((x-350)*(x-350)+(y-350)*(y-350));
319
320 var r2 = Math.sqrt((x-100)*(x-100)+(y-100)*(y-100));
321
322 if (r1<=rad1+rad){
323
324 var sin1 = Math.abs(y-350)/r1;
325
326 var cos1 = Math.abs(x-350)/r1;
327
328 if (x>350){x = 350+(rad1+rad)*cos1;}
329
330 else{x = 350-(rad1+rad)*cos1;}
331
332 if (y>350){y = 350+(rad1+rad)*sin1;}
333
334 else{y = 350-(rad1+rad)*sin1;}
335
336 }
337
338 if (r2<=rad2+rad){
339
340 var sin2 = Math.abs(y-100)/r2;
341
342 var cos2 = Math.abs(x-100)/r2;
343
344 if (x>100){x = 100+(rad2+rad)*cos2;}
345
346 else{x = 100-(rad2+rad)*cos2;}
347
348 if (y>100){y = 100+(rad2+rad)*sin2;}
349
350 else{y = 100-(rad2+rad)*sin2;}
351
352 }
353
354 ctx.beginPath();
355
356 ctx.arc(x,y,rad,0,2*Math.PI);
357
358 ctx.stroke(); ctx.fillStyle="black";
359
360 ctx.fill();
361
362 }
363
364 }
365
366 if (radf[1].checked==true){
367
368 ctx.beginPath();
369
370 ctx.arc(x,y,rad,0,2*Math.PI);
371
372 ctx.stroke(); ctx.fillStyle="black";
373
374 ctx.fill();
375
376 ctx.beginPath();
377
378 ctx.arc(30,30,15,0,2*Math.PI);
379
380 ctx.strokeStyle="grey";
381
382 ctx.stroke();
383
384 if (radd[1].checked==true){
385
386 ctx.beginPath();
387
388 ctx.moveTo(19.4,19.4);
389
390 ctx.lineTo(40.6,40.6);
391
392 ctx.stroke();
393
394 ctx.beginPath();
395
396 ctx.moveTo(19.4,40.6);
397
398 ctx.lineTo(40.6,19.4);
399
400 ctx.stroke();
401
402 }
403
404 else{
405
406 ctx.beginPath();
407
408 ctx.arc(30,30,1,0,2*Math.PI);
409
410 ctx.fillStyle="grey";
411
412 ctx.fill();
413
414 ctx.stroke();
415
416 }
417
418 ctx.beginPath();
419
420 ctx.moveTo(x-5,y);
421
422 ctx.lineTo(x+5,y);
423
424 if (radc[0].checked==true){
425
426 ctx.moveTo(x,y-5);
427
428 ctx.lineTo(x,y+5);
429
430 }
431
432 ctx.strokeStyle="white";
433
434 ctx.lineWidth="3";
435
436 ctx.stroke();
437
438 }
439
440 }
441
442
443
444 function control(){
445
446 phys1();
447
448 draw1();
449
450 }
451
452
453
454 var radf = document.getElementsByName('field');
455
456
457 if (radf[0].checked==true){setInterval(control, 100/fps);}
458
459
460 if (radf[1].checked==true){setInterval(control, 0.1/fps);}
461
462 }