Advertisement
Kostil_Uranio

Untitled

Jan 29th, 2022
1,288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.16 KB | None | 0 0
  1. //Аношин Сергей
  2. #include <iostream>
  3. #include <SFML/Graphics.hpp>
  4. #include <cmath>
  5. #include <cstdlib>
  6. #define PI 3,1415
  7.  
  8. using namespace sf;
  9. using namespace std;
  10.  
  11. int WIN_HEIGHT = 720;
  12. int WIN_WIDTH = 1080;
  13. int X_ZERO = WIN_WIDTH / 2;
  14. int Y_ZERO = WIN_HEIGHT / 2;
  15.  
  16. void axes(RenderWindow& window, Font& font) {
  17.     VertexArray axis_x(Lines, 2);
  18.     axis_x[0] = Vector2f(0, Y_ZERO);
  19.     axis_x[1] = Vector2f(WIN_WIDTH, Y_ZERO);
  20.     axis_x[0].color = axis_x[1].color = Color::Black;
  21.  
  22.     VertexArray axis_y(Lines, 2);
  23.     axis_y[0] = Vector2f(X_ZERO, WIN_HEIGHT);
  24.     axis_y[1] = Vector2f(X_ZERO, 0);
  25.     axis_y[0].color = axis_y[1].color = Color::Black;
  26.     /*
  27.     Vertex arrows[6];
  28.     int size = WIN_WIDTH / 100;
  29.     arrows[0] = Vertex(Vector2f(WIN_WIDTH - size, Y_ZERO + size / 2));
  30.     arrows[1] = Vertex(Vector2f(WIN_WIDTH, Y_ZERO));
  31.     arrows[2] = Vertex(Vector2f(WIN_WIDTH - size, Y_ZERO - size / 2));
  32.     arrows[0].color = arrows[1].color = arrows[2].color = Color::Black;
  33.  
  34.     arrows[3] = Vertex(Vector2f(X_ZERO - size / 2, size));
  35.     arrows[4] = Vertex(Vector2f(X_ZERO, 0));
  36.     arrows[5] = Vertex(Vector2f(X_ZERO + size / 2, size));
  37.     arrows[3].color = arrows[4].color = arrows[5].color = Color::Black;
  38.    
  39.  
  40.     Text text;
  41.     text.setFont(font);
  42.     text.setString("axis X");
  43.     text.setCharacterSize(16);
  44.     text.setFillColor(Color::Black);
  45.     text.setStyle(Text::Regular);
  46.     text.setPosition(WIN_WIDTH - 50, Y_ZERO + 10);
  47.     window.draw(text);
  48.  
  49.     text.setFont(font);
  50.     text.setString("axis Y");
  51.     text.setCharacterSize(14);
  52.     text.setFillColor(Color::Black);
  53.     text.setStyle(Text::Regular);
  54.     text.setPosition(X_ZERO + 10, 10);
  55.     window.draw(text);
  56.     */
  57.     window.draw(axis_x);
  58.     window.draw(axis_y);
  59.     //window.draw(arrows, 6, Triangles);
  60.    
  61. }
  62.  
  63. void ln(RenderWindow& window, int x_strain, int y_strain, int x_displace, int y_displace) {
  64.     double step = WIN_WIDTH / 1000;
  65.     double k = 10;
  66.     VertexArray ln(LineStrip, 1000);
  67.     for (int i = 0; i < 1000; i++) {
  68.         ln[i].position.x = x_displace + X_ZERO + x_strain * (i * step);
  69.         ln[i].position.y = y_displace + Y_ZERO - y_strain * log(i / k);
  70.         ln[i].color = Color::Red;
  71.     }
  72.     window.draw(ln);
  73.  
  74.  
  75.     /*
  76.     double step = WIN_WIDTH / 300;
  77.     Vertex curve[500];
  78.     double k = 10;
  79.     double t = 50;
  80.     for (int i = 1; i < 500; i++) {
  81.         curve[i] = Vertex(Vector2f((X_ZERO + i * step), (Y_ZERO - t * log(i / k))));
  82.         curve[i].color = Color::Red;
  83.     }
  84.  
  85.     for (int i = 0; i < 500; i++) {
  86.         window.draw(curve, 500, Lines);
  87.     }
  88.     */
  89. }
  90. /*
  91. void net(RenderWindow& window) {
  92.     Vertex line[2];
  93.     double step = 50;
  94.     for (int i = 0; i <= WIN_HEIGHT / step; i++) {
  95.         line[0] = Vertex(Vector2f(0, i * step));
  96.         line[1] = Vertex(Vector2f(WIN_WIDTH, i * step));
  97.         line[0].color = line[1].color = Color(168, 168, 168);
  98.  
  99.         if (i * step != Y_ZERO)
  100.             window.draw(line, 2, Lines);
  101.     }
  102.  
  103.     for (int i = 0; i <= WIN_WIDTH / step; i++) {
  104.         line[0] = Vertex(Vector2f(i * step, 0));
  105.         line[1] = Vertex(Vector2f(i * step, WIN_HEIGHT));
  106.         line[0].color = line[1].color = Color(168, 168, 168);
  107.  
  108.         if (i * step != X_ZERO)
  109.             window.draw(line, 2, Lines);
  110.     }
  111.    
  112. }
  113.  
  114. void show_lab(RenderWindow& window) {
  115.     int Numb_vert = 5;
  116.     double step = (2 * PI) / (Numb_vert + 1);
  117.     int X_ZERO = WIN_WIDTH / 2;
  118.     int Y_ZERO = WIN_HEIGHT / 2;
  119.     int R = 300;
  120.     int r = 30;
  121.     CircleShape circle(r);
  122.     for (int i = 0; i <= Numb_vert; i++) {
  123.         circle.setPosition(X_ZERO + cos(i * step), Y_ZERO - sin(i * step));
  124.         circle.setFillColor(Color(230, 0, 230));
  125.         window.draw(circle);
  126.     }
  127.  
  128. }
  129. */
  130. int main() {
  131.     int x_strain; //1
  132.     int y_strain; //40
  133.  
  134.     cout << "enter x_strain" << endl;
  135.     cin >> x_strain;
  136.  
  137.     cout << "enter y_strain" << endl;
  138.     cin >> y_strain;
  139.  
  140.     int x_displace = 0;
  141.     int y_displace = 0;
  142.  
  143.     RenderWindow window(VideoMode(WIN_WIDTH, WIN_HEIGHT), "Graph");
  144.     Font font;
  145.     if (!font.loadFromFile("Roboto-Black.ttf"))
  146.     {
  147.         cout << "loading error" << endl;
  148.     }
  149.  
  150.  
  151.     while (window.isOpen()) {
  152.         // Конструктор обработчика событий
  153.         Event event;
  154.         while (window.pollEvent(event)) {
  155.             if (event.type == Event::KeyPressed) {
  156.                 if (event.key.code == Keyboard::Right)
  157.                     x_displace += 5;
  158.  
  159.                 if (event.key.code == Keyboard::Left)
  160.                     x_displace += -5;
  161.  
  162.                 if (event.key.code == Keyboard::Up)
  163.                     y_displace += -5;
  164.  
  165.                 if (event.key.code == Keyboard::Down)
  166.                     y_displace += 5;
  167.             }
  168.  
  169.             if (event.type == Event::Closed)
  170.                 window.close();
  171.         }
  172.         // Вызовы функций
  173.         window.clear(Color::White);
  174.         //net(window);
  175.         ln(window, x_strain, y_strain, x_displace, y_displace);
  176.         //show_lab(window);
  177.         axes(window, font);
  178.         window.display();
  179.  
  180.     }
  181.     system("pause");
  182.     return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement