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