diff options
| author | Federico Angelilli <code@fedang.net> | 2024-07-10 15:57:13 +0200 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2024-07-10 15:57:13 +0200 |
| commit | d575e261c85b70a8de4f02fb2115b102d3c33f64 (patch) | |
| tree | db84a9d6aa4a0510586aa1c449f131e1b2226db6 /src/util.c | |
| parent | dbd61ded1f7ddd55743b324a5b27017f2a7a886b (diff) | |
Refactor
Diffstat (limited to 'src/util.c')
| -rw-r--r-- | src/util.c | 28 |
1 files changed, 23 insertions, 5 deletions
@@ -1,4 +1,5 @@ #include <assert.h> +#include <stdio.h> #include "util.h" @@ -18,18 +19,35 @@ bool timespec_greater(struct timespec a, struct timespec b) || (a.tv_sec == b.tv_sec && a.tv_nsec > b.tv_nsec); } -bool in_capsule(int px, int py, int x, int y, int w, int h) +void timespec_print(struct timespec *ts) +{ + printf("%ld.%.9ld", ts->tv_sec, ts->tv_nsec); +} + +bool check_rect(int px, int py, int x, int y, int w, int h) +{ + return px >= x && px <= x + w && py >= y && py <= y + h; +} + +bool check_circle(int px, int py, int x, int y, int r) +{ + int dx = x - px; + int dy = y - py; + return (dx * dx + dy * dy) <= r * r; +} + +bool check_capsule(int px, int py, int x, int y, int w, int h) { assert(w >= h); int radius = h / 2; // Trivial case if (w == h) - return in_circle(px, py, x + radius, y + radius, radius); + return check_circle(px, py, x + radius, y + radius, radius); // General case - return in_circle(px, py, x + radius, y + radius, radius) - || in_circle(px, py, x + w - radius, y + radius, radius) - || in_rect(px, py, x + radius, y, w - 2 * radius, h); + return check_circle(px, py, x + radius, y + radius, radius) + || check_circle(px, py, x + w - radius, y + radius, radius) + || check_rect(px, py, x + radius, y, w - 2 * radius, h); } |
