Advertisement
Kostil_Uranio

graph

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