aboutsummaryrefslogtreecommitdiff
path: root/teensy/teensy_math.c
blob: 9247bd0235c955461433b0067f3af8b22cc39dcf (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "teensy.h"

bool ty_pointi_in_recti(ty_Vec2i point, ty_Recti rect)
{
    return
        point.x > rect.x &&
        point.x < rect.x + rect.w &&
        point.y > rect.y &&
        point.y < rect.y + rect.h;
}

bool ty_point_in_rect(ty_Vec2 point, ty_Rect rect)
{
    return
        point.x > rect.x &&
        point.x < rect.x + rect.w &&
        point.y > rect.y &&
        point.y < rect.y + rect.h;
}


ty_Recti ty_recti_shrink(ty_Recti rect, int p)
{
    rect.x += p;
    rect.y += p;
    rect.w -= p * 2;
    rect.h -= p * 2;
    return rect;
}

ty_Recti ty_recti_grow(ty_Recti rect, int p)
{
    rect.x -= p;
    rect.y -= p;
    rect.w += p * 2;
    rect.h += p * 2;
    return rect;
}

ty_Recti ty_recti_clamp(ty_Recti rect, ty_Recti minmax)
{
    ty_Vec2i start = ty_recti_start(rect);
    ty_Vec2i end = ty_recti_end(rect);

    ty_Vec2i min = ty_recti_start(minmax);
    ty_Vec2i max = ty_recti_end(minmax);

    start.x = ty_clamp(start.x, min.x, max.x);
    start.y = ty_clamp(start.y, min.y, max.y);
    end.x   = ty_clamp(end.x,   min.x, max.x);
    end.y   = ty_clamp(end.y,   min.y, max.y);

    return ty_recti(
        start.x, start.y,
        end.x - start.x, end.y - start.y
    );
}