diff options
Diffstat (limited to 'src/util.c')
| -rw-r--r-- | src/util.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..075c55c --- /dev/null +++ b/src/util.c @@ -0,0 +1,35 @@ +#include <assert.h> + +#include "util.h" + +struct timespec timespec_diff(struct timespec a, struct timespec b) +{ + bool over = (b.tv_nsec - a.tv_nsec) < 0; + struct timespec diff = { + .tv_sec = b.tv_sec - a.tv_sec - over, + .tv_nsec = b.tv_nsec - a.tv_nsec + over * 1000000000ul, + }; + return diff; +} + +bool timespec_greater(struct timespec a, struct timespec b) +{ + return a.tv_sec > b.tv_sec + || (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) +{ + assert(w >= h); + int radius = h / 2; + + // Trivial case + if (w == h) + return in_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); +} + |
