Browse Source

Fix parallel to point.

Sam Jaffe 6 years ago
parent
commit
d972ed59e5
3 changed files with 13 additions and 10 deletions
  1. 5 4
      math/src/common.cpp
  2. 4 1
      math/test/common_test.cxx
  3. 4 5
      math/test/shape_test.cxx

+ 5 - 4
math/src/common.cpp

@@ -59,7 +59,7 @@ namespace math {
   }
   
   static dim2::line ray_x(dim2::point const & pt, dim2::line const & l) {
-    auto x_inf = std::max({l.first[0], l.second[0], pt[0]});
+    auto x_inf = std::max({l.first[0], l.second[0], pt[0]}) + 1;
     return {pt, {{x_inf, pt[1]}}};
   }
   
@@ -67,8 +67,9 @@ namespace math {
                        dim2::point const & pt) {
     int hits = 0;
     for (auto l : segments) {
-      if (contains(l, pt)) return true;
-      else if (intersects(l, ray_x(pt, l))) ++hits;
+      if (!intersects(l, ray_x(pt, l))) continue;
+      if (orient(l.first, l.second, pt) == colinear) return contains(l, pt);
+      ++hits;
     }
     return (hits & 1) == 1;
   }
@@ -90,7 +91,7 @@ namespace math {
       return true;
     
     // Special Cases: one of the points exists on the other line
-    return contains(lhs, rhs.second) || contains(lhs, rhs.second) ||
+    return contains(lhs, rhs.first) || contains(lhs, rhs.second) ||
         contains(rhs, lhs.first) || contains(rhs, lhs.second);
   }
   

+ 4 - 1
math/test/common_test.cxx

@@ -84,7 +84,10 @@ TEST_P(UnitSquareTest, PointInSquare) {
 
 TEST_F(UnitSquareTest, PointOutsideSquare) {
   square unit{{{0, 0}}, 1};
-  EXPECT_FALSE(math::contains(unit, {{0.f, 1.1f}}));
+  EXPECT_FALSE(math::contains(unit, {{ 0.f,  1.1f}}));
+  EXPECT_FALSE(math::contains(unit, {{ 0.f, -0.1f}}));
+  EXPECT_FALSE(math::contains(unit, {{-0.1f, 0.0f}}));
+  EXPECT_FALSE(math::contains(unit, {{ 1.1f, 0.0f}}));
 }
 
 INSTANTIATE_TEST_CASE_P(ContainsPoint, UnitSquareTest,

+ 4 - 5
math/test/shape_test.cxx

@@ -43,9 +43,9 @@ std::vector<line> const point_pairs{
 INSTANTIATE_TEST_CASE_P(LineIntersection, FromOriginTest,
                         ValuesIn(point_pairs));
 
-struct XUnitTest : TestWithParam<point> {};
+struct UnitLineTest : TestWithParam<point> {};
 
-TEST_P(XUnitTest, OrthoOnIntersection) {
+TEST_P(UnitLineTest, OrthoOnIntersection) {
   line const ln{{{0, 0}}, {{1, 0}}};
   point const pt = GetParam();
   line const expected{pt, {{pt[0], 0}}};
@@ -53,11 +53,10 @@ TEST_P(XUnitTest, OrthoOnIntersection) {
               Eq(expected));
 }
 
-TEST_P(XUnitTest, OrthoOnIntersectionY) {
+TEST_P(UnitLineTest, OrthoOnIntersectionY) {
   line const ln{{{0, 0}}, {{0, 1}}};
   point const pt = GetParam();
   line const expected{pt, {{pt[0], 0}}};
-  math::lines::orthogonal(ln, pt);
   EXPECT_THAT(math::lines::orthogonal(ln, pt),
               Eq(expected));
 }
@@ -68,7 +67,7 @@ std::vector<point> x_orthos{
   {{2, 1}}, {{1, 2}}, {{-2, 1}}, {{-1, 2}}, {{1, -2}}, {{2, -1}}
 };
 
-INSTANTIATE_TEST_CASE_P(LineOrthogonal, XUnitTest, ValuesIn(x_orthos));
+INSTANTIATE_TEST_CASE_P(LineOrthogonal, UnitLineTest, ValuesIn(x_orthos));
 
 struct DiagonalTest : TestWithParam<std::pair<point, float>> {};