From 91a3e00265cc75f420b9f6db1bd5192e7c3b47f4 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Fri, 21 Nov 2025 20:22:59 +0200 Subject: [PATCH] determining rectangle intersection by vertices --- util.cpp | 44 ++++++++++++++++++++++++++++++++++---------- util.h | 1 + 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/util.cpp b/util.cpp index b60ab1e..8de3000 100755 --- a/util.cpp +++ b/util.cpp @@ -7,20 +7,44 @@ using namespace std; namespace vb01{ - double *x = nullptr, *y = nullptr; + double *x = nullptr, *y = nullptr; - Vector2 getCursorPos() - { - if(!x) - x = new double; + Vector2 getCursorPos() { + if(!x) x = new double; - if(!y) - y = new double; - - glfwGetCursorPos(Root::getSingleton()->getWindow(), x, y); - return Vector2(*x, *y); + if(!y) y = new double; + + glfwGetCursorPos(Root::getSingleton()->getWindow(), x, y); + return Vector2(*x, *y); + } + + bool rectanglesIntersect(Vector2 rp1, Vector2 rd1, Vector2 rps1, Vector2 rp2, Vector2 rd2, Vector2 rdl2, Vector2 rps2){ + Vector2 point[]{ + rp2 + .5 * (rdl2 * rps2.x + rd2 * rps2.y), + rp2 + .5 * (-rdl2 * rps2.x + rd2 * rps2.y), + rp2 + .5 * (-rdl2 * rps2.x - rd2 * rps2.y), + rp2 + .5 * (rdl2 * rps2.x - rd2 * rps2.y) + }; + + for(int i = 0; i < 4; i++){ + Vector2 p = point[i], pv = p - rp1; + float pd = pv.getLength(), angle = rd1.getAngleBetween(pv.norm()); + angle = (angle > PI / 2 ? PI - angle : angle); + + bool b1 = (pd * sin(angle) <= .5 * rps1.x); + bool b2 = (pd * cos(angle) <= .5 * rps1.y); + + if(b1 && b2) return true; } + for(int i = 0; i < 4; i++){ + for(int j = 0; j < 4; j++){ + } + } + + return false; + } + void readFile(string path, vector &lines, int firstLine, int lastLine){ string l; std::ifstream inFile(path); diff --git a/util.h b/util.h index 05b70e3..b67ef6f 100755 --- a/util.h +++ b/util.h @@ -21,6 +21,7 @@ namespace vb01{ static const float PI = 4 * atan(1); + bool rectanglesIntersect(Vector2, Vector2, Vector2, Vector2, Vector2, Vector2, Vector2); void readFile(std::string, std::vector&, int = 0, int = -1); void getLineData(std::string, std::string[], int, int = 0); int findNthOccurence(std::string&, std::string, int, bool = true);