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,
79  float f)
80 {
81  vec2_set(dst, v->x+f, v->y+f);
82 }
83 
84 static inline void vec2_subf(struct vec2 *dst, const struct vec2 *v,
85  float f)
86 {
87  vec2_set(dst, v->x-f, v->y-f);
88 }
89 
90 static inline void vec2_mulf(struct vec2 *dst, const struct vec2 *v,
91  float f)
92 {
93  vec2_set(dst, v->x*f, v->y*f);
94 }
95 
96 static inline void vec2_divf(struct vec2 *dst, const struct vec2 *v,
97  float f)
98 {
99  vec2_set(dst, v->x/f, v->y/f);
100 }
101 
102 static inline void vec2_neg(struct vec2 *dst, const struct vec2 *v)
103 {
104  vec2_set(dst, -v->x, -v->y);
105 }
106 
107 static inline float vec2_dot(const struct vec2 *v1, const struct vec2 *v2)
108 {
109  return v1->x*v2->x + v1->y*v2->y;
110 }
111 
112 static inline float vec2_len(const struct vec2 *v)
113 {
114  return sqrtf(v->x*v->x + v->y*v->y);
115 }
116 
117 static inline float vec2_dist(const struct vec2 *v1, const struct vec2 *v2)
118 {
119  struct vec2 temp;
120  vec2_sub(&temp, v1, v2);
121  return vec2_len(&temp);
122 }
123 
124 static inline void vec2_minf(struct vec2 *dst, const struct vec2 *v,
125  float val)
126 {
127  if (v->x < val)
128  dst->x = val;
129  if (v->y < val)
130  dst->y = val;
131 }
132 
133 static inline void vec2_min(struct vec2 *dst, const struct vec2 *v,
134  const struct vec2 *min_v)
135 {
136  if (v->x < min_v->x)
137  dst->x = min_v->x;
138  if (v->y < min_v->y)
139  dst->y = min_v->y;
140 }
141 
142 static inline void vec2_maxf(struct vec2 *dst, const struct vec2 *v,
143  float val)
144 {
145  if (v->x > val)
146  dst->x = val;
147  if (v->y > val)
148  dst->y = val;
149 }
150 
151 static inline void vec2_max(struct vec2 *dst, const struct vec2 *v,
152  const struct vec2 *max_v)
153 {
154  if (v->x > max_v->x)
155  dst->x = max_v->x;
156  if (v->y > max_v->y)
157  dst->y = max_v->y;
158 }
159 
160 EXPORT void vec2_abs(struct vec2 *dst, const struct vec2 *v);
161 EXPORT void vec2_floor(struct vec2 *dst, const struct vec2 *v);
162 EXPORT void vec2_ceil(struct vec2 *dst, const struct vec2 *v);
163 EXPORT int vec2_close(const struct vec2 *v1, const struct vec2 *v2,
164  float epsilon);
165 EXPORT void vec2_norm(struct vec2 *dst, const struct vec2 *v);
166 
167 #ifdef __cplusplus
168 }
169 #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