Rect2I operator-(Rect2I r, Rect2I value) { return { {r.min.x - value.min.x, r.min.y - value.min.y}, {r.max.x - value.max.x, r.max.y - value.max.y} }; }
Rect2I operator+(Rect2I r, Rect2I value) { return { {r.min.x + value.min.x, r.min.y + value.min.y}, {r.max.x + value.max.x, r.max.y + value.max.y} }; }
Rect2I operator*(Rect2I r, Rect2I value) { return { {r.min.x * value.min.x, r.min.y * value.min.y}, {r.max.x * value.max.x, r.max.y * value.max.y} }; }
Rect2I operator/(Rect2I r, Rect2I value) { return { {r.min.x / value.min.x, r.min.y / value.min.y}, {r.max.x / value.max.x, r.max.y / value.max.y} }; }

Rect2I operator-(Rect2I r, Vec2I value) { return { {r.min.x - value.x, r.min.y - value.y}, {r.max.x - value.x, r.max.y - value.y} }; }
Rect2I operator+(Rect2I r, Vec2I value) { return { {r.min.x + value.x, r.min.y + value.y}, {r.max.x + value.x, r.max.y + value.y} }; }
Rect2I operator*(Rect2I r, Vec2I value) { return { {r.min.x * value.x, r.min.y * value.y}, {r.max.x * value.x, r.max.y * value.y} }; }
Rect2I operator/(Rect2I r, Vec2I value) { return { {r.min.x / value.x, r.min.y / value.y}, {r.max.x / value.x, r.max.y / value.y} }; }
Rect2I operator-(Vec2I value, Rect2I r) { return { {r.min.x - value.x, r.min.y - value.y}, {r.max.x - value.x, r.max.y - value.y} }; }
Rect2I operator+(Vec2I value, Rect2I r) { return { {r.min.x + value.x, r.min.y + value.y}, {r.max.x + value.x, r.max.y + value.y} }; }
Rect2I operator*(Vec2I value, Rect2I r) { return { {r.min.x * value.x, r.min.y * value.y}, {r.max.x * value.x, r.max.y * value.y} }; }
Rect2I operator/(Vec2I value, Rect2I r) { return { {r.min.x / value.x, r.min.y / value.y}, {r.max.x / value.x, r.max.y / value.y} }; }

Rect2I operator-(Int value, Rect2I r) { return { {r.min.x - value, r.min.y - value}, {r.max.x - value, r.max.y - value} }; }
Rect2I operator+(Int value, Rect2I r) { return { {r.min.x + value, r.min.y + value}, {r.max.x + value, r.max.y + value} }; }
Rect2I operator*(Int value, Rect2I r) { return { {r.min.x * value, r.min.y * value}, {r.max.x * value, r.max.y * value} }; }
Rect2I operator/(Int value, Rect2I r) { return { {r.min.x / value, r.min.y / value}, {r.max.x / value, r.max.y / value} }; }
Rect2I operator-(Rect2I r, Int value) { return { {r.min.x - value, r.min.y - value}, {r.max.x - value, r.max.y - value} }; }
Rect2I operator+(Rect2I r, Int value) { return { {r.min.x + value, r.min.y + value}, {r.max.x + value, r.max.y + value} }; }
Rect2I operator*(Rect2I r, Int value) { return { {r.min.x * value, r.min.y * value}, {r.max.x * value, r.max.y * value} }; }
Rect2I operator/(Rect2I r, Int value) { return { {r.min.x / value, r.min.y / value}, {r.max.x / value, r.max.y / value} }; }

Rect2I operator-=(Rect2I &r, Rect2I value) { r = r - value; return r; }
Rect2I operator+=(Rect2I &r, Rect2I value) { r = r + value; return r; }
Rect2I operator*=(Rect2I &r, Rect2I value) { r = r * value; return r; }
Rect2I operator/=(Rect2I &r, Rect2I value) { r = r / value; return r; }
Rect2I operator-=(Rect2I &r, Vec2I value) { r = r - value; return r; }
Rect2I operator+=(Rect2I &r, Vec2I value) { r = r + value; return r; }
Rect2I operator*=(Rect2I &r, Vec2I value) { r = r * value; return r; }
Rect2I operator/=(Rect2I &r, Vec2I value) { r = r / value; return r; }
Rect2I operator-=(Rect2I &r, Int value) { r = r - value; return r; }
Rect2I operator+=(Rect2I &r, Int value) { r = r + value; return r; }
Rect2I operator*=(Rect2I &r, Int value) { r = r * value; return r; }
Rect2I operator/=(Rect2I &r, Int value) { r = r / value; return r; }

Vec2I operator-(Vec2I a, Vec2I b) { return {a.x - b.x, a.y - b.y}; }
Vec2I operator+(Vec2I a, Vec2I b) { return {a.x + b.x, a.y + b.y}; }
Vec2I operator*(Vec2I a, Vec2I b) { return {a.x * b.x, a.y * b.y}; }
Vec2I operator/(Vec2I a, Vec2I b) { return {a.x / b.x, a.y / b.y}; }

Vec2I operator-(Vec2I a, Int b) { return {a.x - b, a.y - b}; }
Vec2I operator+(Vec2I a, Int b) { return {a.x + b, a.y + b}; }
Vec2I operator*(Vec2I a, Int b) { return {a.x * b, a.y * b}; }
Vec2I operator/(Vec2I a, Int b) { return {a.x / b, a.y / b}; }
Vec2I operator-(Int b, Vec2I a) { return {a.x - b, a.y - b}; }
Vec2I operator+(Int b, Vec2I a) { return {a.x + b, a.y + b}; }
Vec2I operator*(Int b, Vec2I a) { return {a.x * b, a.y * b}; }
Vec2I operator/(Int b, Vec2I a) { return {a.x / b, a.y / b}; }

Vec2I operator-=(Vec2I &a, Vec2I b) { a = a - b; return a; }
Vec2I operator+=(Vec2I &a, Vec2I b) { a = a + b; return a; }
Vec2I operator*=(Vec2I &a, Vec2I b) { a = a * b; return a; }
Vec2I operator/=(Vec2I &a, Vec2I b) { a = a / b; return a; }
Vec2I operator-=(Vec2I &a, Int b) { a = a - b; return a; }
Vec2I operator+=(Vec2I &a, Int b) { a = a + b; return a; }
Vec2I operator*=(Vec2I &a, Int b) { a = a * b; return a; }
Vec2I operator/=(Vec2I &a, Int b) { a = a / b; return a; }

Vec2I GetSize(Rect2I r) {
    Vec2I result = {r.max.x - r.min.x, r.max.y - r.min.y};
    return result;
}

Rect2I RectI0Size(Int x, Int y) {
    Rect2I rect = {0, 0, x, y};
    return rect;
}

Rect2I Rect2IFromSize(Vec2I pos, Vec2I size) {
    Rect2I result = {};
    result.min    = pos;
    result.max    = pos + size;
    return result;
}

Rect2I Shrink(Rect2I result, Int v) {
    result.min.x += v;
    result.max.x -= v;
    result.min.y += v;
    result.max.y -= v;
    return result;
}

Vec2I GetMid(Rect2I r) {
    Vec2I size = GetSize(r);
    size.x /= 2;
    size.y /= 2;
    Vec2I result = r.min + size;
    return result;
}

Rect2I CutLeft(Rect2I *r, Int value) {
    Int minx      = r->min.x;
    r->min.x      = Min(r->min.x + value, r->max.x);
    Rect2I result = {
        {    minx, r->min.y},
        {r->min.x, r->max.y}
    };
    return result;
}

Rect2I CutRight(Rect2I *r, Int value) {
    Int maxx      = r->max.x;
    r->max.x      = Max(r->min.x, r->max.x - value);
    Rect2I result = {
        {r->max.x, r->min.y},
        {    maxx, r->max.y},
    };
    return result;
}

// Y is up
Rect2I CutBottom(Rect2I *r, Int value) {
    Int maxy      = r->max.y;
    r->max.y      = Max(r->min.y, r->max.y - value);
    Rect2I result = {
        {r->min.x, r->max.y},
        {r->max.x,     maxy},
    };
    return result;
}

// Y is up
Rect2I CutTop(Rect2I *r, Int value) {
    Int miny      = r->min.y;
    r->min.y      = Min(r->min.y + value, r->max.y);
    Rect2I result = {
        {r->min.x,     miny},
        {r->max.x, r->min.y},
    };
    return result;
}

Int SafeDivide(Int a, Int b) {
    if (b == 0) {
        return 0;
    }
    return a / b;
}

bool AreOverlapping(Vec2I point, Rect2I rec) {
    bool result = (point.x >= rec.min.x) && (point.x < rec.max.x) && (point.y >= rec.min.y) && (point.y < rec.max.y);
    return result;
}