瀏覽代碼

Fix point on line to not give false positives for co-linear data.

Sam Jaffe 6 年之前
父節點
當前提交
0d6c2b6281
共有 3 個文件被更改,包括 15 次插入3 次删除
  1. 1 1
      math/include/game/math/compare.hpp
  2. 7 2
      math/src/common.cpp
  3. 7 0
      math/test/common_test.cxx

+ 1 - 1
math/include/game/math/compare.hpp

@@ -6,7 +6,7 @@
 namespace math {
   template <typename T>
   bool between(T val, T min, T max) {
-    return val >= min && val < max;
+    return val >= min && (min == max || val < max);
   }
 
   template <typename T>

+ 7 - 2
math/src/common.cpp

@@ -36,9 +36,14 @@ namespace math {
   bool contains(dim2::line const & ln, dim2::point const & pt) {
     if (ln.first[0] == ln.second[0]) {
       return pt[0] == ln.first[0] && between(pt[1], ln.first[1], ln.second[1]);
+    } else if (ln.first == pt || ln.second == pt) {
+      return true;
+    } else {
+      auto slope = dim2::line{ln.first, pt}.slope();
+      return (slope == ln.slope() || slope == -1/ln.slope()) &&
+        between(pt[0], ln.first[0], ln.second[0]) &&
+        between(pt[1], ln.first[1], ln.second[1]);
     }
-    return approx_equal((ln.first[0] - pt[0]) * ln.slope() + pt[1],
-                        ln.first[1], static_cast<float>(1E-6));
   }
   
   bool contains(dim2::circle const & shape, dim2::point const & pt) {

+ 7 - 0
math/test/common_test.cxx

@@ -41,6 +41,13 @@ TEST_P(LineTest, ExistsOnOriginLine) {
   EXPECT_TRUE(math::contains(ln, pt));
 }
 
+TEST_P(LineTest, ColinearOutsideDoesNotContain) {
+  point const end = std::get<0>(GetParam());
+  line const ln{end, end * 2};
+  point const pt = end * std::get<1>(GetParam()) / 100.f;
+  EXPECT_FALSE(math::contains(ln, pt));
+}
+
 std::vector<point> const line_ends{
   {{1, 1}}, {{0, 1}}, {{1, 0}}
 };