Advertisement
Guest User

физика, размерность в СИ, работает

a guest
May 27th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.59 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <iostream>
  3. #include <math.h>
  4. #include <cmath>
  5. #include <sstream>
  6. #include <ctime>
  7. #include <windows.h>
  8. #include <conio.h>
  9. #include <stdio.h>
  10.  
  11. #define PI 3.14159265
  12.  
  13. const int size = 20;
  14. const double g = 9.8;
  15. const int indent = 50; //отступ при рисовании (раньше было 30)
  16. const int size_angle_point = 5; //размер отметки поворота на круге
  17. const int len = 600;
  18.  
  19. using namespace sf;
  20. using namespace std;
  21.  
  22. int main()
  23. {
  24.     setlocale(LC_ALL, "Russian");
  25.     CircleShape shape(10);
  26.     CircleShape angle_point(10);
  27.     Font fnt;
  28.     fnt.loadFromFile("CyrilicOld.ttf");
  29.     Text txt1("", fnt, 30);
  30.     txt1.setColor(Color::Red);
  31.     txt1.setStyle(Text::Bold);
  32.     Text txt2("", fnt, 30);
  33.     txt2.setColor(Color::Red);
  34.     txt2.setStyle(Text::Bold);
  35.     Text txt3("", fnt, 30);
  36.     txt3.setColor(Color::Red);
  37.     txt3.setStyle(Text::Bold);
  38.     Text txt4("", fnt, 30);
  39.     txt4.setColor(Color::White);
  40.     txt4.setStyle(Text::Bold);
  41.     Text txt5("", fnt, 30);
  42.     txt5.setColor(Color::White);
  43.     txt5.setStyle(Text::Bold);
  44.     Text txt6("", fnt, 30);
  45.     txt6.setColor(Color::White);
  46.     txt6.setStyle(Text::Bold);
  47.     Text txt7("", fnt, 30);
  48.     txt7.setColor(Color::White);
  49.     txt7.setStyle(Text::Bold);
  50.     RectangleShape line(Vector2f(len, 5));
  51.     shape.setFillColor(Color::Green);
  52.     angle_point.setFillColor(Color::Red);
  53.  
  54.     double x = 0, y = 0;
  55.  
  56.     int type = 0;
  57.     while((type < 1) || (type > 3))
  58.     {
  59.         cout << "Введите тип тела (1 - обруч, 2 - цилиндр, 3 - шар) : ";
  60.         cin >> type;
  61.     }
  62.  
  63.     double mas = -1.0;
  64.     while(mas <= 0)
  65.     {
  66.         cout << "Введите вес (кг, больше 0) : ";
  67.         cin >> mas;
  68.     }
  69.  
  70.     double rad = -1.0;
  71.     double flag_pointer = false;
  72.    
  73.     while ((rad > 50) || (rad <= 0))
  74.     {
  75.         cout << "Введите радиус (метры, больше 0 и до 50) : ";
  76.         cin >> rad;
  77.     }
  78.     shape.setRadius(rad);
  79.     if ((rad / 2) >= size_angle_point)
  80.     {
  81.         angle_point.setRadius(size_angle_point);
  82.         flag_pointer = true;
  83.     }
  84.  
  85.     int alpha = -1;
  86.     while ((alpha >= 90) || (alpha <= 0))
  87.     {
  88.         cout << "Введите угол наклонной плоскости (градусы, от 0 до 90) : ";
  89.         cin >> alpha;
  90.     }
  91.     line.rotate(alpha);
  92.    
  93.     shape.setPosition(indent, indent);
  94.     line.setPosition(indent + rad - rad*sin(alpha * PI / 180), indent + rad + rad*cos(alpha * PI / 180));
  95.  
  96.     double I;
  97.     if (type == 1) I = mas * rad * rad;
  98.     if (type == 2) I = mas * rad * rad / 2;
  99.     if (type == 3) I = 2 * mas * rad * rad / 5;
  100.  
  101.     double a = (mas * g * sin(alpha * PI / 180)) / ((I / (rad * rad)) + mas);
  102.  
  103.     double ax = a * cos(alpha * PI / 180);
  104.     double ay = a * sin(alpha * PI / 180);
  105.  
  106.     double angle = 0; //in rad
  107.     double angle_deg = 0;
  108.     angle_point.setPosition
  109.     (indent + rad + (rad * cos(angle) / 2) - size_angle_point,
  110.      indent + rad + (rad * sin(angle) / 2) - size_angle_point);
  111.  
  112.     double E = a / rad;
  113.     double t = 0.0;
  114.     double V = 0.0;
  115.    
  116.     RenderWindow window(VideoMode(1100, 1000), "WORK", Style::Fullscreen);
  117.  
  118.     while (window.isOpen())
  119.     {
  120.         Event event;
  121.         while (window.pollEvent(event))
  122.         {
  123.             if (event.type == Event::Closed)
  124.                 window.close();
  125.         }
  126.    
  127.         ostringstream astring;
  128.         ostringstream estring;
  129.         ostringstream xstring;
  130.         ostringstream ystring;
  131.         ostringstream angstring;
  132.         ostringstream angdegstring;
  133.         ostringstream speedstring;
  134.         ostringstream istring;
  135.  
  136.         double i_out = floor(I * 100) / 100.0;
  137.         double a_out = floor(a * 10) / 10.0;
  138.         double x_out = floor(x * 10) / 10.0;
  139.         double y_out = floor(y * 10) / 10.0;
  140.         double E_out = floor(E * 1000) / 1000.0;
  141.         double angle_out = floor(angle * 10) / 10.0;
  142.         double angle_deg_out = floor(angle_deg * 10) / 10.0;
  143.         double V_out = floor(V * 10) / 10.0;
  144.  
  145.         istring << i_out;
  146.         astring << a_out;
  147.         xstring << x_out;
  148.         estring << E_out;
  149.         ystring << y_out;
  150.         angstring << angle_out;
  151.         angdegstring << angle_deg_out;
  152.         speedstring << V_out;
  153.  
  154.         txt1.setString("Момент инерции = " + istring.str() + " " + "кг*м^2");
  155.         txt1.setPosition(870, 50);
  156.         txt2.setString("Ускорение = " + astring.str() + " " + "м/с^2");
  157.         txt2.setPosition(870, 150);
  158.         txt3.setString("Угловое ускорение = " + estring.str());
  159.         txt3.setPosition(870, 250);
  160.         txt4.setString("Координаты (" + xstring.str() + ";" + ystring.str() + ")");
  161.         txt4.setPosition(870, 350);
  162.         txt5.setString("Угол поворота = " + angstring.str() + " " + "рад");
  163.         txt5.setPosition(870, 450);
  164.         txt6.setString("Угол поворота = " + angdegstring.str() + " " + "°");
  165.         txt6.setPosition(870, 550);
  166.         txt7.setString("Скорость = " + speedstring.str() + " " + "м/c");
  167.         txt7.setPosition(870, 650);
  168.  
  169.         if ((x < len * cos(alpha * PI / 180)) && !(Keyboard::isKeyPressed(Keyboard::Space)))
  170.         {
  171.             t = t + 0.01;
  172.             x = ax * t * t / 2;
  173.             y = ay * t * t / 2;
  174.             V = a * t;
  175.             shape.setPosition(indent + (ax * t * t / 2), indent + (ay * t * t / 2));
  176.             angle = E * t * t / 2;
  177.             while (angle > 2 * PI) angle = angle - 2 * PI;
  178.             angle_deg = angle * 180 / PI;
  179.             angle_point.setPosition
  180.             (indent + (ax * t * t / 2) + rad + (rad * cos(angle) / 2) - size_angle_point,
  181.              indent + (ay*t*t/2) + rad + (rad * sin(angle) / 2) - size_angle_point);
  182.         }
  183.  
  184.         window.clear();
  185.         window.draw(txt1);
  186.         window.draw(txt2);
  187.         window.draw(txt3);
  188.         window.draw(txt4);
  189.         window.draw(txt5);
  190.         window.draw(txt6);
  191.         window.draw(txt7);
  192.         window.draw(shape);
  193.         if(flag_pointer)
  194.         {
  195.             window.draw(angle_point);
  196.         }
  197.         window.draw(line);
  198.         window.display();
  199.  
  200.         Sleep(10);
  201.         if(Keyboard::isKeyPressed(Keyboard::Escape)) break;
  202.     }
  203.  
  204.     return 0;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement