From d425ac74836ac8d960ce857ecbea971ae7050ed9 Mon Sep 17 00:00:00 2001 From: SoloArchx250 Date: Fri, 29 May 2020 14:04:44 -0400 Subject: [PATCH] created bounding boxes --- src/BoundingBox.cpp | 24 ++++++++++++++++++++++++ src/BoundingBox.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/BoundingBox.cpp create mode 100644 src/BoundingBox.h diff --git a/src/BoundingBox.cpp b/src/BoundingBox.cpp new file mode 100644 index 0000000..f37d1fe --- /dev/null +++ b/src/BoundingBox.cpp @@ -0,0 +1,24 @@ +#include "BoundingBox.h" +#include + +BoundingBox::BoundingBox(Quad &quad) { + this->parent = (size_t)&quad; + init(quad); +} +BoundingBox::BoundingBox() {} + +void BoundingBox::update() { + Quad *quad = (Quad *)parent; + init(quad[0]); +} + +void BoundingBox::render(SDL_Renderer *rend) { + uint8_t r,g,b,a; + SDL_GetRenderDrawColor(rend,&r,&g,&b,&a); + SDL_SetRenderDrawColor(rend,WHITE); + SDL_RenderDrawLine(rend,box.topL.x,box.topL.y,box.topR.x,box.topR.y); + SDL_RenderDrawLine(rend,box.topR.x,box.topR.y,box.botR.x,box.botR.y); + SDL_RenderDrawLine(rend,box.botR.x,box.botR.y,box.botL.x,box.botL.y); + SDL_RenderDrawLine(rend,box.botL.x,box.botL.y,box.topL.x,box.topL.y); + SDL_SetRenderDrawColor(rend,r,g,b,a); +} \ No newline at end of file diff --git a/src/BoundingBox.h b/src/BoundingBox.h new file mode 100644 index 0000000..614e7e4 --- /dev/null +++ b/src/BoundingBox.h @@ -0,0 +1,44 @@ +#include +#include "Quad.h" + +class BoundingBox { + private: + size_t parent; + void init(Quad quad) { + xvalues[0] = quad.topL.x; + xvalues[1] = quad.topR.x; + xvalues[2] = quad.botR.x; + xvalues[3] = quad.botL.x; + + yvalues[0] = quad.topL.y; + yvalues[1] = quad.topR.y; + yvalues[2] = quad.botR.y; + yvalues[3] = quad.botL.y; + + for(int i=0; i<4; i++) { + if(xvalues[i] > max.x) { + max.x = xvalues[i]; + } else { + if(xvalues[i] < min.x) { + min.x = xvalues[i]; + } + } + if(yvalues[i] > max.y) { + max.y = yvalues[i]; + } else { + if(min.y < yvalues[i]) { + min.y = yvalues[i]; + } + } + } + box = Quad(Vector(min.x,min.y),Vector(max.x,max.y)); + } + public: + int xvalues[4],yvalues[4]; + Vector max,min; + Quad box; + BoundingBox(Quad &quad); + BoundingBox(); + void update(); + void render(SDL_Renderer *rend); +}; \ No newline at end of file