Forgot to add math int

This commit is contained in:
Krzosa Karol
2024-07-21 10:00:01 +02:00
parent c9cd2e6c1e
commit 6ac1c8711c

View File

@@ -0,0 +1,136 @@
struct Vec2I {
Int x;
Int y;
};
struct Rect2I {
Vec2I min;
Vec2I max;
};
Vec2I GetSize(Rect2I r) {
Vec2I result = {r.max.x - r.min.x, r.max.y - r.min.y};
return result;
}
// clang-format off
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-(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-=(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; }
// clang-format on
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;
}