aboutsummaryrefslogtreecommitdiff
path: root/src/util.c
diff options
context:
space:
mode:
authorFederico Angelilli <code@fedang.net>2024-07-09 17:02:42 +0200
committerFederico Angelilli <code@fedang.net>2024-07-09 17:02:42 +0200
commit0c567c67933cc48ac57f08167d88a57ce0504524 (patch)
treeda7c5ccc0935093cf5ecc661371db324ea0005cb /src/util.c
parent28adc6b395d2fb7545189636cec3651b9c2a5f73 (diff)
Add event handling
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c35
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);
+}
+