Advertisement
Guest User

Untitled

a guest
May 19th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "drawMethods/drawArc.as"
  2.  
  3. //параметры полотна для рисования тела и графиков
  4. //полотно повернуто снизу вверх, значит можно использовать
  5. //стандартную систему координат
  6. _global.plane_width = 600;
  7. _global.plane_height = 600;
  8. _global.plane_x_offset = _root.p0._x;
  9. _global.plane_y_offset = _root.p0._y;
  10.  
  11. //размер сегмента
  12. _global.plane_xsegm = plane_width/10;
  13. _global.plane_ysegm = plane_height/10;
  14. _global.plane_scale = plane_xsegm/15;
  15.  
  16. //полотно для вывода графика
  17. _global.graph_width = 200;
  18. _global.graph_height = 200;
  19. _global.graph_x_offset = _root.g0._x;
  20. _global.graph_y_offset = _root.g0._y;
  21.  
  22. _global.graph_xsegm = graph_width/60;
  23. _global.graph_ysegm = graph_height/60;
  24. _global.graph_scale = graph_xsegm;
  25.  
  26. init = function()
  27. {
  28.     init_clips();
  29.     add_ui_listeners();
  30.     planes_bg_init();
  31.     graph_bg_init();
  32.     redraw();
  33.    
  34.  
  35. };
  36.  
  37. //загрузка клипов на сцену и установка начальных параметров
  38. init_clips = function()
  39. {
  40.     _global.body = p1.attachMovie("body", "body", getNextHighestDepth());
  41.     body.plane_length = 100;
  42.     body.g = 10;
  43.     body.dst_frm_0 = 200;
  44.  
  45.     body.m = sel_m.value;
  46.     body.Fouter = sel_Fouter.value;
  47.     body.CoF = sel_CoF.value;
  48.     body.a = sel_a.value * math.PI / 180;
  49.  
  50.     _global.grdot = g3.attachMovie("dot", "grdot", getNextHighestDepth());
  51.    
  52.     var vect = new Object();
  53.  
  54.     vect.mg = _root.attachMovie("vlbl_mg", "vlbl_mg", getNextHighestDepth());
  55.    
  56.     vect.ma = _root.attachMovie("vlbl_ma", "vlbl_ma", getNextHighestDepth());
  57.    
  58.     vect.N = _root.attachMovie("vlbl_N", "vlbl_N", getNextHighestDepth());
  59.  
  60.     vect.Fouter = _root.attachMovie("vlbl_Fouter", "vlbl_Fouter", getNextHighestDepth());
  61.  
  62.     vect.FoF = _root.attachMovie("vlbl_FoF", "vlbl_FoF", getNextHighestDepth());
  63.    
  64.     a_lbl = _root.attachMovie("vlbl_a", "vlbl_a", getNextHighestDepth());
  65.    
  66.     a_lbl._x = 515; a_lbl._y = 325;
  67.  
  68.     _global.vect = vect;
  69. };
  70.  
  71. //перерисовка тела и графика после изменения параметров
  72. redraw = function()
  73. {   //trace(">>> redraw"); 
  74.     draw_planes();
  75.     initial_pos();
  76.     pos_body();
  77.     redraw_graph();
  78.     sel_m_lbl.text = body.m + lbl.kg;
  79.     sel_Fouter_lbl.text = body.Fouter + lbl.Newton;
  80.     sel_CoF_lbl.text = body.CoF;
  81.     sel_a_lbl.text = body.a / Math.PI * 180 + lbl.a;
  82.     if (sel_a.value < 20)
  83.         a_lbl._y = -999;
  84.     else
  85.         a_lbl._y = 325;
  86. };
  87.  
  88. //навешивание обработчиков на контролы
  89. add_ui_listeners = function()
  90. { //trace(">>> add_ui_listeners ");
  91.     start_button.onRelease = function()
  92.     {
  93.         if (body.accel != 0 && grdot._x == 0) {
  94.             slide_body();
  95.             start_button.label = lbl.reset;
  96.             pause_button.enable();
  97.         } else {
  98.             _root.onEnterFrame = null;
  99.             banzai.gotoAndStop(1);
  100.             body.v = 0;
  101.             redraw();
  102.             start_button.label = lbl.launch;
  103.         };
  104.     };
  105.  
  106.     play_button.actions = function() {
  107.         _root.onEnterFrame = function() {
  108.             step();
  109.         };
  110.         play_button.disable();
  111.         step_button.disable();
  112.         pause_button.enable();
  113.     };
  114.     step_button.actions = function() {
  115.         if (!_root.onEnterFrame) {
  116.             step();
  117.         };
  118.     };
  119.  
  120.     pause_button.actions = function() {
  121.         _root.onEnterFrame = null;
  122.         play_button.enable();
  123.         step_button.enable();
  124.         pause_button.disable();
  125.     };
  126.  
  127.     var slideListener:Object = new Object();
  128.     slideListener.onChange = function(evnt:Object)
  129.     {
  130.         if (grdot._x != 0)
  131.             return;
  132.         switch(String(evnt.target)) {
  133.             case "_level0.sel_m":
  134.             body.m = evnt.value;
  135.             break;
  136.  
  137.             case "_level0.sel_Fouter":
  138.             body.Fouter = evnt.value;
  139.             break;
  140.  
  141.             case "_level0.sel_CoF":
  142.             body.CoF = evnt.value;
  143.             break;
  144.  
  145.             case "_level0.sel_a":
  146.             body.a = evnt.value * Math.PI / 180;
  147.         };
  148.             redraw();
  149.        
  150.     };
  151.  
  152.     sel_m.addListener(slideListener);
  153.     sel_Fouter.addListener(slideListener);
  154.     sel_CoF.addListener(slideListener);
  155.     sel_a.addListener(slideListener);
  156. };
  157.  
  158. //движение тела
  159. slide_body = function()
  160. {
  161.     if(body.accel == 0)
  162.         return;
  163.     banzai.gotoAndPlay(2);
  164.     _global.body.v = 0;
  165.  
  166.     _root.onEnterFrame = function()
  167.     {
  168.         step();
  169.     };
  170. };
  171.  
  172. step = function()
  173. { //trace("step");
  174.     body.v += body.accel/15;
  175.     body.x -= body.v * Math.cos(body.a);
  176.     body.y -= body.v * Math.sin(body.a);
  177.     //if(body.x > 10 && body.x < 300) { //приехали?
  178.     if(body.x > 0 && body.x < 300) { //приехали?
  179.         pos_body();
  180.         grdot._x = body.v * graph_scale;
  181.     } else {
  182.         _root.onEnterFrame = null;
  183.         pause_button.disable();
  184.     };
  185. };
  186.  
  187. //начальная позиция
  188. initial_pos = function()
  189. {
  190.     body.x = body.dst_frm_0 * Math.cos(body.a);
  191.     body.y = body.dst_frm_0 * Math.sin(body.a);
  192.     grdot._x = 0;
  193. };
  194.  
  195. //перемещение тела и рассчет векторов
  196. pos_body = function()
  197. {
  198.     var x_dec, y_dec, v, body_center_point, Fsum;
  199.    
  200.     body._x = body.x;
  201.     body._y = body.y;
  202.    
  203.     var body_center_point:Object = {x:0, y:0};
  204.     body.center.localToGlobal(body_center_point);
  205.     p3.globalToLocal(body_center_point);
  206.    
  207.     body.x7 = body_center_point.x; //M_p * Math.cos(M_b);
  208.     body.y7 = body_center_point.y; //M_p * Math.sin(M_b);
  209.    
  210.     p3.clear(); p3.lineStyle(2, 0x559955);
  211.    
  212.  
  213.  
  214.     v = vect.mg;
  215.         v.magn = body.m * body.g;
  216.         v.x0 = body.x7;
  217.         v.y0 = body.y7;
  218.         v.x_proj = 0;
  219.         v.y_proj = v.magn;
  220.         v.x1 = body.x7;
  221.         v.y1 = body.y7 + v.y_proj * -plane_scale;
  222.         vector(p3, v.x0, v.y0, v.x1, v.y1, vect.mg);
  223.  
  224.     v = vect.N;
  225.         v.magn = vect.mg.magn * Math.cos(body.a);
  226.         v.x0 = body.x7;
  227.         v.y0 = body.y7;
  228.         if(body.a != 0) {
  229.             v.x_proj = v.magn * Math.cos(Math.PI/2 + body.a);
  230.         } else {
  231.             v.x_proj = 0;
  232.         };
  233.         v.y_proj = v.magn * Math.sin(Math.PI/2 + body.a);
  234.         v.x1 = body.x7 + v.x_proj * plane_scale;
  235.         v.y1 = body.y7 + v.y_proj * plane_scale;
  236.         vector(p3, v.x0, v.y0, v.x1, v.y1, vect.N);
  237.  
  238.     body.frict_max = body.CoF * vect.N.magn;
  239.  
  240.     v = vect.Fouter;
  241.         v.magn = body.Fouter;
  242.         v.x0 = body.x7;
  243.         v.y0 = body.y7;
  244.         v.x1 = body.x7 - v.magn * Math.cos(body.a) * plane_scale;
  245.         v.y1 = body.y7 - v.magn * Math.sin(body.a) * plane_scale;
  246.         vector(p3, v.x0, v.y0, v.x1, v.y1, vect.Fouter);
  247.  
  248.     v = vect.ma;
  249.         v.magn = Math.sqrt(Math.pow((vect.N.x_proj), 2) + Math.pow(vect.N.y_proj - vect.mg.y_proj, 2));
  250.         v.x0 = body.x7;
  251.         v.y0 = body.y7;
  252.         v.x1 = body.x7 + ((vect.N.x1 - body.x7) + (vect.mg.x1 - body.x7));
  253.         v.y1 = body.y7 + ((vect.N.y1 - body.y7) + (vect.mg.y1 - body.y7));
  254.         vector(p3, v.x0, v.y0, v.x1, v.y1, vect.ma);
  255.         p1.clear();
  256.         p1.lineStyle(0, 0xDDDDDD);
  257.         line(p1, vect.N.x1, vect.N.y1, v.x1, v.y1, vect.ma);
  258.         line(p1, vect.mg.x1, vect.mg.y1, v.x1, v.y1, vect.ma);
  259.            
  260.  
  261.     v = vect.FoF;
  262.     Fsum = vect.ma.magn + vect.Fouter.magn;
  263.     if (Fsum > 0) {
  264.         if (Fsum > body.frict_max)
  265.             v.magn = body.frict_max;
  266.         else if (Fsum < body.frict_max)
  267.             v.magn = Fsum;
  268.     } else if (Fsum < 0) {
  269.         if (Fsum < -body.frict_max)
  270.             v.magn = -body.frict_max;
  271.         if (Fsum > -body.frict_max)
  272.             v.magn = Fsum;
  273.     };
  274.         v.magn *= -1;
  275.         v.x0 = body.x7;
  276.         v.y0 = body.y7;
  277.         v.x1 = body.x7 - v.magn * Math.cos(body.a) * plane_scale;
  278.         v.y1 = body.y7 - v.magn * Math.sin(body.a) * plane_scale;
  279.         p3.lineStyle(1, 0x005555)
  280.         vector(p3, v.x0, v.y0, v.x1, v.y1, vect.FoF);
  281.  
  282.     body._rotation = body.a / Math.PI * 180;
  283.  
  284.     body.Fsum = vect.ma.magn + vect.Fouter.magn + vect.FoF.magn;
  285.     body.accel = body.Fsum / body.m;
  286.    
  287.  
  288.     N_lbl.text = round_01(vect.N.magn) + lbl.Newton;
  289.     CoFN_lbl.text = round_01(body.CoF * vect.N.magn) + lbl.Newton;
  290.     Fsum_lbl.text = round_01(body.Fsum) + lbl.Newton;
  291.     FoF_lbl.text = round_01(vect.FoF.magn) + lbl.Newton;
  292.     accel_lbl.text = round_01(body.accel) + lbl.ms2;
  293.  
  294.     tm_round(
  295.     lbl.ma + "=", vect.ma.magn,
  296.     lbl.Fsum + "=", vect.ma.magn - vect.Fouter.magn - vect.FoF.magn,
  297.     lbl.acc + "=", body.accel, "v=", body.v);
  298.     //tm("v.x0", v.x0, "v.x1", v.x1, "v.y0", v.y0, "v.y1", v.y1, "beta", Math.PI/2 - (Math.PI/4 - body.a)); */
  299.  
  300. };
  301.  
  302. //перерисовка графика
  303. redraw_graph = function()
  304. {
  305.     var Ktr = -body.frict_max * graph_scale;
  306.  
  307.     g1.clear();
  308.     g1.lineStyle(2, 0xFF5500);
  309.     line(g1, graph_width/2, Ktr, 0, Ktr);
  310.     line(g1, 0, Ktr, 0, -Ktr);
  311.     line(g1, 0, -Ktr, -graph_width/2, -Ktr);
  312.  
  313.     grdot._y = vect.FoF.magn * graph_scale;
  314. };
  315.  
  316. //рисование плоскостей
  317. draw_planes = function()
  318. {
  319.     var x_dec, y_dec;
  320.  
  321.     p2.clear(); p2.lineStyle(1, 0x995555)
  322.  
  323.     line(p2, 0, 0, 300, 0);
  324.  
  325.     x_dec = 300 * Math.cos(body.a);
  326.     y_dec = 300 * Math.sin(body.a);
  327.     line(p2, 0, 0, x_dec, y_dec);
  328.    
  329.     p2.moveTo(50, 0);
  330.     p2.drawArc(50, 0, 50, -body.a * 180 / Math.PI, 0);
  331. };
  332.  
  333. //оси координат на на плоскости с телом
  334. planes_bg_init = function()
  335. {
  336.     p0.lineStyle(1, 0xFFEEEE);
  337.     line(p0, -plane_width / 2, 0, plane_width / 2, 0);
  338.     line(p0, -plane_width / 2, 0, plane_width / 2, 0);
  339.     line(p0, 0, -plane_height / 2, 0, plane_height / 2);   
  340.  
  341.     p0.moveTo(plane_width/2 -10, 10);  
  342.     p0.lineTo(plane_width/2, 0);
  343.     p0.lineTo(plane_width/2 -10, -10);
  344.     p0.moveTo(-10, plane_height/2 - 10);
  345.     p0.lineTo(0, plane_height/2);
  346.     p0.lineTo(10, plane_height/2 - 10);
  347. };
  348.  
  349. //разметка полотна графика, рисование осей координат, сетки и подписей
  350. graph_bg_init = function()
  351. {
  352.     var i:Number, x_grid:Number, y_grid:Number, tmp_obj:Object;
  353.  
  354.     g0.lineStyle(1, 0xE0E0E0, 100); //серые линии
  355.     for(i = -30; i <= 30; i = i + 10) { //рисование линий
  356.        
  357.         x_grid = graph_width / 60 * i;
  358.         y_grid = graph_width / 60 * i;
  359.  
  360.         line(g0, x_grid, graph_width/2, x_grid, -graph_width/2);
  361.         line(g0, graph_height/2, y_grid, -graph_height/2, y_grid);
  362.  
  363.     //подпись координат
  364.         var my_fmt:TextFormat = new TextFormat();
  365.         my_fmt.color = 0x000000;
  366.         my_fmt.align = "center";
  367.         my_fmt.size = 13;
  368.         if(i / 5 == Math.round(i / 5 )) {
  369.             tmp_obj = _root.createTextField("g0_label_x"+String(i), _root.getNextHighestDepth(), x_grid - 12, 0, 25, 30); //координаты по Х
  370.             tmp_obj.text = i; tmp_obj.setTextFormat(my_fmt);
  371.             tmp_obj._x += graph_x_offset;
  372.             tmp_obj._y = graph_y_offset - tmp_obj._y;
  373.         };
  374.  
  375.         my_fmt.align = "right";
  376.         if((i / 5 == Math.round(i / 5 )) && i != 0) {
  377.             tmp_obj = _root.createTextField("g0_label_y"+String(i), _root.getNextHighestDepth(), -24, y_grid + 8, 25, 30); //координаты по Х
  378.             tmp_obj.text = i; tmp_obj.setTextFormat(my_fmt);
  379.             tmp_obj._x += graph_x_offset;
  380.             tmp_obj._y = graph_y_offset - tmp_obj._y;
  381.         };
  382.     };
  383.    
  384.     my_fmt.size = 18;
  385.     my_fmt.align = "left";
  386.     tmp_obj = _root.createTextField("g0_label_x_axe", _root.getNextHighestDepth(), graph_width/2 + 10, 0, 50, 50); //ось Х
  387.         tmp_obj.text = lbl.vms;
  388.         tmp_obj.setTextFormat(my_fmt);
  389.         tmp_obj._x += graph_x_offset;
  390.         tmp_obj._y = graph_y_offset - tmp_obj._y;
  391.  
  392.     tmp_obj = _root.createTextField("g0_label_y_axe", _root.getNextHighestDepth(), 2, graph_height/2 + 20, 50, 50); //координаты по Х
  393.         tmp_obj.text = lbl.FH;
  394.         tmp_obj.setTextFormat(my_fmt);
  395.         tmp_obj._x += graph_x_offset;
  396.         tmp_obj._y = graph_y_offset - tmp_obj._y;
  397.  
  398.     g0.lineStyle(1, 0xFF3333);
  399.     line(g0, -graph_width / 2, 0, graph_width / 2, 0);
  400.     line(g0, -graph_width / 2, 0, graph_width / 2, 0);
  401.     line(g0, 0, -graph_height / 2, 0, graph_height / 2);   
  402.  
  403.     g0.moveTo(graph_width/2 -10, 10);  
  404.     g0.lineTo(graph_width/2, 0);
  405.     g0.lineTo(graph_width/2 -10, -10);
  406.     g0.moveTo(-10, graph_height/2 - 10);
  407.     g0.lineTo(0, graph_height/2);
  408.     g0.lineTo(10, graph_height/2 - 10);
  409.  
  410.     g3.swapDepths(_root.getNextHighestDepth());
  411. };
  412.  
  413. //служебные функции
  414. round_01 = function(rounded)
  415. { //обрезание строки до 2 символа после точки включительно
  416.     if(String(rounded).indexOf(".") != -1)
  417.         return(String(rounded).slice(0, String(rounded).indexOf(".") + 3));
  418.     return(rounded);
  419. };
  420.  
  421. line = function(clip:Object, x1:Number, y1:Number, x2:Number, y2:Number)
  422. { //рисование линии по 4 координатам
  423.     clip.moveTo(x1,y1); clip.lineTo(x2,y2);
  424. };
  425.  
  426. tm_round = function(t0:Object, t1:Object, t2:Object, t3:Object, t4:Object, t5:Object, t6:Object, t7:Object, t8:Object, t9:Object, t10:Object, t11:Object, t12:Object, t13:Object, t14:Object, t15:Object, t16:Object, t17:Object, t18:Object)
  427. {
  428.     var trace_string:String = "";
  429.     for(i = 0; i <= arguments.length-1; i++) {
  430.         if (arguments[i] != undefined)
  431.             trace_string += round_01(arguments[i]);
  432.         if (Math.round(i/2) != i/2)
  433.             trace_string += "\t\t";
  434.     };
  435.     //trace(trace_string);
  436.     param.text = trace_string;
  437. };
  438.  
  439. //функция для рисования векторов
  440. vector = function(clip, x1, y1, x2, y2, label)
  441. {
  442.     var p, a, a_90;
  443.     p = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
  444.     a = Math.atan2(y2 - y1, x2 - x1);
  445.     a_90 = (a + Math.PI / 2);
  446.  
  447.     //координаты для стрелок векторов
  448.     x_dec = (p - 7) * Math.cos(a);
  449.     y_dec = (p - 7) * Math.sin(a);
  450.     x1_dec = 5 * Math.cos(a_90);
  451.     y1_dec = 5 * Math.sin(a_90);
  452.  
  453.     if (Math.abs(x2 - x1) >= 5 || Math.abs(y2 - y1) >= 5) {
  454.         line(clip, x1, y1, x2, y2);
  455.         line(clip, x_dec+x1+x1_dec, y_dec+y1+y1_dec, x2, y2);
  456.         line(clip, x_dec+x1-x1_dec, y_dec+y1-y1_dec, x2, y2);
  457.    
  458.         x_dec = (p + 15) * Math.cos(a);
  459.         y_dec = (p + 15) * Math.sin(a);
  460.         vector_label(x_dec + x1, y_dec + y1, label);
  461.  
  462.     } else {
  463.         vector_label(-999, -999, label);
  464.     };
  465. };
  466.  
  467. //подписи к векторам
  468. vector_label = function(x, y, label)
  469. {
  470.     x += plane_x_offset;
  471.     y = plane_y_offset - y;
  472.  
  473.     label._x = x;
  474.     label._y = y;
  475. };
  476.  
  477. //алиас к трейсу, чтобы удобнее вызывать
  478. tm = function(t0:Object, t1:Object, t2:Object, t3:Object, t4:Object, t5:Object, t6:Object, t7:Object, t8:Object, t9:Object, t10:Object, t11:Object, t12:Object, t13:Object, t14:Object)
  479. {
  480.     var trace_string:String = "";
  481.     for(i = 0; i <= 15; i ++) {
  482.         if (arguments[i] != undefined)
  483.             trace_string += arguments[i] + " \t";
  484.     };
  485.     trace(trace_string);
  486.     param.text = trace_string;
  487. };
  488.  
  489. init(); //поехали (:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement