-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector2.c
More file actions
54 lines (40 loc) · 989 Bytes
/
vector2.c
File metadata and controls
54 lines (40 loc) · 989 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct Vector2 {
int X;
int Y;
};
struct Vector2* vector2_new(int x, int y) {
struct Vector2 *obj = malloc(sizeof(struct Vector2));
obj->X = x;
obj->Y = y;
return obj;
}
void vector2_show(struct Vector2 *this) {
printf("X=%d; Y=%d\n", this->X, this->Y);
}
void vector2_set_x(struct Vector2 *this, int x) {
this->X = x;
}
void vector2_set_y(struct Vector2 *this, int y) {
this->Y = y;
}
double vector2_length(struct Vector2 *this) {
return sqrt((this->X * this->X) + (this->Y * this->Y));
}
int main (int argc, char **argv) {
struct Vector2 *v = vector2_new(1,1);
// This prints X=1; Y=1
vector2_show(v);
// Setting X to 3
vector2_set_x(v, 3);
// prints X=3; Y=1
vector2_show(v);
// calculating length of vector2
double length = vector2_length(v);
// Prints: Length=3.162278
printf("Length=%f\n", length);
free(v);
return 0;
}