-
Notifications
You must be signed in to change notification settings - Fork 1
/
rockets_math.h
127 lines (99 loc) · 2.42 KB
/
rockets_math.h
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#ifndef _rockets_math_h
#define _rockets_math_h
#include <math.h>
// Math
#define CLAMP(x,xmin,xmax) ((x) < (xmin) ? (xmin) : (x) > (xmax) ? (xmax) : (x))
float
deg_to_rad(float deg)
{
return deg * M_PI / 180.0;
}
/* bool */
/* bounds_contains(float top_leftx, float top_lefty, */
/* float bottom_rightx, float bottom_righty, */
/* float x, float y) */
/* { */
/* return (x >= top_leftx && x <= bottom_rightx && */
/* y >= top_lefty && y <= bottom_righty); */
/* } */
// Vectors
typedef union {
struct {float x; float y;};
float arr[2];
} V2;
V2
v2(float x, float y)
{
return (V2){.x = x, .y = y};
}
V2
v2_plus(V2 i, V2 j)
{
return v2(i.x + j.x,
i.y + j.y);
}
V2
v2_minus(V2 i, V2 j)
{
return v2(i.x - j.x,
i.y - j.y);
}
V2
v2_scale(V2 v, float f)
{
return v2(v.x * f,
v.y * f);
}
V2
v2_rotate(V2 v, float radians)
{
float nx = v.x * cos(radians) - v.y * sin(radians);
float ny = v.x * sin(radians) + v.y * cos(radians);
return v2(nx, ny);
}
/* // Bounding Box */
/* typedef struct BoundingBox { */
/* V2 top_left; */
/* V2 bottom_right; */
/* } BoundingBox; */
/* /\* Bounding Box *\/ */
/* BoundingBox */
/* boundingBox(V2 top_left, float width, float height) */
/* { */
/* return (BoundingBox){top_left, v2_plus(top_left, v2(width, height))}; */
/* } */
/* bool */
/* bb_contains(BoundingBox bb, float x, float y) */
/* { */
/* return bounds_contains(bb.top_left.x, */
/* bb.top_left.y, */
/* bb.bottom_right.x, */
/* bb.bottom_right.y, x, y); */
/* } */
/* V2 */
/* bb_center(BoundingBox bb) */
/* { */
/* float centerx = ((bb.bottom_right.x - bb.top_left.x) / 2) */
/* + bb.top_left.x; */
/* float centery = ((bb.bottom_right.y - bb.top_left.y) / 2) */
/* + bb.top_left.y; */
/* return v2(centerx, centery); */
/* } */
/* // Calculate width and height of bb */
/* V2 */
/* bb_size(BoundingBox bb) */
/* { */
/* return v2(bb.bottom_right.x - bb.top_left.x, */
/* bb.bottom_right.y - bb.top_left.y); */
/* } */
/* // up the size of a bb by 1 */
/* BoundingBox */
/* bb_blow_up(BoundingBox bb) */
/* { */
/* bb.top_left.x -= 1; */
/* bb.top_left.y -= 1; */
/* bb.bottom_right.x += 2; */
/* bb.bottom_right.y += 2; */
/* return bb; */
/* } */
#endif