From 0c6d090ddb02bfd6840a1b40644ed98f6bd4cef7 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Fri, 27 Feb 2026 08:28:27 +0100 Subject: [PATCH] math: Added point in rect tester. Added tests for tile to screen and point in rect. --- src/test.c | 7 +++++-- src/tests/t_math.c | 22 ++++++++++++++++++++++ src/u_math.c | 5 +++++ src/u_math.h | 7 +++++-- 4 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 src/tests/t_math.c diff --git a/src/test.c b/src/test.c index 605d52e..842bad7 100644 --- a/src/test.c +++ b/src/test.c @@ -9,6 +9,7 @@ #ifdef LRTS_IMPL #include "tests/t_args.c" #include "tests/t_arena.c" +#include "tests/t_math.c" #endif int main(int argc, char **argv) { @@ -17,8 +18,10 @@ int main(int argc, char **argv) { T_TESTBEGIN("lrts test"); lrts_getopt(argc, argv, lrts_cfg()); - T_TESTCASE("test-argv", t_test_argv); - T_TESTCASE("test-arena", t_test_arena); + T_TESTCASE("argv", t_test_argv); + T_TESTCASE("arena", t_test_arena); + T_TESTCASE("tile-to-screen", t_test_tile_to_screen); + T_TESTCASE("point-in-rect", t_test_pointinrect); T_TESTEND("lrts test"); diff --git a/src/tests/t_math.c b/src/tests/t_math.c new file mode 100644 index 0000000..9772227 --- /dev/null +++ b/src/tests/t_math.c @@ -0,0 +1,22 @@ +#include "../u_arena.h" +#include "t_defs.h" +#include "../u_math.h" + +int t_test_pointinrect(void) { + T_ASSERT(u_point_in_rect(5, 5, 4, 2, 4, 5), ("point in rect\n")); + T_ASSERT(u_point_in_rect(1, 1, 4, 2, 4, 5) == LRTS_FALSE, ("point not in rect\n")); + + return 0; +} + +int t_test_tile_to_screen(void) { + struct u_vec2 res = u_tile_to_screen(0, 0); + T_ASSERT(res.x == 0 && res.y == 0, ("tile to screen %d/%d\n", res.x, res.y)); + + res = u_tile_to_screen(1, 1); + T_ASSERT(res.x == 96 && res.y == 0, ("tile to screen %d/%d\n", res.x, res.y)); + + res = u_tile_to_screen(1, 2); + T_ASSERT(res.x == 144 && res.y == 24, ("tile to screen %d/%d\n", res.x, res.y)); + return 0; +} diff --git a/src/u_math.c b/src/u_math.c index c661829..679ae44 100644 --- a/src/u_math.c +++ b/src/u_math.c @@ -63,3 +63,8 @@ struct u_vec2 u_tile_to_screen(i32 x, i32 y) { return r; } + +lrts_bool u_point_in_rect(i32 x, i32 y, i32 rx, i32 ry, i32 rw, i32 rh) { + return x >= rx && x <= rx + rw + && y >= ry && y <= ry + rh; +} diff --git a/src/u_math.h b/src/u_math.h index b5838f4..4403907 100644 --- a/src/u_math.h +++ b/src/u_math.h @@ -6,6 +6,9 @@ #define U_FP(n, f) {n, f} #define U_V2(x, y) {x, y} +/* Clamps an integer value between min and max */ +#define U_CLAMP(v, min, max) if (v < min) { v = min; } if (v > max) { v = max; } + /* fixed point number * n: whole part * f: fractional part @@ -48,7 +51,7 @@ struct u_vec2 u_vec2_mul(i32 n, struct u_vec2 v); */ struct u_vec2 u_tile_to_screen(i32 x, i32 y); -/* Clamps an integer value between min and max */ -#define U_CLAMP(v, min, max) if (v < min) { v = min; } if (v > max) { v = max; } +/* tests if a point is contained inside a rectangle */ +lrts_bool u_point_in_rect(i32 x, i32 y, i32 rx, i32 ry, i32 rw, i32 rh); #endif -- 2.30.2