created bounding boxes

This commit is contained in:
Solomon W. 2020-05-29 14:04:44 -04:00
parent ecff6bd5b8
commit d425ac7483
2 changed files with 68 additions and 0 deletions

24
src/BoundingBox.cpp Normal file
View File

@ -0,0 +1,24 @@
#include "BoundingBox.h"
#include <cstdlib>
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);
}

44
src/BoundingBox.h Normal file
View File

@ -0,0 +1,44 @@
#include <cstddef>
#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);
};