aboutsummaryrefslogtreecommitdiff
path: root/include/math/aabb2.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/math/aabb2.h')
-rw-r--r--include/math/aabb2.h35
1 files changed, 35 insertions, 0 deletions
diff --git a/include/math/aabb2.h b/include/math/aabb2.h
new file mode 100644
index 0000000..edb6cb6
--- /dev/null
+++ b/include/math/aabb2.h
@@ -0,0 +1,35 @@
1#pragma once
2
3#include "vec2.h"
4
5#include <stddef.h>
6
7typedef struct aabb2 {
8 vec2 min;
9 vec2 max;
10} aabb2;
11
12/// Construct an AABB from min and max bounds.
13static inline aabb2 aabb2_make(vec2 min, vec2 max) {
14 return (aabb2){.min = min, .max = max};
15}
16
17/// Construct an AABB from an array of points.
18static inline aabb2 aabb2_from_array(const vec2 points[], size_t count) {
19 aabb2 box = (aabb2){0};
20 for (size_t i = 0; i < count; ++i) {
21 box.min = vec2_min(box.min, points[i]);
22 box.max = vec2_max(box.max, points[i]);
23 }
24 return box;
25}
26
27/// Add a point to the AABB. The AABB grows to contain the point.
28static inline aabb2 aabb2_add(aabb2 box, vec2 p) {
29 return (aabb2){.min = vec2_min(box.min, p), .max = vec2_max(box.max, p)};
30}
31
32/// Return the AABB of two AABBs.
33static inline aabb2 aabb2_sum(aabb2 a, aabb2 b) {
34 return (aabb2){.min = vec2_min(a.min, b.min), .max = vec2_max(a.max, b.max)};
35}