-
Notifications
You must be signed in to change notification settings - Fork 10
/
vector.c
304 lines (237 loc) · 4.8 KB
/
vector.c
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* vector.c
* XBolo
*
* Created by Robert Chrzanowski.
* Copyright 2004 Robert Chrzanowski. All rights reserved.
*
*/
#include "vector.h"
#include <stdint.h>
#include <float.h>
#include <math.h>
const float kPif = 3.14159265358979;
const float k2Pif = 6.28318530717959;
/*
* converts a unsigned integer to a float
*/
float u16tof(uint16_t s) {
return ((float)s)/((float)INT16RADIX);
}
/*
* converts a float to a unsigned integer
*/
uint16_t ftou16(float s) {
return (int16_t)(s*((float)INT16RADIX));
}
/*
* converts a integer to a float
*/
float i16tof(int16_t s) {
return ((float)s)/((float)INT16RADIX);
}
/*
* converts a float to a integer
*/
int16_t ftoi16(float s) {
return (int16_t)(s*((float)INT16RADIX));
}
Vec2f make2f(float x, float y) {
Vec2f r;
r.x = x;
r.y = y;
return r;
}
Vec2f neg2f(Vec2f v) {
return make2f(-v.x, -v.y);
}
Vec2f add2f(Vec2f v1, Vec2f v2) {
return make2f(v1.x + v2.x, v1.y + v2.y);
}
Vec2f sub2f(Vec2f v1, Vec2f v2) {
return make2f(v1.x - v2.x, v1.y - v2.y);
}
Vec2f mul2f(Vec2f v, float s) {
return make2f(v.x*s, v.y*s);
}
Vec2f div2f(Vec2f v, float s) {
return make2f(v.x/s, v.y/s);
}
float dot2f(Vec2f v1, Vec2f v2) {
return v1.x*v2.x + v1.y*v2.y;
}
float mag2f(Vec2f v) {
return sqrt(dot2f(v, v));
}
Vec2f unit2f(Vec2f v) {
return div2f(v, mag2f(v));
}
Vec2f prj2f(Vec2f v1, Vec2f v2) {
return mul2f(v1, dot2f(v1, v2)/dot2f(v1, v1));
}
float cmp2f(Vec2f v1, Vec2f v2) {
return dot2f(v1, v2)/mag2f(v1);
}
int isequal2f(Vec2f v1, Vec2f v2) {
return v1.x == v2.x && v1.y == v2.y;
}
Vec2f tan2f(float theta) {
return make2f(cos(theta), sin(theta));
}
float _atan2f(Vec2f dir) {
return atan2f(dir.y, dir.x);
}
Vec2i32 make2i32(int32_t x, int32_t y) {
Vec2i32 r;
r.x = x;
r.y = y;
return r;
}
Vec2i32 neg2i32(Vec2i32 v) {
return make2i32(-v.x, -v.y);
}
Vec2i32 add2i32(Vec2i32 v1, Vec2i32 v2) {
return make2i32(v1.x + v2.x, v1.y + v2.y);
}
Vec2i32 sub2i32(Vec2i32 v1, Vec2i32 v2) {
return make2i32(v1.x - v2.x, v1.y - v2.y);
}
Vec2i32 mul2i32(Vec2i32 v, int32_t s) {
return make2i32(v.x*s, v.y*s);
}
Vec2i32 div2i32(Vec2i32 v, int32_t s) {
return make2i32(v.x/s, v.y/s);
}
int32_t dot2i32(Vec2i32 v1, Vec2i32 v2) {
return v1.x*v2.x + v1.y*v2.y;
}
int32_t mag2i32(Vec2i32 v) {
return sqrt(dot2i32(v, v));
}
Vec2i32 prj2i32(Vec2i32 v1, Vec2i32 v2) {
return mul2i32(v1, dot2i32(v1, v2)/dot2i32(v1, v1));
}
int32_t cmp2i32(Vec2i32 v1, Vec2i32 v2) {
return dot2i32(v1, v2)/mag2i32(v1);
}
int isequal2i32(Vec2i32 v1, Vec2i32 v2) {
return v1.x == v2.x && v1.y == v2.y;
}
Vec2i32 tan2i32(uint8_t dir) {
return make2i32(cos(dir*(kPif/8.0f))*INT32_MAX, sin(dir*(kPif/8.0f))*INT32_MAX);
}
Vec2i32 scale2i32(uint8_t dir, int32_t scale) {
return div2i32(tan2i32(dir), INT32_MAX/scale);
}
Vec2i16 c2i32to2i16(Vec2i32 v) {
return make2i16(v.x, v.y);
}
/*
* returns a vector with x and y
*/
Vec2i16 make2i16(int16_t x, int16_t y) {
Vec2i16 r;
r.x = x;
r.y = y;
return r;
}
/*
* r = -v
*/
Vec2i16 neg2i16(Vec2i16 v) {
Vec2i16 r;
r.x = -v.x;
r.y = -v.y;
return r;
}
/*
* r = v1 + v2
*/
Vec2i16 add2i16(Vec2i16 v1, Vec2i16 v2) {
Vec2i16 r;
r.x = v1.x + v2.x;
r.y = v1.y + v2.y;
return r;
}
/*
* r = v1 - v2
*/
Vec2i16 sub2i16(Vec2i16 v1, Vec2i16 v2) {
Vec2i16 r;
r.x = v1.x - v2.x;
r.y = v1.y - v2.y;
return r;
}
/*
* r = v X s
*/
Vec2i16 mul2i16(Vec2i16 v, int16_t s) {
Vec2i16 r;
// r.x = ((int32_t)v.x)*((int32_t)s)/((int32_t)INT16RADIX);
// r.y = ((int32_t)v.y)*((int32_t)s)/((int32_t)INT16RADIX);
r.x = v.x*s;
r.y = v.y*s;
return r;
}
/*
* r = v/s
*/
Vec2i16 div2i16(Vec2i16 v, int16_t s) {
Vec2i16 r;
// r.x = ((int32_t)v.x)*((int32_t)INT16RADIX)/((int32_t)s);
// r.y = ((int32_t)v.y)*((int32_t)INT16RADIX)/((int32_t)s);
r.x = v.x/s;
r.y = v.y/s;
return r;
}
/*
* r = v1*v2
*/
int16_t dot2i16(Vec2i16 v1, Vec2i16 v2) {
return v1.x*v2.x + v1.y*v2.y;
}
/*
* r = |v|
*/
int16_t mag2i16(Vec2i16 v) {
return sqrt(dot2i16(v, v));
}
/*
* r = v1.proj(v2)
*/
Vec2i16 prj2i16(Vec2i16 v1, Vec2i16 v2) {
return mul2i16(v1, dot2i16(v1, v2)/dot2i16(v1, v1));
}
/*
* r = v1.comp(v2)
*/
int16_t cmp2i16(Vec2i16 v1, Vec2i16 v2) {
return dot2i16(v1, v2)/mag2i16(v1);
}
/*
* returns 0 if v1 != v2
*/
int isequal2i16(Vec2i16 v1, Vec2i16 v2) {
return v1.x == v2.x && v1.y == v2.y;
}
Vec2i16 tan2i16(uint8_t dir) {
return make2i16(cos(dir*(kPif/8.0f))*INT16_MAX, sin(dir*(kPif/8.0f))*INT16_MAX);
}
Vec2i16 scale2i16(uint8_t dir, int16_t scale) {
return div2i16(tan2i16(dir), INT16_MAX/scale);
}
Vec2i8 c2i16to2i8(Vec2i16 v) {
return make2i8(v.x, v.y);
}
/*
* U8to2i16() converts dir to a vector.
*/
Vec2i8 make2i8(int8_t x, int8_t y) {
Vec2i8 r;
r.x = x;
r.y = y;
return r;
}
int isequal2i8(Vec2i8 v1, Vec2i8 v2) {
return v1.x == v2.x && v1.y == v2.y;
}