#include "game/aabb.h"
#include <math.h>

bool AABB_Collide(AABB a, AABB b)
{
  bool collision_x = a.pos.x + a.size.x >= b.pos.x && b.pos.x + b.size.x >= a.pos.x;
  bool collision_y = a.pos.y + a.size.x >= b.pos.y && b.pos.y + b.size.y >= a.pos.y;
  return collision_x && collision_y;
}

bool AABB_Point(AABB a, V2f v)
{
  bool collision_x = a.pos.x + a.size.x >= v.x && a.pos.x <= v.x;
  bool collision_y = a.pos.x + a.size.y >= v.y && a.pos.y <= v.y;
  return collision_x && collision_y;
}

bool AABB_Slab(V2f origin, V2f point, AABB a)
{
  V2f start = a.pos;
  V2f finish = {a.pos.x + a.size.x, a.pos.y + a.size.y};
  V2f invdirection = {1 / (origin.x - point.x), 1 / (origin.y - point.y)};
  // x
  F32 tLow = (start.x - origin.x) * invdirection.x;
  F32 tHigh = (finish.x - origin.x) * invdirection.x;
  F32 tMin = Min(tLow, tHigh);
  F32 tMax = Max(tLow, tHigh);
  // y
  tLow = (start.y - origin.y) * invdirection.y;
  tHigh = (finish.y - origin.y) * invdirection.y;

  tMin = Max(tMin, Min(tLow, tHigh));
  tMax = Min(tMax, Max(tLow, tHigh));

  return tMax >= tMin;
}

V2f AABB_Centre(AABB a) {
  return V2F(a.pos.x + a.size.x/2, a.pos.y + a.size.y/2);
}
