@karinar

Как вывести график только по точкам заданного диапазона?

Здравствуйте! Подскажите пожалуйста, как построить графики, по точкам, выводимых в таблице?
#include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
 
const int width = 800;
const int height = 600;
 
// функция для расчета первой функции
float function1(float x) {
    // здесь нужно написать код расчета первой функции
    // например, можно использовать какую-то формулу
    return 2 * x - 4;
}
 
// функция для расчета второй функции
float function2(float x) {
    // здесь нужно написать код расчета второй функции
    // например, можно использовать другую формулу
    return 2 / x - 4;
}
 
// функция для вывода таблицы значений в консоль и в файл
void printtable(float start, float end) {
    ofstream file("table.txt"); // открываем файл для записи
 
    // выводим шапку таблицы
    cout << "x\tf1(x)\tf2(x)" << endl;
    file << "x\tf1(x)\tf2(x)" << endl;
 
    float step = (end - start) / 20; // расчитываем шаг аргумента
 
    // выводим значения функций для каждого значения аргумента
    for (float x = start; x <= end; x += step) {
        float value1 = function1(x);
        float value2 = function2(x);
 
        cout << x << "\t";
 
        // проверяем, удалось ли вычислить значение функции
        if (isfinite(value1)) {
            cout << value1;
        }
        else {
            cout << "-";
        }
 
        cout << "\t";
 
        // проверяем, удалось ли вычислить значение функции
        if (isfinite(value2)) {
            cout << value2;
        }
        else {
            cout << "-";
        }
 
        cout << endl;
 
        file << x << "\t" << value1 << "\t" << value2 << endl;
    }
 
    file.close(); // закрываем файл
}
 
int main() {
    setlocale(LC_ALL, "");
    float start, end;
 
    cout << "введите начальное значение интервала: ";
    cin >> start;
 
    cout << "введите конечное значение интервала: ";
    cin >> end;
 
    cout << "f1(x) = pow(x + exp(x), 1 / 15)" << endl;
    cout << "f2(x) = pow(x + exp(x), 1 / 25)" << endl;
 
    printtable(start, end);
 
    sf::RenderWindow window(sf::VideoMode(width, height), "график функции");
 
    sf::VertexArray x_axis(sf::Lines, 2);
    x_axis[0].position = sf::Vector2f(0, height / 2);
    x_axis[1].position = sf::Vector2f(width, height / 2);
    x_axis[0].color = sf::Color::Black;
    x_axis[1].color = sf::Color::Black;
 
    sf::VertexArray y_axis(sf::Lines, 2);
    y_axis[0].position = sf::Vector2f(width / 2, 0);
    y_axis[1].position = sf::Vector2f(width / 2, height);
    y_axis[0].color = sf::Color::Black;
    y_axis[1].color = sf::Color::Black;
 
    sf::VertexArray graph1(sf::LineStrip, width);
    for (int i = 0; i < width; ++i) {
        float x = i - width / 2;
        float y1 = function1(x);
        graph1[i].position = sf::Vector2f(x + width / 2, -y1 + height / 2);
        graph1[i].color = sf::Color::Red;
    }
 
    sf::VertexArray graph2(sf::LineStrip, width);
    for (int i = 0; i < width; ++i) {
        float x = i - width / 2;
        float y2 = function2(x);
        graph2[i].position = sf::Vector2f(x + width / 2, -y2 + height / 2);
        graph2[i].color = sf::Color::Green;
    }
 
    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }
 
        window.clear(sf::Color::White);
 
        window.draw(x_axis);
        window.draw(y_axis);
        window.draw(graph1);
        window.draw(graph2);
 
        window.display();
    }
    return 0;
}
  • Вопрос задан
  • 57 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
07 мая 2024, в 13:07
7000 руб./за проект
07 мая 2024, в 12:59
500 руб./за проект
07 мая 2024, в 12:58
25000 руб./за проект