#include // cin, cout #include // ifstream #include // srand, rand #include // time #include // strcmp #include // TODO: or use arrays for maximum performance? #include "Display.h" #include "Point.h" // TODOs: // - use SFML vec2 instead of Point class // - calc min and max points for x, y // *) just a loop and save/override points -> maybe in Data Block? // *) std::minmax_element for x, y -> not saving just temp draw // https://en.cppreference.com/w/cpp/algorithm/minmax_element // - actual algorithm :p // - architecture: // # vis in own thread, calls "lib" functions? // # seperate implementation for vis and perf? int main (int argc, char **argv) { // read program arguments // -v mode with enabled visualization // -f read random numbers from file // TODO: care a little more <3 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], "-f") == 0) fileRnd = argv[i + 1]; } int valCount = 10; std::vector points; if (fileRnd.empty()) // generate random numbers { std::cout << "generating random numbers..." << std::endl; //srand(static_cast (time(0))); srand(static_cast (0)); // fixed seed for testing // 2) rectangle //float diff = (2.f * HEIGHT / valCount); // 3) circle //float rad = HEIGHT / 2; //8.0f; //float deg = (float)(2 * 3.14159 / valCount); float x = 0; float y = 0; for (int i = 0; i < valCount; ++i) { // 1) random generation //x = OFFSET + static_cast (rand()) / (static_cast (RAND_MAX / (WIDTH - OFFSET))); //y = OFFSET + static_cast (rand()) / (static_cast (RAND_MAX / (HEIGHT - OFFSET))); x = OFFSET + static_cast (rand()) / (static_cast (RAND_MAX / (WIDTH - 2*OFFSET))); y = OFFSET + static_cast (rand()) / (static_cast (RAND_MAX / (HEIGHT - 2*OFFSET))); // 2) rectangle //x = (i%2 == 0) ? OFFSET : WIDTH - OFFSET; //y = diff/2 + (i/2) * diff; // 3) position in circle around center, x/y plane //x = WIDTH/2 + rad * cosf(i * deg); //y = HEIGHT/2 + rad * sinf(i * deg); points.push_back(Point(x, y)); } } else // read random numbers { std::cout << "reading random numbers from file: " << fileRnd << "..." << std::endl; std::ifstream in(fileRnd.c_str()); if (in.is_open()) { std::string line; std::getline(in, line); // get first line for explizit valueCount valCount = std::stoi(line); float x = 0; float y = 0; for (int i = 0; i < valCount; ++i) { getline(in, line); //getline(in, line, ','); // could also be used slightly different but includes newlines :/ size_t pos = line.find(','); if (pos != std::string::npos) { x = std::stof(line.substr(0, pos)); y = std::stof(line.substr(pos + 1)); points.push_back(Point(x, y)); } else { throw "invalid format!"; } } } } // TODO: sort here, once and for all? xD //sortPoints(points); for (Point& pt : points) { std::cout << "pt: " << pt.x() << ", " << pt.y() << std::endl; } if (vis) { // TEST to check SFML coordinate system //points.push_back(Point(0, 0)); Display display(points); display.show(); } std::cout << "any key to exit..."; std::cin.get(); return 0; }