aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/math.hpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/common/math.hpp b/src/common/math.hpp
index afbda1a..48a5869 100644
--- a/src/common/math.hpp
+++ b/src/common/math.hpp
@@ -2,6 +2,8 @@
#include <cstddef>
#include <cstdint>
+#include <common/shapes.hpp>
+#include <algorithm>
struct V2ST {
@@ -56,3 +58,25 @@ struct Color {
float a;
};
+
+inline bool
+Intersect_Rectangle_Circle(Rectangle rect, Circle circle)
+{
+ float xmin = std::min(rect.x0, rect.x1);
+ float xmax = std::max(rect.x0, rect.x1);
+ float ymin = std::min(rect.y0, rect.y1);
+ float ymax = std::max(rect.y0, rect.y1);
+
+ float closest_x = std::max(xmin, std::min(circle.x, xmax));
+ float closest_y = std::max(ymin, std::min(circle.y, ymax));
+
+ float dx = closest_x - circle.x;
+ float dy = closest_y - circle.y;
+ float d_sq = dx*dx + dy*dy;
+
+ float r_sq = circle.r * circle.r;
+
+ bool is_intersect = d_sq <= r_sq;
+ return is_intersect;
+}
+