/
annabasalyga
/
Traffic_simulator
Обзор
Документация
Войти
/
annabasalyga
/
Traffic_simulator
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
main.cpp
255 строк
8 KB
Anna
Final version n.1
22 янв 2026, 15:55
22 янв 2026, 15:55
d0e56e2
Код
Авторство
О чём код?
#include <iostream> #include "graph.h" #include "histogram.h" #include "stream.h" int main() { sf::RenderWindow window(sf::VideoMode(900, 600), "Traffic Simulator"); DirectedGraph graph; Histogram hist; const int N = 12; std::vector<sf::Vector2f> pts = { {200,150}, {350,100}, {500,150}, {600,250}, {550,400}, {450,480}, {300,450}, {180,350}, {150,250}, {720, 180}, {750, 360}, {100, 420} }; for (int i = 0; i < N; ++i) graph.addVertex(i, pts[i]); for (int i = 0; i < 9; ++i) graph.addEdge(i, (i + 1) % 9); graph.addEdge(0, 4); graph.addEdge(2, 7); graph.addEdge(5, 1); graph.addEdge(8, 3); graph.addEdge(3, 9); graph.addEdge(9, 4); graph.addEdge(4, 10); graph.addEdge(10, 6); graph.addEdge(7, 11); graph.addEdge(11, 1); graph.addEdge(6, 2); graph.addEdge(10, 3); graph.setStartEnd(5, 7); hist.update(graph); int moveVertex = -1; bool move = false; bool mouseMoved = false; sf::Vector2f mousePressPos; auto deser = std::make_shared<EdgeUpdateDeserializer>(); BidirectionalStream<EdgeLoadUpdate> stream("load_stream.txt", deser); stream.Open(); /*stream.Seek(0);*/ sf::Clock timer; std::vector<EdgeLoadUpdate> pendingUpdates; bool inputActive = false; std::string input; int selectedFrom = -1; int selectedTo = -1; while (window.isOpen()) { sf::Event e; while (window.pollEvent(e)) { if (e.type == sf::Event::Closed) window.close(); // ����� ������ if (e.type == sf::Event::MouseButtonPressed && e.mouseButton.button == sf::Mouse::Left) { sf::Vector2f mp = window.mapPixelToCoords(sf::Mouse::getPosition(window)); DirectedGraph::Edge* clickedEdge = graph.getEdgeAt(mp); if (clickedEdge) { selectedFrom = clickedEdge->from; selectedTo = clickedEdge->to; graph.selectedFrom = selectedFrom; graph.selectedTo = selectedTo; input.clear(); inputActive = true; std::cout << selectedFrom << " " << selectedTo << std::endl; } else { int clickedVertex = graph.getVertexAt(mp); if (clickedVertex != -1) { moveVertex = clickedVertex; move = true; mousePressPos = mp; } } found:; } if (e.type == sf::Event::MouseMoved && move) { sf::Vector2f mp = window.mapPixelToCoords(sf::Mouse::getPosition(window)); float dist = std::hypot( mp.x - mousePressPos.x, mp.y - mousePressPos.y ); if (move && dist > 6.f) { mouseMoved = true; } if (mouseMoved && moveVertex != -1) { graph.changeVertexPos(moveVertex, mp); } } if (e.type == sf::Event::MouseButtonReleased && e.mouseButton.button == sf::Mouse::Left) { move = false; if (!mouseMoved && moveVertex != -1) { graph.start = moveVertex; std::cout << "Start vertex was changed" << std::endl; graph.findAllPaths(); graph.draw(window); } mouseMoved = false; moveVertex == -1; } if (e.type == sf::Event::MouseButtonPressed && e.mouseButton.button == sf::Mouse::Right) { sf::Vector2f mp = window.mapPixelToCoords(sf::Mouse::getPosition(window)); int clickedVertex = graph.getVertexAt(mp); if (clickedVertex != -1) { graph.end = clickedVertex; graph.findAllPaths(); graph.draw(window); } } // ���� � ����� if (e.type == sf::Event::TextEntered && inputActive) { std::cout << "Keyboard text condition" << std::endl; if (e.text.unicode == '\r') { try { int v = std::stoi(input); std::cout << v << std::endl; if (v >= 1 && v <= 10 && selectedFrom != -1 && selectedTo != -1) { stream.Write({ selectedFrom, selectedTo, v }); std::cout << "Value " << v << " was written to stream" << std::endl; } if (stream.GetPosition() > 0) { stream.Seek(stream.GetPosition() - 1); while (!stream.IsEndOfStream()) { auto u = stream.TryRead(); if (!u.has_value()) break; graph.updateEdgeLoad(u->from, u->to, u->load); std::cout << "Value " << u->load << " was read from stream" << std::endl; } } } catch (...) {} input.clear(); inputActive = false; selectedFrom = selectedTo = -1; graph.selectedFrom = graph.selectedTo = -1; } else if (e.text.unicode == 8 && !input.empty()) { input.pop_back(); } else if (isdigit(e.text.unicode)) { input += static_cast<char>(e.text.unicode); } } } // ��� � ��� if (timer.getElapsedTime().asSeconds() > 1.0f) { graph.updateHistory(); hist.update(graph); timer.restart(); } window.clear(sf::Color(200, 255, 200)); graph.draw(window); hist.draw(window); { /*std::cout << "Box is drawing" << std::endl;*/ sf::RectangleShape box({ 260, 220 }); box.setPosition(620.f, 250.f); box.setFillColor(sf::Color(245, 245, 245, 100)); box.setOutlineThickness(2.f); box.setOutlineColor(sf::Color(80, 80, 80)); window.draw(box); // ��������� sf::Text title; title.setFont(graph.font); title.setString("Traffic jam"); title.setCharacterSize(30); title.setFillColor(sf::Color::Black); title.setPosition(630.f, 260.f); window.draw(title); // ���������� sf::Text desc; desc.setFont(graph.font); desc.setString("Enter the value (1-10):"); desc.setCharacterSize(18); desc.setFillColor(sf::Color::Black); desc.setPosition(630.f, 320.f); window.draw(desc); // ���� ����� sf::RectangleShape inputBox({ 100.f, 50.f }); inputBox.setPosition(630.f, 350.f); inputBox.setFillColor(sf::Color::White); inputBox.setOutlineThickness(2.f); inputBox.setOutlineColor( inputActive ? sf::Color(100, 150, 255) : sf::Color(160, 160, 160) ); window.draw(inputBox); // ����� ����� sf::Text inputText; inputText.setFont(graph.font); inputText.setCharacterSize(30); inputText.setFillColor(sf::Color::Black); inputText.setString(inputActive ? input : ""); inputText.setPosition(665.f, 355.f); window.draw(inputText); } window.display(); } }