-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.c
More file actions
executable file
·61 lines (50 loc) · 1.27 KB
/
vector.c
File metadata and controls
executable file
·61 lines (50 loc) · 1.27 KB
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
#include <math.h>
#include "vector.h"
#include "pi.h"
void vector_initialize(Vector * vector) {
vector->x = 0;
vector->y = 0;
}
void vector_rotate(Vector * vector, float radians) {
float new_x = vector->x * cosf(radians) - vector->y * sinf(radians);
float new_y = vector->x * sinf(radians) + vector->y * cosf(radians);
vector->x = new_x;
vector->y = new_y;
}
void vector_add(Vector * vector, Vector other) {
vector->x += other.x;
vector->y += other.y;
}
void vector_subtract(Vector * vector, Vector other) {
vector->x -= other.x;
vector->y -= other.y;
}
void vector_multiply(Vector * vector, Vector other) {
vector->x *= other.x;
vector->y *= other.y;
}
void vector_multiply_scalar(Vector * vector, float scalar) {
vector->x *= scalar;
vector->y *= scalar;
}
float vector_sq_distance(Vector a, Vector b) {
float dx = a.x - b.x;
float dy = a.y - b.y;
return dx * dx + dy * dy;
}
float vector_distance(Vector a, Vector b) {
return sqrtf(vector_sq_distance(a, b));
}
float vector_angle(Vector vector) {
return atan2f(vector.y, vector.x);
}
float vector_angle_between(Vector a, Vector b) {
vector_subtract(&b, a);
return vector_angle(b);
}
Vector vector_create(float x, float y) {
Vector vector;
vector.x = x;
vector.y = y;
return vector;
}