Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "drawMethods/drawArc.as"
- //параметры полотна для рисования тела и графиков
- //полотно повернуто снизу вверх, значит можно использовать
- //стандартную систему координат
- _global.plane_width = 600;
- _global.plane_height = 600;
- _global.plane_x_offset = _root.p0._x;
- _global.plane_y_offset = _root.p0._y;
- //размер сегмента
- _global.plane_xsegm = plane_width/10;
- _global.plane_ysegm = plane_height/10;
- _global.plane_scale = plane_xsegm/15;
- //полотно для вывода графика
- _global.graph_width = 200;
- _global.graph_height = 200;
- _global.graph_x_offset = _root.g0._x;
- _global.graph_y_offset = _root.g0._y;
- _global.graph_xsegm = graph_width/60;
- _global.graph_ysegm = graph_height/60;
- _global.graph_scale = graph_xsegm;
- init = function()
- {
- init_clips();
- add_ui_listeners();
- planes_bg_init();
- graph_bg_init();
- redraw();
- };
- //загрузка клипов на сцену и установка начальных параметров
- init_clips = function()
- {
- _global.body = p1.attachMovie("body", "body", getNextHighestDepth());
- body.plane_length = 100;
- body.g = 10;
- body.dst_frm_0 = 200;
- body.m = sel_m.value;
- body.Fouter = sel_Fouter.value;
- body.CoF = sel_CoF.value;
- body.a = sel_a.value * math.PI / 180;
- _global.grdot = g3.attachMovie("dot", "grdot", getNextHighestDepth());
- var vect = new Object();
- vect.mg = _root.attachMovie("vlbl_mg", "vlbl_mg", getNextHighestDepth());
- vect.ma = _root.attachMovie("vlbl_ma", "vlbl_ma", getNextHighestDepth());
- vect.N = _root.attachMovie("vlbl_N", "vlbl_N", getNextHighestDepth());
- vect.Fouter = _root.attachMovie("vlbl_Fouter", "vlbl_Fouter", getNextHighestDepth());
- vect.FoF = _root.attachMovie("vlbl_FoF", "vlbl_FoF", getNextHighestDepth());
- a_lbl = _root.attachMovie("vlbl_a", "vlbl_a", getNextHighestDepth());
- a_lbl._x = 515; a_lbl._y = 325;
- _global.vect = vect;
- };
- //перерисовка тела и графика после изменения параметров
- redraw = function()
- { //trace(">>> redraw");
- draw_planes();
- initial_pos();
- pos_body();
- redraw_graph();
- sel_m_lbl.text = body.m + lbl.kg;
- sel_Fouter_lbl.text = body.Fouter + lbl.Newton;
- sel_CoF_lbl.text = body.CoF;
- sel_a_lbl.text = body.a / Math.PI * 180 + lbl.a;
- if (sel_a.value < 20)
- a_lbl._y = -999;
- else
- a_lbl._y = 325;
- };
- //навешивание обработчиков на контролы
- add_ui_listeners = function()
- { //trace(">>> add_ui_listeners ");
- start_button.onRelease = function()
- {
- if (body.accel != 0 && grdot._x == 0) {
- slide_body();
- start_button.label = lbl.reset;
- pause_button.enable();
- } else {
- _root.onEnterFrame = null;
- banzai.gotoAndStop(1);
- body.v = 0;
- redraw();
- start_button.label = lbl.launch;
- };
- };
- play_button.actions = function() {
- _root.onEnterFrame = function() {
- step();
- };
- play_button.disable();
- step_button.disable();
- pause_button.enable();
- };
- step_button.actions = function() {
- if (!_root.onEnterFrame) {
- step();
- };
- };
- pause_button.actions = function() {
- _root.onEnterFrame = null;
- play_button.enable();
- step_button.enable();
- pause_button.disable();
- };
- var slideListener:Object = new Object();
- slideListener.onChange = function(evnt:Object)
- {
- if (grdot._x != 0)
- return;
- switch(String(evnt.target)) {
- case "_level0.sel_m":
- body.m = evnt.value;
- break;
- case "_level0.sel_Fouter":
- body.Fouter = evnt.value;
- break;
- case "_level0.sel_CoF":
- body.CoF = evnt.value;
- break;
- case "_level0.sel_a":
- body.a = evnt.value * Math.PI / 180;
- };
- redraw();
- };
- sel_m.addListener(slideListener);
- sel_Fouter.addListener(slideListener);
- sel_CoF.addListener(slideListener);
- sel_a.addListener(slideListener);
- };
- //движение тела
- slide_body = function()
- {
- if(body.accel == 0)
- return;
- banzai.gotoAndPlay(2);
- _global.body.v = 0;
- _root.onEnterFrame = function()
- {
- step();
- };
- };
- step = function()
- { //trace("step");
- body.v += body.accel/15;
- body.x -= body.v * Math.cos(body.a);
- body.y -= body.v * Math.sin(body.a);
- //if(body.x > 10 && body.x < 300) { //приехали?
- if(body.x > 0 && body.x < 300) { //приехали?
- pos_body();
- grdot._x = body.v * graph_scale;
- } else {
- _root.onEnterFrame = null;
- pause_button.disable();
- };
- };
- //начальная позиция
- initial_pos = function()
- {
- body.x = body.dst_frm_0 * Math.cos(body.a);
- body.y = body.dst_frm_0 * Math.sin(body.a);
- grdot._x = 0;
- };
- //перемещение тела и рассчет векторов
- pos_body = function()
- {
- var x_dec, y_dec, v, body_center_point, Fsum;
- body._x = body.x;
- body._y = body.y;
- var body_center_point:Object = {x:0, y:0};
- body.center.localToGlobal(body_center_point);
- p3.globalToLocal(body_center_point);
- body.x7 = body_center_point.x; //M_p * Math.cos(M_b);
- body.y7 = body_center_point.y; //M_p * Math.sin(M_b);
- p3.clear(); p3.lineStyle(2, 0x559955);
- v = vect.mg;
- v.magn = body.m * body.g;
- v.x0 = body.x7;
- v.y0 = body.y7;
- v.x_proj = 0;
- v.y_proj = v.magn;
- v.x1 = body.x7;
- v.y1 = body.y7 + v.y_proj * -plane_scale;
- vector(p3, v.x0, v.y0, v.x1, v.y1, vect.mg);
- v = vect.N;
- v.magn = vect.mg.magn * Math.cos(body.a);
- v.x0 = body.x7;
- v.y0 = body.y7;
- if(body.a != 0) {
- v.x_proj = v.magn * Math.cos(Math.PI/2 + body.a);
- } else {
- v.x_proj = 0;
- };
- v.y_proj = v.magn * Math.sin(Math.PI/2 + body.a);
- v.x1 = body.x7 + v.x_proj * plane_scale;
- v.y1 = body.y7 + v.y_proj * plane_scale;
- vector(p3, v.x0, v.y0, v.x1, v.y1, vect.N);
- body.frict_max = body.CoF * vect.N.magn;
- v = vect.Fouter;
- v.magn = body.Fouter;
- v.x0 = body.x7;
- v.y0 = body.y7;
- v.x1 = body.x7 - v.magn * Math.cos(body.a) * plane_scale;
- v.y1 = body.y7 - v.magn * Math.sin(body.a) * plane_scale;
- vector(p3, v.x0, v.y0, v.x1, v.y1, vect.Fouter);
- v = vect.ma;
- v.magn = Math.sqrt(Math.pow((vect.N.x_proj), 2) + Math.pow(vect.N.y_proj - vect.mg.y_proj, 2));
- v.x0 = body.x7;
- v.y0 = body.y7;
- v.x1 = body.x7 + ((vect.N.x1 - body.x7) + (vect.mg.x1 - body.x7));
- v.y1 = body.y7 + ((vect.N.y1 - body.y7) + (vect.mg.y1 - body.y7));
- vector(p3, v.x0, v.y0, v.x1, v.y1, vect.ma);
- p1.clear();
- p1.lineStyle(0, 0xDDDDDD);
- line(p1, vect.N.x1, vect.N.y1, v.x1, v.y1, vect.ma);
- line(p1, vect.mg.x1, vect.mg.y1, v.x1, v.y1, vect.ma);
- v = vect.FoF;
- Fsum = vect.ma.magn + vect.Fouter.magn;
- if (Fsum > 0) {
- if (Fsum > body.frict_max)
- v.magn = body.frict_max;
- else if (Fsum < body.frict_max)
- v.magn = Fsum;
- } else if (Fsum < 0) {
- if (Fsum < -body.frict_max)
- v.magn = -body.frict_max;
- if (Fsum > -body.frict_max)
- v.magn = Fsum;
- };
- v.magn *= -1;
- v.x0 = body.x7;
- v.y0 = body.y7;
- v.x1 = body.x7 - v.magn * Math.cos(body.a) * plane_scale;
- v.y1 = body.y7 - v.magn * Math.sin(body.a) * plane_scale;
- p3.lineStyle(1, 0x005555)
- vector(p3, v.x0, v.y0, v.x1, v.y1, vect.FoF);
- body._rotation = body.a / Math.PI * 180;
- body.Fsum = vect.ma.magn + vect.Fouter.magn + vect.FoF.magn;
- body.accel = body.Fsum / body.m;
- N_lbl.text = round_01(vect.N.magn) + lbl.Newton;
- CoFN_lbl.text = round_01(body.CoF * vect.N.magn) + lbl.Newton;
- Fsum_lbl.text = round_01(body.Fsum) + lbl.Newton;
- FoF_lbl.text = round_01(vect.FoF.magn) + lbl.Newton;
- accel_lbl.text = round_01(body.accel) + lbl.ms2;
- tm_round(
- lbl.ma + "=", vect.ma.magn,
- lbl.Fsum + "=", vect.ma.magn - vect.Fouter.magn - vect.FoF.magn,
- lbl.acc + "=", body.accel, "v=", body.v);
- //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)); */
- };
- //перерисовка графика
- redraw_graph = function()
- {
- var Ktr = -body.frict_max * graph_scale;
- g1.clear();
- g1.lineStyle(2, 0xFF5500);
- line(g1, graph_width/2, Ktr, 0, Ktr);
- line(g1, 0, Ktr, 0, -Ktr);
- line(g1, 0, -Ktr, -graph_width/2, -Ktr);
- grdot._y = vect.FoF.magn * graph_scale;
- };
- //рисование плоскостей
- draw_planes = function()
- {
- var x_dec, y_dec;
- p2.clear(); p2.lineStyle(1, 0x995555)
- line(p2, 0, 0, 300, 0);
- x_dec = 300 * Math.cos(body.a);
- y_dec = 300 * Math.sin(body.a);
- line(p2, 0, 0, x_dec, y_dec);
- p2.moveTo(50, 0);
- p2.drawArc(50, 0, 50, -body.a * 180 / Math.PI, 0);
- };
- //оси координат на на плоскости с телом
- planes_bg_init = function()
- {
- p0.lineStyle(1, 0xFFEEEE);
- line(p0, -plane_width / 2, 0, plane_width / 2, 0);
- line(p0, -plane_width / 2, 0, plane_width / 2, 0);
- line(p0, 0, -plane_height / 2, 0, plane_height / 2);
- p0.moveTo(plane_width/2 -10, 10);
- p0.lineTo(plane_width/2, 0);
- p0.lineTo(plane_width/2 -10, -10);
- p0.moveTo(-10, plane_height/2 - 10);
- p0.lineTo(0, plane_height/2);
- p0.lineTo(10, plane_height/2 - 10);
- };
- //разметка полотна графика, рисование осей координат, сетки и подписей
- graph_bg_init = function()
- {
- var i:Number, x_grid:Number, y_grid:Number, tmp_obj:Object;
- g0.lineStyle(1, 0xE0E0E0, 100); //серые линии
- for(i = -30; i <= 30; i = i + 10) { //рисование линий
- x_grid = graph_width / 60 * i;
- y_grid = graph_width / 60 * i;
- line(g0, x_grid, graph_width/2, x_grid, -graph_width/2);
- line(g0, graph_height/2, y_grid, -graph_height/2, y_grid);
- //подпись координат
- var my_fmt:TextFormat = new TextFormat();
- my_fmt.color = 0x000000;
- my_fmt.align = "center";
- my_fmt.size = 13;
- if(i / 5 == Math.round(i / 5 )) {
- tmp_obj = _root.createTextField("g0_label_x"+String(i), _root.getNextHighestDepth(), x_grid - 12, 0, 25, 30); //координаты по Х
- tmp_obj.text = i; tmp_obj.setTextFormat(my_fmt);
- tmp_obj._x += graph_x_offset;
- tmp_obj._y = graph_y_offset - tmp_obj._y;
- };
- my_fmt.align = "right";
- if((i / 5 == Math.round(i / 5 )) && i != 0) {
- tmp_obj = _root.createTextField("g0_label_y"+String(i), _root.getNextHighestDepth(), -24, y_grid + 8, 25, 30); //координаты по Х
- tmp_obj.text = i; tmp_obj.setTextFormat(my_fmt);
- tmp_obj._x += graph_x_offset;
- tmp_obj._y = graph_y_offset - tmp_obj._y;
- };
- };
- my_fmt.size = 18;
- my_fmt.align = "left";
- tmp_obj = _root.createTextField("g0_label_x_axe", _root.getNextHighestDepth(), graph_width/2 + 10, 0, 50, 50); //ось Х
- tmp_obj.text = lbl.vms;
- tmp_obj.setTextFormat(my_fmt);
- tmp_obj._x += graph_x_offset;
- tmp_obj._y = graph_y_offset - tmp_obj._y;
- tmp_obj = _root.createTextField("g0_label_y_axe", _root.getNextHighestDepth(), 2, graph_height/2 + 20, 50, 50); //координаты по Х
- tmp_obj.text = lbl.FH;
- tmp_obj.setTextFormat(my_fmt);
- tmp_obj._x += graph_x_offset;
- tmp_obj._y = graph_y_offset - tmp_obj._y;
- g0.lineStyle(1, 0xFF3333);
- line(g0, -graph_width / 2, 0, graph_width / 2, 0);
- line(g0, -graph_width / 2, 0, graph_width / 2, 0);
- line(g0, 0, -graph_height / 2, 0, graph_height / 2);
- g0.moveTo(graph_width/2 -10, 10);
- g0.lineTo(graph_width/2, 0);
- g0.lineTo(graph_width/2 -10, -10);
- g0.moveTo(-10, graph_height/2 - 10);
- g0.lineTo(0, graph_height/2);
- g0.lineTo(10, graph_height/2 - 10);
- g3.swapDepths(_root.getNextHighestDepth());
- };
- //служебные функции
- round_01 = function(rounded)
- { //обрезание строки до 2 символа после точки включительно
- if(String(rounded).indexOf(".") != -1)
- return(String(rounded).slice(0, String(rounded).indexOf(".") + 3));
- return(rounded);
- };
- line = function(clip:Object, x1:Number, y1:Number, x2:Number, y2:Number)
- { //рисование линии по 4 координатам
- clip.moveTo(x1,y1); clip.lineTo(x2,y2);
- };
- 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)
- {
- var trace_string:String = "";
- for(i = 0; i <= arguments.length-1; i++) {
- if (arguments[i] != undefined)
- trace_string += round_01(arguments[i]);
- if (Math.round(i/2) != i/2)
- trace_string += "\t\t";
- };
- //trace(trace_string);
- param.text = trace_string;
- };
- //функция для рисования векторов
- vector = function(clip, x1, y1, x2, y2, label)
- {
- var p, a, a_90;
- p = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
- a = Math.atan2(y2 - y1, x2 - x1);
- a_90 = (a + Math.PI / 2);
- //координаты для стрелок векторов
- x_dec = (p - 7) * Math.cos(a);
- y_dec = (p - 7) * Math.sin(a);
- x1_dec = 5 * Math.cos(a_90);
- y1_dec = 5 * Math.sin(a_90);
- if (Math.abs(x2 - x1) >= 5 || Math.abs(y2 - y1) >= 5) {
- line(clip, x1, y1, x2, y2);
- line(clip, x_dec+x1+x1_dec, y_dec+y1+y1_dec, x2, y2);
- line(clip, x_dec+x1-x1_dec, y_dec+y1-y1_dec, x2, y2);
- x_dec = (p + 15) * Math.cos(a);
- y_dec = (p + 15) * Math.sin(a);
- vector_label(x_dec + x1, y_dec + y1, label);
- } else {
- vector_label(-999, -999, label);
- };
- };
- //подписи к векторам
- vector_label = function(x, y, label)
- {
- x += plane_x_offset;
- y = plane_y_offset - y;
- label._x = x;
- label._y = y;
- };
- //алиас к трейсу, чтобы удобнее вызывать
- 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)
- {
- var trace_string:String = "";
- for(i = 0; i <= 15; i ++) {
- if (arguments[i] != undefined)
- trace_string += arguments[i] + " \t";
- };
- trace(trace_string);
- param.text = trace_string;
- };
- init(); //поехали (:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement