/
annabasalyga
/
Traffic_simulator
Обзор
Документация
Войти
/
annabasalyga
/
Traffic_simulator
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
histogram.cpp
101 строка
3 KB
Anna
Final version n.1
22 янв 2026, 15:55
22 янв 2026, 15:55
d0e56e2
Код
Авторство
О чём код?
#include <iostream> #include "histogram.h" Histogram::Histogram() { if (!font.loadFromFile("font.ttf")) { std::cerr << "Failed to load font for histogram\n"; } } void Histogram::update(const DirectedGraph& graph) { hasBest = false; hasAlt = false; const auto& paths = graph.getPaths(); if (paths.empty()) return; int best = graph.getBestPathIndex(); int alt = graph.getAlternativePathIndex(); if (best != -1) { bestPredCost = graph.predictPath(paths[best]); hasBest = true; } if (alt != -1) { altPredCost = graph.predictPath(paths[alt]); hasAlt = true; } } void Histogram::draw(sf::RenderWindow& win) { if (!hasBest && !hasAlt) return; sf::Vector2f panelPos(620.f, 20.f); sf::Vector2f panelSize(260.f, 220.f); float barWidth = 60.f; float maxBarHeight = 130.f; float bottomY = panelPos.y + panelSize.y - 30.f; double maxValue = std::max(bestPredCost, altPredCost); if (maxValue < 1e-6) { maxValue = 1.0; } // ��� sf::RectangleShape panel(panelSize); panel.setPosition(panelPos); panel.setFillColor(sf::Color(255, 255, 255, 100)); panel.setOutlineThickness(2.f); panel.setOutlineColor(sf::Color::Black); win.draw(panel); // ��������� sf::Text title("Path costs", font, 30); title.setFillColor(sf::Color::Black); title.setPosition(630.f, panelPos.y + 10.f); win.draw(title); // ������ ���� if (hasBest) { float h = static_cast<float>(bestPredCost) / maxValue * maxBarHeight; sf::RectangleShape bar({ barWidth, h }); bar.setFillColor(sf::Color::Green); bar.setPosition(panelPos.x + 50.f, bottomY - h); win.draw(bar); drawLabel(win, "Best", bar.getPosition().x + 5.f, bottomY + 5.f); drawValue(win, bestPredCost, bar.getPosition().x, bar.getPosition().y - 20.f); } // �������������� ���� if (hasAlt) { float h = static_cast<float>(altPredCost) / maxValue * maxBarHeight; sf::RectangleShape bar({ barWidth, h }); bar.setFillColor(sf::Color(120, 0, 255)); bar.setPosition(panelPos.x + 150.f, bottomY - h); win.draw(bar); drawLabel(win, "Alt", bar.getPosition().x + 10.f, bottomY + 5.f); drawValue(win, altPredCost, bar.getPosition().x, bar.getPosition().y - 20.f); } } void Histogram::drawLabel(sf::RenderWindow& win, const std::string& s, float x, float y) { sf::Text t(s, font, 18); t.setFillColor(sf::Color::Black); t.setPosition(x, y); win.draw(t); } void Histogram::drawValue(sf::RenderWindow& win, double v, float x, float y) { sf::Text t(std::to_string(static_cast<int>(v)), font, 14); t.setFillColor(sf::Color::Black); t.setPosition(x, y); win.draw(t); }