Browse Source

read data from file

Kajetan Johannes Hammerle 3 years ago
parent
commit
d675b20ed9
2 changed files with 26 additions and 2 deletions
  1. 25 1
      Game.cpp
  2. 1 1
      utils/Random.cpp

+ 25 - 1
Game.cpp

@@ -2,6 +2,9 @@
 #include <GLFW/glfw3.h>
 #include <iostream>
 #include <algorithm>
+#include <chrono>
+#include <fstream>
+#include <cmath>
 
 #include "Game.h"
 #include "utils/Random.h"
@@ -27,11 +30,29 @@ Game::Game(Keys& keys, const char* file, bool steps) : keys(keys), keyNext(keys.
 state(SPLIT) {
     if(file == nullptr) {
         Random r;
-        int amount = steps ? 50 : 1000000;
+        int amount = steps ? 50 : 100000;
         for(int i = 0; i < amount; i++) {
             data.push_back({r.nextFloat(), r.nextFloat()});
         }
     } else {
+        std::ifstream in;
+        in.open(file);
+        if(!in.good()) {
+            std::cout << "cannot read '" << file << "'\n";
+            return;
+        }
+        int count;
+        in >> count;
+        while(in.get() != '\n');
+        for(int i = 0; i < count; i++) {
+            float x;
+            float y;
+            in >> x;
+            in.get();
+            in >> y;
+            while(!in.eof() && in.get() != '\n');
+            data.push_back({x, y});
+        }
     }
 
     if(data.size() > 0) {
@@ -341,12 +362,15 @@ void Game::renderGroup(Renderer& renderer, const std::vector<Point>& group, uint
 }
 
 void Game::skip() {
+    long time = std::chrono::high_resolution_clock::now().time_since_epoch().count();
     split();
     while(merge()) {
         while(findLowerTangent());
         while(findUpperTangent());
         finalizeMerge();
     }
+    time = std::chrono::high_resolution_clock::now().time_since_epoch().count() - time;
+    std::cout << "Took " << (time / 1000000.0) << "ms\n";
     state = END;
     hullA.clear();
     hullB.clear();

+ 1 - 1
utils/Random.cpp

@@ -2,7 +2,7 @@
 
 #include "utils/Random.h"
 
-Random::Random() : seed(std::chrono::steady_clock::now().time_since_epoch().count()) {
+Random::Random() : seed(std::chrono::high_resolution_clock::now().time_since_epoch().count()) {
 }
 
 Random::Random(u64 seed) : seed(seed) {