Open Broadcaster Software
Free, open source software for live streaming and recording
vec2.h
Go to the documentation of this file.
1 /******************************************************************************
2  Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 ******************************************************************************/
17 
18 #pragma once
19 
20 #include "../util/c99defs.h"
21 #include <math.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 struct vec2 {
28  union {
29  struct {
30  float x, y;
31  };
32  float ptr[2];
33  };
34 };
35 
36 static inline void vec2_zero(struct vec2 *dst)
37 {
38  dst->x = 0.0f;
39  dst->y = 0.0f;
40 }
41 
42 static inline void vec2_set(struct vec2 *dst, float x, float y)
43 {
44  dst->x = x;
45  dst->y = y;
46 }
47 
48 static inline void vec2_copy(struct vec2 *dst, const struct vec2 *v)
49 {
50  dst->x = v->x;
51  dst->y = v->y;
52 }
53 
54 static inline void vec2_add(struct vec2 *dst, const struct vec2 *v1,
55  const struct vec2 *v2)
56 {
57  vec2_set(dst, v1->x + v2->x, v1->y + v2->y);
58 }
59 
60 static inline void vec2_sub(struct vec2 *dst, const struct vec2 *v1,
61  const struct vec2 *v2)
62 {
63  vec2_set(dst, v1->x - v2->x, v1->y - v2->y);
64 }
65 
66 static inline void vec2_mul(struct vec2 *dst, const struct vec2 *v1,
67  const struct vec2 *v2)
68 {
69  vec2_set(dst, v1->x * v2->x, v1->y * v2->y);
70 }
71 
72 static inline void vec2_div(struct vec2 *dst, const struct vec2 *v1,
73  const struct vec2 *v2)
74 {
75  vec2_set(dst, v1->x / v2->x, v1->y / v2->y);
76 }
77 
78 static inline void vec2_addf(struct vec2 *dst, const struct vec2 *v, float f)
79 {
80  vec2_set(dst, v->x + f, v->y + f);
81 }
82 
83 static inline void vec2_subf(struct vec2 *dst, const struct vec2 *v, float f)
84 {
85  vec2_set(dst, v->x - f, v->y - f);
86 }
87 
88 static inline void vec2_mulf(struct vec2 *dst, const struct vec2 *v, float f)
89 {
90  vec2_set(dst, v->x * f, v->y * f);
91 }
92 
93 static inline void vec2_divf(struct vec2 *dst, const struct vec2 *v, float f)
94 {
95  vec2_set(dst, v->x / f, v->y / f);
96 }
97 
98 static inline void vec2_neg(struct vec2 *dst, const struct vec2 *v)
99 {
100  vec2_set(dst, -v->x, -v->y);
101 }
102 
103 static inline float vec2_dot(const struct vec2 *v1, const struct vec2 *v2)
104 {
105  return v1->x * v2->x + v1->y * v2->y;
106 }
107 
108 static inline float vec2_len(const struct vec2 *v)
109 {
110  return sqrtf(v->x * v->x + v->y * v->y);
111 }
112 
113 static inline float vec2_dist(const struct vec2 *v1, const struct vec2 *v2)
114 {
115  struct vec2 temp;
116  vec2_sub(&temp, v1, v2);
117  return vec2_len(&temp);
118 }
119 
120 static inline void vec2_minf(struct vec2 *dst, const struct vec2 *v, float val)
121 {
122  if (v->x < val)
123  dst->x = val;
124  if (v->y < val)
125  dst->y = val;
126 }
127 
128 static inline void vec2_min(struct vec2 *dst, const struct vec2 *v,
129  const struct vec2 *min_v)
130 {
131  if (v->x < min_v->x)
132  dst->x = min_v->x;
133  if (v->y < min_v->y)
134  dst->y = min_v->y;
135 }
136 
137 static inline void vec2_maxf(struct vec2 *dst, const struct vec2 *v, float val)
138 {
139  if (v->x > val)
140  dst->x = val;
141  if (v->y > val)
142  dst->y = val;
143 }
144 
145 static inline void vec2_max(struct vec2 *dst, const struct vec2 *v,
146  const struct vec2 *max_v)
147 {
148  if (v->x > max_v->x)
149  dst->x = max_v->x;
150  if (v->y > max_v->y)
151  dst->y = max_v->y;
152 }
153 
154 EXPORT void vec2_abs(struct vec2 *dst, const struct vec2 *v);
155 EXPORT void vec2_floor(struct vec2 *dst, const struct vec2 *v);
156 EXPORT void vec2_ceil(struct vec2 *dst, const struct vec2 *v);
157 EXPORT int vec2_close(const struct vec2 *v1, const struct vec2 *v2,
158  float epsilon);
159 EXPORT void vec2_norm(struct vec2 *dst, const struct vec2 *v);
160 
161 #ifdef __cplusplus
162 }
163 #endif
EXPORT void vec2_floor(struct vec2 *dst, const struct vec2 *v)
Definition: vec2.h:27
float ptr[2]
Definition: vec2.h:32
EXPORT int vec2_close(const struct vec2 *v1, const struct vec2 *v2, float epsilon)
#define EXPORT
Definition: c99defs.h:49
EXPORT void vec2_norm(struct vec2 *dst, const struct vec2 *v)
float y
Definition: vec2.h:30
EXPORT void vec2_ceil(struct vec2 *dst, const struct vec2 *v)
EXPORT void vec2_abs(struct vec2 *dst, const struct vec2 *v)
float x
Definition: vec2.h:30