aboutsummaryrefslogtreecommitdiff
path: root/include/math/aabb2.h
blob: edb6cb620cec53c052dc9f6b77411f0040251f83 (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
#pragma once

#include "vec2.h"

#include <stddef.h>

typedef struct aabb2 {
  vec2 min;
  vec2 max;
} aabb2;

/// Construct an AABB from min and max bounds.
static inline aabb2 aabb2_make(vec2 min, vec2 max) {
  return (aabb2){.min = min, .max = max};
}

/// Construct an AABB from an array of points.
static inline aabb2 aabb2_from_array(const vec2 points[], size_t count) {
  aabb2 box = (aabb2){0};
  for (size_t i = 0; i < count; ++i) {
    box.min = vec2_min(box.min, points[i]);
    box.max = vec2_max(box.max, points[i]);
  }
  return box;
}

/// Add a point to the AABB. The AABB grows to contain the point.
static inline aabb2 aabb2_add(aabb2 box, vec2 p) {
  return (aabb2){.min = vec2_min(box.min, p), .max = vec2_max(box.max, p)};
}

/// Return the AABB of two AABBs.
static inline aabb2 aabb2_sum(aabb2 a, aabb2 b) {
  return (aabb2){.min = vec2_min(a.min, b.min), .max = vec2_max(a.max, b.max)};
}