Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <SFML/Graphics.hpp>
- #include <cmath>
- #include <cstdlib>
- using namespace sf;
- using namespace std;
- int WIN_HEIGHT = 720;
- int WIN_WIDTH = 1080;
- int X_ZERO = WIN_WIDTH / 2;
- int Y_ZERO = WIN_HEIGHT / 2;
- void axes(RenderWindow& window, Font& font) {
- VertexArray axis_x(Lines, 2);
- axis_x[0] = Vector2f(0, Y_ZERO);
- axis_x[1]= Vector2f(WIN_WIDTH, Y_ZERO);
- axis_x[0].color = axis_x[1].color = Color::Black;
- VertexArray axis_y(Lines, 2);
- axis_y[0] = Vector2f(X_ZERO, WIN_HEIGHT);
- axis_y[1] = Vector2f(X_ZERO, 0);
- axis_y[0].color = axis_y[1].color = Color::Black;
- Vertex arrows[6];
- int size = WIN_WIDTH / 100;
- arrows[0] = Vertex(Vector2f(WIN_WIDTH - size, Y_ZERO + size / 2));
- arrows[1] = Vertex(Vector2f(WIN_WIDTH, Y_ZERO));
- arrows[2] = Vertex(Vector2f(WIN_WIDTH - size, Y_ZERO - size / 2));
- arrows[0].color = arrows[1].color = arrows[2].color = Color::Black;
- arrows[3] = Vertex(Vector2f(X_ZERO - size / 2, size));
- arrows[4] = Vertex(Vector2f(X_ZERO, 0));
- arrows[5] = Vertex(Vector2f(X_ZERO + size / 2, size));
- arrows[3].color = arrows[4].color = arrows[5].color = Color::Black;
- Text text;
- text.setFont(font);
- text.setString("axis X");
- text.setCharacterSize(16);
- text.setFillColor(Color::Black);
- text.setStyle(Text::Regular);
- text.setPosition(WIN_WIDTH - 50, Y_ZERO + 10);
- window.draw(text);
- text.setFont(font);
- text.setString("axis Y");
- text.setCharacterSize(14);
- text.setFillColor(Color::Black);
- text.setStyle(Text::Regular);
- text.setPosition(X_ZERO + 10, 10);
- window.draw(text);
- window.draw(axis_x);
- window.draw(axis_y);
- window.draw(arrows, 6, Triangles);
- }
- void ln(RenderWindow& window, int x_strain, int y_strain, int x_displace, int y_displace) {
- double step = WIN_WIDTH / 1000;
- double k = 10;
- VertexArray ln(LineStrip, 1000);
- for (int i = 0; i < 1000; i++) {
- ln[i].position.x = x_displace + X_ZERO + x_strain * (i * step);
- ln[i].position.y = y_displace + Y_ZERO - y_strain * log(i / k);
- ln[i].color = Color::Black;
- }
- window.draw(ln);
- /*
- double step = WIN_WIDTH / 300;
- Vertex curve[500];
- double k = 10;
- double t = 50;
- for (int i = 1; i < 500; i++) {
- curve[i] = Vertex(Vector2f((X_ZERO + i * step), (Y_ZERO - t * log(i / k))));
- curve[i].color = Color::Red;
- }
- for (int i = 0; i < 500; i++) {
- window.draw(curve, 500, Lines);
- }
- */
- }
- void net(RenderWindow& window) {
- Vertex line[2];
- double step = 30;
- for (int i = 0; i <= WIN_HEIGHT / step; i++) {
- line[0] = Vertex(Vector2f(0, i * step));
- line[1] = Vertex(Vector2f(WIN_WIDTH, i * step));
- line[0].color = line[1].color = Color(168, 168, 168);
- if (i * step != Y_ZERO)
- window.draw(line, 2, Lines);
- }
- for (int i = 0; i <= WIN_WIDTH / 30; i++) {
- line[0] = Vertex(Vector2f(i * step, 0));
- line[1] = Vertex(Vector2f(i * step, WIN_HEIGHT));
- line[0].color = line[1].color = Color(168, 168, 168);
- if (i * step != X_ZERO)
- window.draw(line, 2, Lines);
- }
- }
- int main() {
- int x_strain; //1
- int y_strain; //40
- cout << "enter x_strain" << endl;
- cin >> x_strain;
- cout << "enter y_strain" << endl;
- cin >> y_strain;
- int x_displace = 0;
- int y_displace = 0;
- RenderWindow window(VideoMode(WIN_WIDTH, WIN_HEIGHT), "Graph");
- Font font;
- if (!font.loadFromFile("Roboto-Black.ttf"))
- {
- cout << "loading error" << endl;
- }
- while (window.isOpen()) {
- // Конструктор обработчика событий
- Event event;
- while (window.pollEvent(event)) {
- if (event.type == Event::KeyPressed) {
- if (event.key.code == Keyboard::Right)
- x_displace += 5;
- if (event.key.code == Keyboard::Left)
- x_displace += -5;
- if (event.key.code == Keyboard::Up)
- y_displace += -5;
- if (event.key.code == Keyboard::Down)
- y_displace += 5;
- }
- if (event.type == Event::Closed)
- window.close();
- }
- // Вызовы функций
- window.clear(Color::White);
- net(window);
- ln(window, x_strain, y_strain, x_displace, y_displace);
- axes(window, font);
- window.display();
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement