Ver código fonte

Fix broken tests and error in intersection calculations.

Sam Jaffe 6 anos atrás
pai
commit
c6f0ea3612
3 arquivos alterados com 11 adições e 5 exclusões
  1. 2 2
      math/src/common.cpp
  2. 1 1
      math/src/shape.cpp
  3. 8 2
      math/test/common_test.cxx

+ 2 - 2
math/src/common.cpp

@@ -85,10 +85,10 @@ namespace math {
   }
 
   bool intersects(dim2::line const & lhs, dim2::circle const & rhs) {
+    if (contains(rhs, lhs.first) || contains(rhs, lhs.second)) { return true; }
     dim2::line const orth = lines::orthogonal(lhs, rhs.center);
     vec2 const delta = orth.second - orth.first;
-    float delsq = delta.dot(delta), rsq = std::pow(rhs.radius, 2);
-    return delsq < rsq || (delsq == rsq && contains(lhs, orth.second));
+    return delta.dot(delta) <= std::pow(rhs.radius, 2) && intersects(lhs, orth);
   }
 
   bool intersects(dim2::line const & lhs, dim2::quad const & rhs) {

+ 1 - 1
math/src/shape.cpp

@@ -46,7 +46,7 @@ namespace math { namespace lines {
   }
 
   inline dim2::point intersection(float b1, float s1, float b2, float s2) {
-    float const x = safe_div(b1 + b2, s1 - s2);
+    float const x = safe_div(b1 - b2, s2 - s1);
     return {{x, b1 + x * s1}};
   }
 

+ 8 - 2
math/test/common_test.cxx

@@ -191,7 +191,7 @@ TEST(CircleTest, LineIntersectsWhenContained) {
 
 TEST(CircleTest, ChordLineIntersects) {
   circle const c1{{{0, 0}}, 1};
-  line const ln{{{1, 1}}, {{-1, 0.5}}};
+  line const ln{{{1.5, -0.5}}, {{-0.5, 1.5}}};
   EXPECT_TRUE(math::intersects(c1, ln));
 }
 
@@ -231,8 +231,14 @@ TEST(CircleTest, ContainedInQuad) {
   EXPECT_TRUE(math::intersects(c1, sq));
 }
 
-TEST(CircleTest, NonIntersecting) {
+TEST(CircleTest, NonIntersectingTangent) {
   circle const c1{{{0, 0}}, 1};
   square const sq{{{1, 1}}, 1};
   EXPECT_FALSE(math::intersects(c1, sq));
 }
+
+TEST(CircleTest, NonIntersecting) {
+  circle const c1{{{0, 0}}, 1};
+  square const sq{{{1.5, 0.5}}, 0.5};
+  EXPECT_FALSE(math::intersects(c1, sq));
+}