Browse Source

Add function to check for lines are parallel.
Add tests for point in square.

Sam Jaffe 6 years ago
parent
commit
cdc8474c8c

+ 1 - 0
math/include/game/math/shape.hpp

@@ -47,6 +47,7 @@ namespace math { namespace shapes {
 } }
 
 namespace math { namespace lines {
+  bool parallel(dim2::line const & lhs, dim2::line const & rhs);
   dim2::point intersection(dim2::line const & lhs, dim2::line const & rhs);
   dim2::line orthogonal(dim2::line const & from, dim2::point const & to);
 } }

+ 1 - 1
math/math.xcodeproj/project.pbxproj

@@ -133,8 +133,8 @@
 				CD1FCFD9227E197800F9BF93 /* GoogleMock.xcodeproj */,
 				CDA34D89225175CB008036A7 /* game */,
 				CDA34D8A22517670008036A7 /* include */,
-				CD3C80791D66440200ACC795 /* test */,
 				CD3786321CFA304800BE89B2 /* src */,
+				CD3C80791D66440200ACC795 /* test */,
 				CD1FCFCE227E194D00F9BF93 /* math-test */,
 				CD3786191CF9F61100BE89B2 /* Products */,
 				CD1FCFE8227E198100F9BF93 /* Frameworks */,

+ 4 - 0
math/src/shape.cpp

@@ -52,6 +52,10 @@ namespace math { namespace shapes {
 } }
 
 namespace math { namespace lines {
+  bool parallel(dim2::line const & lhs, dim2::line const & rhs) {
+    return lhs.slope() == rhs.slope();
+  }
+  
   inline dim2::point intersection(float b1, float s1, float b2, float s2) {
     float const x = (b1 + b2) / (s1 - s2);
     return {{x, b1 + x * s1}};

+ 17 - 0
math/test/common_test.cxx

@@ -73,3 +73,20 @@ point unit_circle_angle(math::degree degs) {
 
 INSTANTIATE_TEST_CASE_P(LiesOnEdge, UnitCircleTest,
     ValuesIn(generate(0.0, 360.0, 5.0, unit_circle_angle)));
+
+struct UnitSquareTest : TestWithParam<std::tuple<float, float>> {};
+
+TEST_P(UnitSquareTest, PointInSquare) {
+  square unit{{{0, 0}}, 1};
+  point pt{{std::get<0>(GetParam()), std::get<1>(GetParam())}};
+  EXPECT_TRUE(math::contains(unit, pt));
+}
+
+TEST_F(UnitSquareTest, PointOutsideSquare) {
+  square unit{{{0, 0}}, 1};
+  EXPECT_FALSE(math::contains(unit, {{0.f, 1.1f}}));
+}
+
+INSTANTIATE_TEST_CASE_P(ContainsPoint, UnitSquareTest,
+    Combine(ValuesIn(generate(0.f, 1.f, 0.25f)),
+            ValuesIn(generate(0.f, 1.f, 0.25f))));