#include #include #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); } 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 check_circle(px, py, x + radius, y + radius, radius); // General case 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); }