Задача о двух телах

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

Описание[править]

Решение и визуализация задачи двух тел.

Шелых Екатерина

Файл:[[1]]

Код программы[править]

Текст программы на языке JavaScript:
  1 var gravitationalConstant = 6.67*Math.pow(10,-11);//гравитационная постоянная
  2 var point_x1 = 1200/6*3;
  3 var point_y1 = 300;
  4 var point_x2 = 1200/6*4;
  5 var point_y2 = 300;
  6 var sP = 1;
  7 var k = 0;
  8 var flag = 0;
  9 function firstPaint(){
 10   var canvas = document.getElementById('pole');
 11   if (canvas.getContext){
 12       var ctx = canvas.getContext('2d');
 13   }
 14   var img1 = new Image();
 15       img1.src = "planet1.jpg";
 16   img1.onload = function(){
 17 
 18       var img = new Image();
 19           img.src = "space.jpg";
 20        img.onload = function(){
 21          var pattern = ctx.createPattern(img,"repeat");
 22             ctx.fillStyle = pattern;
 23             ctx.beginPath();
 24             ctx.clearRect(0,0,1500,800);
 25             ctx.moveTo(1,1);
 26             ctx.fillRect(0, 0, 1500, 800);
 27             ctx.closePath();
 28             ctx.beginPath();
 29             ctx.fillStyle = "#F6F231";
 30             ctx.arc(point_x1,point_y1,10,0,Math.PI*2);
 31             ctx.fill();
 32         var img2 = new Image();
 33             img2.src = "planet2.jpg";
 34         img2.onload = function(){
 35           var pattern2 = ctx.createPattern(img2,"repeat");
 36             ctx.beginPath();
 37             ctx.fillStyle = pattern2;
 38 
 39         ctx.moveTo(point_x2,point_y2);
 40         ctx.arc(point_x2,point_y2,10,0,Math.PI*2);
 41         ctx.fill();
 42       }
 43     }
 44 }
 45 
 46 }
 47 function AddPlanet(x,y,mass,speed){
 48   this.x = x;
 49   this.y = y;
 50   this.mass = mass;
 51 
 52   this.M = this.mass*gravitationalConstant;
 53   this.speed = speed;
 54 }
 55 function makePlanets(){
 56    point_x1 = 1200/6*3;
 57    point_y1 = 300;
 58    point_x2 = 1200/6*4;
 59    point_y2 = 300;
 60   var mass =document.getElementById('firstMass').value*Math.pow(10,20);
 61   var speed = 1;
 62   var firstPlanet = new AddPlanet(point_x1,point_y1,mass,speed);
 63   mass = document.getElementById('secondMass').value*Math.pow(10,20);
 64   speed = 1;
 65   var secondPlanet = new AddPlanet(point_x1,point_y1,mass,speed);
 66 
 67 
 68   firstPlanet.r = Math.pow(10,7)*(point_x2 - point_x1 - (point_x2-point_x1)*firstPlanet.mass/(firstPlanet.mass + secondPlanet.mass));//радиус вектор 1 тела относительно центра масс системы
 69   secondPlanet.r =  Math.pow(10,7)*((point_x2-point_x1)*firstPlanet.mass/(firstPlanet.mass + secondPlanet.mass));//радиус вектор 1 тела относительно центра масс системы
 70   firstPlanet.massCenterX = point_x2 - (point_x2-point_x1)*firstPlanet.mass/(firstPlanet.mass + secondPlanet.mass);//рассчет x координыты центра масс
 71   firstPlanet.c = firstPlanet.speed*firstPlanet.r;//константы закона площадей
 72   secondPlanet.c = secondPlanet.speed*secondPlanet.r;//константы закона площадей
 73 
 74 
 75   firstPlanet.f = -firstPlanet.M + firstPlanet.speed*firstPlanet.c;//векторы Лапласса
 76   secondPlanet.f = -secondPlanet.M + secondPlanet.speed*secondPlanet.c;//векторы Лапласса
 77 
 78   firstPlanet.p = Math.pow(firstPlanet.c, 2)/firstPlanet.M/Math.pow(10,6); //фокальный параметр
 79   secondPlanet.p = Math.pow(secondPlanet.c, 2)/secondPlanet.M/Math.pow(10,6); //фокальный параметр
 80 
 81   firstPlanet.e = firstPlanet.f/firstPlanet.M;//эксцентриситет
 82   secondPlanet.e = secondPlanet.f/secondPlanet.M;//эксцентриситет
 83   console.log('e1=',firstPlanet.e,'e2=',secondPlanet.e);
 84 
 85   drawPlanets(firstPlanet,secondPlanet);
 86 }
 87 
 88 function drawPlanets(firstPlanet,secondPlanet){
 89   var canvas = document.getElementById('pole');
 90   if (canvas.getContext){
 91     var ctx = canvas.getContext('2d');
 92   }
 93 
 94   var img = new Image();
 95       img.src = "space.jpg";
 96   var img2 = new Image();
 97       img2.src = "planet2.jpg";
 98 
 99   var pattern2 = ctx.createPattern(img2,"repeat");
100 
101       img.onload = function() {
102         var pattern = ctx.createPattern(img,"repeat");
103         ctx.fillStyle = pattern;
104 
105         var angle = Math.PI;
106         var angle2 = 0;
107 
108         firstPlanet.a = firstPlanet.p/(1 - firstPlanet.e)*6;
109         firstPlanet.b = firstPlanet.p/Math.pow((1 - firstPlanet.e),0.5)*3;
110         firstPlanet.c = Math.abs(firstPlanet.e*firstPlanet.a);//фокольное расстояние
111           secondPlanet.a = secondPlanet.p/(1 - secondPlanet.e)*6;
112           secondPlanet.b = secondPlanet.p/Math.pow((1 - secondPlanet.e),0.5)*3;
113           secondPlanet.c = Math.abs(secondPlanet.e*secondPlanet.a);//фокольное расстояние
114         firstPlanet.leftScope = firstPlanet.a*Math.cos(180);
115         secondPlanet.scope = secondPlanet.a*Math.cos(0);
116         point_x1 = firstPlanet.massCenterX + firstPlanet.c;
117         point_x2 = firstPlanet.massCenterX - secondPlanet.c;
118 var x = 10, y = 390;
119         var timerId = setInterval( function(){
120           if (k==2){
121             var a =1;
122           }
123           else{
124             angle = angle + Math.PI/360;
125             if (angle >= 2*Math.PI){
126                   angle = 0;
127               }
128               angle2 = angle2 + Math.PI/360;
129               if (angle2 >= 2*Math.PI){
130                   angle2 = 0;
131                 }
132               firstPlanet.x = point_x1 + firstPlanet.a*Math.cos(angle);
133               firstPlanet.y = point_y1 + firstPlanet.b*Math.sin(angle);
134 
135               secondPlanet.x = point_x2 + secondPlanet.a*Math.cos(angle2);
136               secondPlanet.y = point_y2 + secondPlanet.b*Math.sin(angle2);
137               ctx.fillStyle = pattern;
138                 ctx.beginPath();
139                 ctx.clearRect(0,0,1500,800);
140                 ctx.moveTo(1,1);
141                 ctx.fillRect(0, 0, 1500, 800);
142                 ctx.closePath();
143 
144               ctx.beginPath();
145               ctx.moveTo(firstPlanet.x,firstPlanet.y);
146               ctx.fillStyle = "#F6F231";
147               ctx.arc(firstPlanet.x,firstPlanet.y,10,0,2*Math.PI);
148               ctx.fill();
149 
150               ctx.beginPath();
151               ctx.moveTo(firstPlanet.x,firstPlanet.y);
152               ctx.fillStyle = pattern2;
153               ctx.arc(secondPlanet.x ,secondPlanet.y ,10,0,2*Math.PI);
154               ctx.fill();
155 
156               ctx.beginPath();
157               ctx.moveTo(firstPlanet.massCenterX,point_y1);
158               ctx.fillStyle = "#1D067F";
159               ctx.arc(firstPlanet.massCenterX,point_y1 ,4,0,2*Math.PI);
160               ctx.fill();
161 
162               y = graph1(firstPlanet,secondPlanet,x,y);
163               x = x + 1;
164               if (k==1){
165                 clearTimeout(timerId);
166                 k = 0;
167               }
168             }
169           },sP);
170 
171         };
172 }
173 
174 
175 function breakk(){
176   k = 1;
177 }
178 
179 
180 function pausee(){
181   if (k==0){
182     k = 2;
183   }
184   else{
185     k = 0;
186   }
187 }
188 
189 function graph1(firstPlanet,secondPlanet,x,y){
190   var canvas = document.getElementById('pole2');
191   if (canvas.getContext){
192       var canvas = canvas.getContext('2d');
193   }
194 
195 
196 if ( flag == 0){
197   canvas.beginPath();
198   canvas.strokeStyle = "#000000";
199   canvas.moveTo(10,395);
200   canvas.lineTo(10,10);
201   canvas.lineTo(5,15);
202   canvas.moveTo(10,10);
203   canvas.lineTo(15,15);
204   canvas.moveTo(5,390);
205   canvas.lineTo(690,390);
206   canvas.lineTo(685,395);
207   canvas.moveTo(690,390);
208   canvas.lineTo(685,385);
209   canvas.moveTo(550,20);
210   canvas.lineTo(550,100);
211   canvas.lineTo(690,100);
212   canvas.lineTo(690,20);
213   canvas.lineTo(550,20);
214   canvas.strokeStyle = "#000000";
215   canvas.font = "20px serif";
216   canvas.fillText('R(t)',20,20);
217   canvas.stroke();
218   flag = 1;
219 }
220  return lengAndDraw(firstPlanet,secondPlanet,canvas,x,y)
221 }
222 
223 
224 function lengAndDraw(firstPlanet,secondPlanet,canvas,x,y){
225   var l = Math.sqrt( Math.pow(firstPlanet.x-secondPlanet.x,2) + Math.pow(firstPlanet.y - secondPlanet.y,2))/3.5;
226 //  console.log(l);
227   canvas.beginPath();
228   canvas.clearRect(550,20,140,80);
229   canvas.strokeStyle = "#000000";
230   canvas.moveTo(x,y);
231   canvas.lineTo(x + 1, 390 - l);
232   canvas.font = "20px serif";
233   canvas.fillText('r = ' + Math.floor(l*3.5) + '*10^7', 570, 60);
234   canvas.stroke();
235    return (390 - l);
236 }