-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathvector.c
60 lines (52 loc) · 838 Bytes
/
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
#include <math.h>
#include "vector.h"
Vec2d vsub(Vec2d v1, Vec2d v2)
{
Vec2d res;
res.x = v1.x - v2.x;
res.y = v1.y - v2.y;
return res;
}
Vec2d vadd(Vec2d v1, Vec2d v2)
{
Vec2d res;
res.x = v1.x + v2.x;
res.y = v1.y + v2.y;
return res;
}
Vec2d vmul(Vec2d v, double d)
{
Vec2d res;
res.x = v.x * d;
res.y = v.y * d;
return res;
}
Vec2d vdiv(Vec2d v, double d)
{
Vec2d res;
res.x = v.x / d;
res.y = v.y / d;
return res;
}
double length(Vec2d v)
{
return sqrt(v.x*v.x + v.y*v.y);
}
double distance(Vec2d v1, Vec2d v2)
{
return length(vsub(v1, v2));
}
Vec2f d2f(Vec2d v)
{
Vec2f res;
res.x = v.x;
res.y = v.y;
return res;
}
Vec2d f2d(Vec2f v)
{
Vec2d res;
res.x = v.x;
res.y = v.y;
return res;
}