فهرست منبع

Quadrangle intersects circle.

Sam Jaffe 6 سال پیش
والد
کامیت
d45e31e265
3فایلهای تغییر یافته به همراه25 افزوده شده و 10 حذف شده
  1. 4 0
      math/include/game/math/shape.hpp
  2. 11 10
      math/src/common.cpp
  3. 10 0
      math/src/shape.cpp

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

@@ -42,6 +42,10 @@ namespace math { namespace dim2 {
   };
 } }
 
+namespace math { namespace shapes {
+  std::vector<dim2::line> segments(dim2::quad const & shape);
+} }
+
 namespace math { namespace lines {
   dim2::point intersection(dim2::line const & lhs, dim2::line const & rhs);
   dim2::line orthogonal(dim2::line const & from, dim2::point const & to);

+ 11 - 10
math/src/common.cpp

@@ -60,11 +60,7 @@ namespace math {
   }
   
   bool contains(dim2::quad const & shape, dim2::point const & pt) {
-    std::vector<dim2::line> segments{
-      {shape.ll, shape.lr}, {shape.lr, shape.ur},
-      {shape.ur, shape.ul}, {shape.ul, shape.ll}
-    };
-    return contains(segments, pt);
+    return contains(shapes::segments(shape), pt);
   }
   
   bool intersects(dim2::line const & lhs, dim2::line const & rhs) {
@@ -79,10 +75,7 @@ namespace math {
   }
   
   bool intersects(dim2::line const & lhs, dim2::quad const & rhs) {
-    std::vector<dim2::line> segments{
-      {rhs.ll, rhs.lr}, {rhs.lr, rhs.ur},
-      {rhs.ur, rhs.ul}, {rhs.ul, rhs.ll}
-    };
+    std::vector<dim2::line> segments = shapes::segments(rhs);
     auto lhs_intersects = [&lhs](dim2::line const & ln) {
       return intersects(lhs, ln);
     };
@@ -91,7 +84,15 @@ namespace math {
         contains(segments, lhs.second);
   }
   
-  bool intersects(dim2::quad const & lhs, dim2::circle const & rhs);
+  bool intersects(dim2::quad const & lhs, dim2::circle const & rhs) {
+    std::vector<dim2::line> segments = shapes::segments(lhs);
+    auto rhs_intersects = [&rhs](dim2::line const & ln) {
+      return intersects(ln, rhs);
+    };
+    return std::any_of(segments.begin(), segments.end(), rhs_intersects) ||
+        contains(lhs, rhs.center);
+  }
+  
   bool intersects(dim2::quad const & lhs, dim2::quad const & rhs);
   
   bool intersects(dim2::circle const & lhs, dim2::circle const & rhs) {

+ 10 - 0
math/src/shape.cpp

@@ -8,6 +8,7 @@
 #include "game/math/shape.hpp"
 
 #include <iostream>
+#include <vector>
 
 namespace math { namespace dim2 {
   float line::length() const {
@@ -41,6 +42,15 @@ namespace math { namespace dim2 {
   }
 } }
 
+namespace math { namespace shapes {
+  std::vector<dim2::line> segments(dim2::quad const & shape) {
+    return {
+      {shape.ll, shape.lr}, {shape.lr, shape.ur},
+      {shape.ur, shape.ul}, {shape.ul, shape.ll}
+    };
+  }
+} }
+
 namespace math { namespace lines {
   inline dim2::point intersection(float b1, float s1, float b2, float s2) {
     float const x = (b1 + b2) / (s1 - s2);