| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#include "vector.h" |
| 27 |
|
| 28 |
#include <math.h> |
| 29 |
|
| 30 |
#include "tile.h" |
| 31 |
|
| 32 |
struct vec2i svec2i_scale_divide(const struct vec2i v, const mint_t scale) |
| 33 |
{ |
| 34 |
return svec2i(v.x / scale, v.y / scale); |
| 35 |
} |
| 36 |
|
| 37 |
struct vec2i Vec2iToTile(struct vec2i v) |
| 38 |
{ |
| 39 |
return svec2i(v.x / TILE_WIDTH, v.y / TILE_HEIGHT); |
| 40 |
} |
| 41 |
|
| 42 |
struct vec2i Vec2iCenterOfTile(struct vec2i v) |
| 43 |
{ |
| 44 |
return svec2i( |
| 45 |
v.x * TILE_WIDTH + TILE_WIDTH / 2, |
| 46 |
v.y * TILE_HEIGHT + TILE_HEIGHT / 2); |
| 47 |
} |
| 48 |
|
| 49 |
struct vec2i Vec2ToTile(const struct vec2 v) |
| 50 |
{ |
| 51 |
return svec2i((int)(v.x / TILE_WIDTH), (int)(v.y / TILE_HEIGHT)); |
| 52 |
} |
| 53 |
|
| 54 |
struct vec2 Vec2CenterOfTile(const struct vec2i v) |
| 55 |
{ |
| 56 |
return svec2_assign_vec2i(Vec2iCenterOfTile(v)); |
| 57 |
} |
| 58 |
|
| 59 |
Rect2i Rect2iNew(const struct vec2i pos, const struct vec2i size) |
| 60 |
{ |
| 61 |
Rect2i r; |
| 62 |
r.Pos = pos; |
| 63 |
r.Size = size; |
| 64 |
return r; |
| 65 |
} |
| 66 |
Rect2i Rect2iZero(void) |
| 67 |
{ |
| 68 |
return Rect2iNew(svec2i_zero(), svec2i_zero()); |
| 69 |
} |
| 70 |
|
| 71 |
bool Rect2iIsZero(const Rect2i r) |
| 72 |
{ |
| 73 |
return svec2i_is_zero(r.Pos) && svec2i_is_zero(r.Size); |
| 74 |
} |
| 75 |
|
| 76 |
bool Rect2iIsAtEdge(const Rect2i r, const struct vec2i v) |
| 77 |
{ |
| 78 |
return v.y == r.Pos.y || v.y == r.Pos.y + r.Size.y - 1 || v.x == r.Pos.x || |
| 79 |
v.x == r.Pos.x + r.Size.x - 1; |
| 80 |
} |
| 81 |
|
| 82 |
bool Rect2iIsInside(const Rect2i r, const struct vec2i v) |
| 83 |
{ |
| 84 |
return !( |
| 85 |
v.y < r.Pos.y || v.y > r.Pos.y + r.Size.y - 1 || v.x < r.Pos.x || |
| 86 |
v.x > r.Pos.x + r.Size.x - 1); |
| 87 |
} |
| 88 |
|
| 89 |
bool Rect2iOverlap(const Rect2i r1, const Rect2i r2) |
| 90 |
{ |
| 91 |
return r1.Pos.x < r2.Pos.x + r2.Size.x && |
| 92 |
r1.Pos.x + r1.Size.x > r2.Pos.x && |
| 93 |
r1.Pos.y < r2.Pos.y + r2.Size.y && r1.Pos.y + r1.Size.y > r2.Pos.y; |
| 94 |
} |
| 95 |
|
| 96 |
struct vec2i Rect2iCenter(const Rect2i r) |
| 97 |
{ |
| 98 |
return svec2i_add(r.Pos, svec2i_scale_divide(r.Size, 2)); |
| 99 |
} |