mirror of
https://github.com/koloideal/SortLab.git
synced 2026-06-10 10:25:30 +03:00
skelet
This commit is contained in:
Binary file not shown.
@@ -10,4 +10,6 @@ public:
|
||||
virtual bool isFinished() const = 0;
|
||||
virtual std::string getName() const = 0;
|
||||
virtual void reset() = 0;
|
||||
virtual std::string getTimeComplexity() const = 0;
|
||||
virtual std::string getSpaceComplexity() const = 0;
|
||||
};
|
||||
|
||||
@@ -14,6 +14,8 @@ private:
|
||||
sf::Font font_;
|
||||
sf::Text algorithmText_;
|
||||
sf::Text stateText_;
|
||||
sf::Text timeComplexityText_;
|
||||
sf::Text spaceComplexityText_;
|
||||
sf::Text comparisonsText_;
|
||||
sf::Text swapsText_;
|
||||
sf::Text speedText_;
|
||||
|
||||
@@ -9,6 +9,8 @@ public:
|
||||
bool isFinished() const override;
|
||||
std::string getName() const override;
|
||||
void reset() override;
|
||||
std::string getTimeComplexity() const override;
|
||||
std::string getSpaceComplexity() const override;
|
||||
|
||||
private:
|
||||
enum class Phase { COMPARING, SWAPPING, NEXT };
|
||||
|
||||
@@ -9,6 +9,8 @@ public:
|
||||
bool isFinished() const override;
|
||||
std::string getName() const override;
|
||||
void reset() override;
|
||||
std::string getTimeComplexity() const override;
|
||||
std::string getSpaceComplexity() const override;
|
||||
|
||||
private:
|
||||
enum class Phase { COMPARING, SHIFTING, INSERTING, NEXT };
|
||||
|
||||
@@ -10,6 +10,8 @@ public:
|
||||
bool isFinished() const override;
|
||||
std::string getName() const override;
|
||||
void reset() override;
|
||||
std::string getTimeComplexity() const override;
|
||||
std::string getSpaceComplexity() const override;
|
||||
|
||||
private:
|
||||
enum class Phase { MERGING, COPYING_LEFT, COPYING_RIGHT, COPYING_BACK, NEXT_MERGE };
|
||||
|
||||
@@ -10,6 +10,8 @@ public:
|
||||
bool isFinished() const override;
|
||||
std::string getName() const override;
|
||||
void reset() override;
|
||||
std::string getTimeComplexity() const override;
|
||||
std::string getSpaceComplexity() const override;
|
||||
|
||||
private:
|
||||
enum class Phase { PARTITIONING, SWAPPING_PIVOT, PUSHING_RANGES };
|
||||
|
||||
@@ -9,6 +9,8 @@ public:
|
||||
bool isFinished() const override;
|
||||
std::string getName() const override;
|
||||
void reset() override;
|
||||
std::string getTimeComplexity() const override;
|
||||
std::string getSpaceComplexity() const override;
|
||||
|
||||
private:
|
||||
enum class Phase { FINDING_MIN, SWAPPING, NEXT };
|
||||
|
||||
+49
-10
@@ -168,27 +168,66 @@ void App::update(float dt) {
|
||||
}
|
||||
|
||||
void App::render() {
|
||||
window_.clear(sf::Color(18, 18, 22));
|
||||
window_.clear(sf::Color(10, 12, 20));
|
||||
|
||||
float width = window_.getSize().x / static_cast<float>(array_.getSize());
|
||||
float maxVal = static_cast<float>(array_.getSize());
|
||||
|
||||
sf::VertexArray vertices(sf::Quads);
|
||||
|
||||
for (int i = 0; i < array_.getSize(); ++i) {
|
||||
float height = (array_.getValue(i) / maxVal) * (window_.getSize().y * 0.8f);
|
||||
float x = i * width;
|
||||
float y = window_.getSize().y - height;
|
||||
|
||||
sf::Color barColor;
|
||||
bool useGradient = false;
|
||||
|
||||
sf::RectangleShape bar(sf::Vector2f(width - 1.0f, height));
|
||||
bar.setPosition(i * width, window_.getSize().y - height);
|
||||
|
||||
switch (array_.getState(i)) {
|
||||
case Array::State::NORMAL: bar.setFillColor(sf::Color(200, 200, 200)); break;
|
||||
case Array::State::COMPARE: bar.setFillColor(sf::Color::Yellow); break;
|
||||
case Array::State::SWAP: bar.setFillColor(sf::Color::Red); break;
|
||||
case Array::State::SORTED: bar.setFillColor(sf::Color::Green); break;
|
||||
case Array::State::NORMAL:
|
||||
useGradient = true;
|
||||
break;
|
||||
case Array::State::COMPARE:
|
||||
barColor = sf::Color(0, 220, 255);
|
||||
break;
|
||||
case Array::State::SWAP:
|
||||
barColor = sf::Color(255, 120, 30);
|
||||
break;
|
||||
case Array::State::SORTED:
|
||||
barColor = sf::Color(50, 220, 120);
|
||||
break;
|
||||
}
|
||||
|
||||
if (useGradient) {
|
||||
float normalizedValue = array_.getValue(i) / maxVal;
|
||||
sf::Color bottomColor(60, 80, 140);
|
||||
sf::Color topColor(160, 180, 255);
|
||||
|
||||
sf::Color gradientTop(
|
||||
static_cast<sf::Uint8>(bottomColor.r + (topColor.r - bottomColor.r) * normalizedValue),
|
||||
static_cast<sf::Uint8>(bottomColor.g + (topColor.g - bottomColor.g) * normalizedValue),
|
||||
static_cast<sf::Uint8>(bottomColor.b + (topColor.b - bottomColor.b) * normalizedValue)
|
||||
);
|
||||
|
||||
sf::Color gradientBottom(
|
||||
static_cast<sf::Uint8>(bottomColor.r + (topColor.r - bottomColor.r) * normalizedValue * 0.6f),
|
||||
static_cast<sf::Uint8>(bottomColor.g + (topColor.g - bottomColor.g) * normalizedValue * 0.6f),
|
||||
static_cast<sf::Uint8>(bottomColor.b + (topColor.b - bottomColor.b) * normalizedValue * 0.6f)
|
||||
);
|
||||
|
||||
vertices.append(sf::Vertex(sf::Vector2f(x, window_.getSize().y), gradientBottom));
|
||||
vertices.append(sf::Vertex(sf::Vector2f(x + width - 1.0f, window_.getSize().y), gradientBottom));
|
||||
vertices.append(sf::Vertex(sf::Vector2f(x + width - 1.0f, y), gradientTop));
|
||||
vertices.append(sf::Vertex(sf::Vector2f(x, y), gradientTop));
|
||||
} else {
|
||||
vertices.append(sf::Vertex(sf::Vector2f(x, window_.getSize().y), barColor));
|
||||
vertices.append(sf::Vertex(sf::Vector2f(x + width - 1.0f, window_.getSize().y), barColor));
|
||||
vertices.append(sf::Vertex(sf::Vector2f(x + width - 1.0f, y), barColor));
|
||||
vertices.append(sf::Vertex(sf::Vector2f(x, y), barColor));
|
||||
}
|
||||
|
||||
window_.draw(bar);
|
||||
}
|
||||
|
||||
window_.draw(vertices);
|
||||
ui_.draw(window_);
|
||||
|
||||
window_.display();
|
||||
|
||||
+20
-5
@@ -2,7 +2,7 @@
|
||||
#include <iostream>
|
||||
|
||||
UI::UI() : fontLoaded_(false) {
|
||||
if (!font_.loadFromFile("assets/fonts/Roboto-Regular.ttf")) {
|
||||
if (!font_.loadFromFile("assets/fonts/JetBrainsMono-Regular.ttf")) {
|
||||
std::cerr << "Failed to load font" << std::endl;
|
||||
return;
|
||||
}
|
||||
@@ -19,20 +19,30 @@ UI::UI() : fontLoaded_(false) {
|
||||
stateText_.setFillColor(sf::Color::White);
|
||||
stateText_.setPosition(15.0f, 50.0f);
|
||||
|
||||
timeComplexityText_.setFont(font_);
|
||||
timeComplexityText_.setCharacterSize(16);
|
||||
timeComplexityText_.setFillColor(sf::Color(160, 160, 160));
|
||||
timeComplexityText_.setPosition(15.0f, 80.0f);
|
||||
|
||||
spaceComplexityText_.setFont(font_);
|
||||
spaceComplexityText_.setCharacterSize(16);
|
||||
spaceComplexityText_.setFillColor(sf::Color(160, 160, 160));
|
||||
spaceComplexityText_.setPosition(15.0f, 105.0f);
|
||||
|
||||
comparisonsText_.setFont(font_);
|
||||
comparisonsText_.setCharacterSize(18);
|
||||
comparisonsText_.setFillColor(sf::Color(220, 220, 220));
|
||||
comparisonsText_.setPosition(15.0f, 80.0f);
|
||||
comparisonsText_.setPosition(15.0f, 135.0f);
|
||||
|
||||
swapsText_.setFont(font_);
|
||||
swapsText_.setCharacterSize(18);
|
||||
swapsText_.setFillColor(sf::Color(220, 220, 220));
|
||||
swapsText_.setPosition(15.0f, 105.0f);
|
||||
swapsText_.setPosition(15.0f, 160.0f);
|
||||
|
||||
speedText_.setFont(font_);
|
||||
speedText_.setCharacterSize(18);
|
||||
speedText_.setFillColor(sf::Color(220, 220, 220));
|
||||
speedText_.setPosition(15.0f, 130.0f);
|
||||
speedText_.setPosition(15.0f, 185.0f);
|
||||
|
||||
controlsText_.setFont(font_);
|
||||
controlsText_.setCharacterSize(15);
|
||||
@@ -63,6 +73,9 @@ void UI::update(const Sorter& sorter, bool isPlaying, bool isFinished, int steps
|
||||
}
|
||||
stateText_.setString(state);
|
||||
|
||||
timeComplexityText_.setString("Time: " + sorter.getTimeComplexity());
|
||||
spaceComplexityText_.setString("Space: " + sorter.getSpaceComplexity());
|
||||
|
||||
comparisonsText_.setString("Comparisons: " + std::to_string(array.getComparisons()));
|
||||
swapsText_.setString("Swaps: " + std::to_string(array.getSwaps()));
|
||||
speedText_.setString("Speed: " + std::to_string(stepsPerFrame) + "x");
|
||||
@@ -74,7 +87,7 @@ void UI::draw(sf::RenderWindow& window) {
|
||||
}
|
||||
|
||||
float leftWidth = 350.0f;
|
||||
float leftHeight = 160.0f;
|
||||
float leftHeight = 215.0f;
|
||||
leftBackground_.setSize(sf::Vector2f(leftWidth, leftHeight));
|
||||
leftBackground_.setPosition(5.0f, 5.0f);
|
||||
|
||||
@@ -93,6 +106,8 @@ void UI::draw(sf::RenderWindow& window) {
|
||||
|
||||
window.draw(algorithmText_);
|
||||
window.draw(stateText_);
|
||||
window.draw(timeComplexityText_);
|
||||
window.draw(spaceComplexityText_);
|
||||
window.draw(comparisonsText_);
|
||||
window.draw(swapsText_);
|
||||
window.draw(speedText_);
|
||||
|
||||
@@ -63,6 +63,14 @@ std::string BubbleSorter::getName() const {
|
||||
return "Bubble Sort";
|
||||
}
|
||||
|
||||
std::string BubbleSorter::getTimeComplexity() const {
|
||||
return "O(n) / O(n²) / O(n²)";
|
||||
}
|
||||
|
||||
std::string BubbleSorter::getSpaceComplexity() const {
|
||||
return "O(1)";
|
||||
}
|
||||
|
||||
void BubbleSorter::reset() {
|
||||
i_ = 0;
|
||||
j_ = 0;
|
||||
|
||||
@@ -67,6 +67,14 @@ std::string InsertionSorter::getName() const {
|
||||
return "Insertion Sort";
|
||||
}
|
||||
|
||||
std::string InsertionSorter::getTimeComplexity() const {
|
||||
return "O(n) / O(n²) / O(n²)";
|
||||
}
|
||||
|
||||
std::string InsertionSorter::getSpaceComplexity() const {
|
||||
return "O(1)";
|
||||
}
|
||||
|
||||
void InsertionSorter::reset() {
|
||||
i_ = 1;
|
||||
j_ = 0;
|
||||
|
||||
@@ -121,6 +121,14 @@ std::string MergeSorter::getName() const {
|
||||
return "Merge Sort";
|
||||
}
|
||||
|
||||
std::string MergeSorter::getTimeComplexity() const {
|
||||
return "O(n log n) / O(n log n) / O(n log n)";
|
||||
}
|
||||
|
||||
std::string MergeSorter::getSpaceComplexity() const {
|
||||
return "O(n)";
|
||||
}
|
||||
|
||||
void MergeSorter::reset() {
|
||||
currentSize_ = 1;
|
||||
leftStart_ = 0;
|
||||
|
||||
@@ -108,6 +108,14 @@ std::string QuickSorter::getName() const {
|
||||
return "Quick Sort";
|
||||
}
|
||||
|
||||
std::string QuickSorter::getTimeComplexity() const {
|
||||
return "O(n log n) / O(n log n) / O(n²)";
|
||||
}
|
||||
|
||||
std::string QuickSorter::getSpaceComplexity() const {
|
||||
return "O(log n)";
|
||||
}
|
||||
|
||||
void QuickSorter::reset() {
|
||||
stack_.clear();
|
||||
currentLow_ = 0;
|
||||
|
||||
@@ -76,6 +76,14 @@ std::string SelectionSorter::getName() const {
|
||||
return "Selection Sort";
|
||||
}
|
||||
|
||||
std::string SelectionSorter::getTimeComplexity() const {
|
||||
return "O(n²) / O(n²) / O(n²)";
|
||||
}
|
||||
|
||||
std::string SelectionSorter::getSpaceComplexity() const {
|
||||
return "O(1)";
|
||||
}
|
||||
|
||||
void SelectionSorter::reset() {
|
||||
i_ = 0;
|
||||
j_ = 0;
|
||||
|
||||
Reference in New Issue
Block a user