Browse Source

correct handling of points on a line

Kajetan Johannes Hammerle 3 years ago
parent
commit
b01e9fac03
1 changed files with 17 additions and 16 deletions
  1. 17 16
      Game.cpp

+ 17 - 16
Game.cpp

@@ -7,6 +7,9 @@
 #include "utils/Random.h"
 
 bool Game::Point::operator<(const Point& other) {
+    if(std::abs(x - other.x) < 0.0001f) {
+        return y < other.y;
+    }
     return x < other.x;
 }
 
@@ -14,8 +17,12 @@ Game::Game(Keys& keys) : keys(keys), keyNext(keys.add(GLFW_KEY_N)), active(0) {
     std::cout << "register on " << keys.add(GLFW_KEY_W) << "\n";
 
     Random r(6476);
-    for(int i = 0; i < 1000; i++) {
+    for(int i = 0; i < 25; i++) {
         data.push_back({r.nextFloat() * 1.9f - 0.95f, r.nextFloat() * 1.9f - 0.95f});
+        //float f = r.nextFloat() * 1.9f - 0.95f;
+        //data.push_back({f * 0.8f, f});
+        //data.push_back({r.nextFloat() * 1.9f - 0.95f, 0});
+        //data.push_back({0, r.nextFloat() * 1.9f - 0.95f});
     }
 
     std::cout << data.size() << "\n";
@@ -81,20 +88,14 @@ void Game::split() {
         v.push_back(data[index++]);
         float f = det(groups[active].back()[0], groups[active].back()[1], groups[active].back()[2]);
         if(std::abs(f) < 0.0001f) {
-            float minX = v[0].x;
-            float maxX = v[2].x;
-            if(std::abs(minX - maxX) < 0.0001f) {
-                float minY = std::min(v[0].y, std::min(v[1].y, v[2].y));
-                float maxY = std::max(v[0].y, std::max(v[1].y, v[2].y));
-                if(minY < v[0].y && v[0].y < maxY) {
-                    v.erase(v.begin());
-                } else if(minY < v[1].y && v[1].y < maxY) {
-                    v.erase(v.begin() + 1);
-                } else {
-                    v.pop_back();
-                }
+            if(std::abs(v[0].y - v[1].y) > 0.0001f || std::abs(v[1].y - v[2].y) > 0.0001f) {
+                v[0].x = (std::abs(v[0].x) < 0.0001f) ? 0.001f : v[0].x * 1.001f;
             } else {
-                v.erase(v.begin() + 1);
+                v[0].y = (std::abs(v[0].y) < 0.0001f) ? 0.001f : v[0].y * 1.001f;
+            }
+            f = det(groups[active].back()[0], groups[active].back()[1], groups[active].back()[2]);
+            if(f > 0.0f) {
+                std::swap(groups[active].back()[1], groups[active].back()[2]);
             }
         } else if(f > 0.0f) {
             std::swap(groups[active].back()[1], groups[active].back()[2]);
@@ -113,7 +114,7 @@ void Game::split() {
 bool Game::isLowerTangent(const Point& a, const Point& b, const std::vector<Point>& hull) const {
     for(const Point& p : hull) {
         float det = (a.x - p.x) * (b.y - p.y) - (b.x - p.x) * (a.y - p.y);
-        if(det < 0.0f) {
+        if(det < -0.00001f) {
             return false;
         }
     }
@@ -123,7 +124,7 @@ bool Game::isLowerTangent(const Point& a, const Point& b, const std::vector<Poin
 bool Game::isUpperTangent(const Point& a, const Point& b, const std::vector<Point>& hull) const {
     for(const Point& p : hull) {
         float det = (a.x - p.x) * (b.y - p.y) - (b.x - p.x) * (a.y - p.y);
-        if(det > 0.0f) {
+        if(det > 0.00001f) {
             return false;
         }
     }