finally kinda finished drawing algorithm

This commit is contained in:
incredibleLeitman 2020-11-29 14:51:55 +01:00
parent 52213825de
commit 4e97501e1e
3 changed files with 64 additions and 19 deletions

View File

@ -8,7 +8,7 @@
#include "Point.h"
#include "Utility.h"
Display::Display (const std::vector<Point> &pts)
Display::Display (const std::vector<Point> &pts, int stepSize) : m_stepSize(stepSize)
{
if (!m_font.loadFromFile("Resources/LiberationSans-Regular.ttf"))
{
@ -98,7 +98,8 @@ void Display::show()
// choose a simple sleep
update();
render(window);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
std::this_thread::sleep_for(std::chrono::milliseconds(m_stepSize));
}
}
@ -207,17 +208,25 @@ void Display::update ()
if (m_curLine == nullptr)
{
size_t lines = m_lines.size();
if (lines < 1) throw "no more lines to work :-o TODO: just jump to finish?";
if (lines < 1) // no more open lines -> fin
{
m_points.clear();
m_textStatus.setString(text + "finished calculating convex hull");
return;
}
m_curLine = &m_lines[lines - 1];
}
std::cout << std::endl << "current line: " <<
std::to_string(m_curLine->from().x()) << ", " <<
std::to_string(m_curLine->from().y()) << ", " <<
std::to_string(m_curLine->to().x()) << ", " <<
std::to_string(m_curLine->to().y()) << ", " <<
" of " << std::to_string(m_lines.size()) << std::endl;
std::cout << "lines " << std::to_string(m_lines.size()) << std::endl;
for (auto& line : m_lines)
{
std::cout << " " <<
std::to_string(line.from().x()) << ", " <<
std::to_string(line.from().y()) << " - " <<
std::to_string(line.to().x()) << ", " <<
std::to_string(line.to().y()) << " " << std::endl;
}
for (auto& pt : m_points)
{
@ -253,7 +262,7 @@ void Display::update ()
}
*/
if (pt.getFillColor() != sf::Color::Green) continue;
if (pt.getFillColor() == sf::Color::Green) continue;
float distance = pDistance(
pt.getPosition().x, pt.getPosition().y,
@ -271,12 +280,14 @@ void Display::update ()
if (maxDistance > 0)
{
std::cout << "max point at " << pos.x << ", " << pos.y << std::endl;
// not append but insert between last line points
// -> append a point, then swap values
size_t vertices = m_hull.getVertexCount();
m_hull.append(sf::Vertex(pos, sf::Color::Blue));
m_hull[vertices] = m_hull[vertices - 1];
m_hull[vertices - 1] = pos;
m_hull[vertices - 1].position = pos;
size_t points = m_convex.getPointCount();
m_convex.setPointCount(points + 1);
@ -286,7 +297,20 @@ void Display::update ()
//m_lines.push_back(Line(Point(pos.x, pos.y), m_curLine->to()));
m_lines.push_back(Line(Point(pos.x, pos.y), m_curLine->to()));
m_curLine->set_to(Point(pos.x, pos.y));
//m_curLine->set_to(Point(pos.x, pos.y)); // only changes the value of the pointer
//(*m_curLine).set_to(Point(pos.x, pos.y)); // doesn't update list
//(*m_curLine) = Line(m_curLine->from(), Point(pos.x, pos.y)); // doesn't update list
m_lines[m_lines.size() - 2] = Line(m_curLine->from(), Point(pos.x, pos.y)); // updates list entry
std::cout << "added to lines " << std::to_string(m_lines.size()) << std::endl;
for (auto& line : m_lines)
{
std::cout << " " <<
std::to_string(line.from().x()) << ", " <<
std::to_string(line.from().y()) << " - " <<
std::to_string(line.to().x()) << ", " <<
std::to_string(line.to().y()) << " " << std::endl;
}
}
else
{
@ -323,7 +347,13 @@ void Display::update ()
if (m_points.size() == 0) m_textStatus.setString(text + "finished calculating convex hull");
else m_textStatus.setString(text + "adding new hull point...");
}
else m_textStatus.setString(text + "invalid status!");
else if (m_step > 0) m_textStatus.setString(text + "invalid status!");
if (m_stepSize == 0 && curStep > 0)
{
std::cout << "any key to continue with next step...";
std::cin.get();
}
if (curStep != 5 || m_points.size() > 0) m_step++;
}
@ -335,10 +365,22 @@ void Display::render (sf::RenderWindow &window)
// draw already calculated hull
//if (step >= 1)
{
/*size_t vertices = m_hull.getVertexCount();
for (size_t i = 0; i < vertices; ++i)
{
std::cout << "hull pt[" << i << "] color: " << std::to_string(m_hull[i].color.toInteger()) << " >> " <<
std::to_string(m_hull[i].color.a) << ", " <<
std::to_string(m_hull[i].color.r) << ", " <<
std::to_string(m_hull[i].color.g) << ", " <<
std::to_string(m_hull[i].color.b) << std::endl;
}*/
//window.draw(&m_hull[0], m_hull.size(), sf::Lines);
window.draw(&m_hull[0], m_hull.getVertexCount(), m_hull.getPrimitiveType());
window.draw(m_convex);
// TODO: either insert points at correct location
// or only sort, then draw final hull
//window.draw(m_convex);
}
// always print remaining points

View File

@ -6,7 +6,6 @@
// windows - manual dl from https://www.sfml-dev.org/download.php
#include <SFML/Graphics.hpp>
#include "Line.h"
//class Point;
@ -19,6 +18,7 @@
class Display
{
private:
int m_stepSize;
sf::Font m_font;
sf::Text m_textStatus;
@ -41,7 +41,7 @@ private:
void render(sf::RenderWindow &);
public:
Display(const std::vector<Point> &);
Display(const std::vector<Point> &, int stepSize = 0);
void show();
};

View File

@ -24,12 +24,15 @@ int main (int argc, char **argv)
// read program arguments
// -v mode with enabled visualization
// -f <filename> read random numbers from file
// -stepsize <size> time between autosteps; if 0 or not provided -> manual steps
// TODO: care a little more <3
int stepSize = 0;
std::string fileRnd = "";
bool vis = false;
for (int i = 0; i < argc; ++i)
{
if (strcmp(argv[i], "-v") == 0) vis = true;
else if (strcmp(argv[i], "-stepsize") == 0) stepSize = std::stoi(argv[i + 1]);
else if (strcmp(argv[i], "-f") == 0) fileRnd = argv[i + 1];
}
@ -111,7 +114,7 @@ int main (int argc, char **argv)
// TEST to check SFML coordinate system
//points.push_back(Point(0, 0));
Display display(points);
Display display(points, stepSize);
display.show();
}