aboutsummaryrefslogtreecommitdiff
path: root/src/util.c
blob: 075c55c05a2866c5e09d2d119bb45789db988f34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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);
}