summaryrefslogtreecommitdiffstats
path: root/vec.c
blob: ba8edfa8bb86d5ea4398ae441e098fa23409b415 (plain) (blame)
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
#include "balls.h"

Vec
vsub(Vec v1, Vec v2) {
	return V(v1.x-v2.x, v1.y-v2.y);
}

Vec
vmuls(Vec v, int a) {
	return V(v.x*a, v.y*a);
}

Vec
vdivs(Vec v, int a) {
	if (a == 0)
		return V(0, 0);
	return V(v.x/a, v.y/a);
}

int
vdot(Vec v1, Vec v2) {
	return v1.x*v2.x + v1.y*v2.y;
}

int
vlen(Vec v) {
	return sqrt(v.x*v.x + v.y*v.y);
}

Vec
unitnorm(Vec v) {
	return vdivs(v, vlen(v));
}

Point
ptaddv(Point p, Vec v) {
	p.x += v.x;
	p.y += v.y;
	return p;
}

Vec
V(int x, int y) {
	Vec v = {x, y};
	return v;
}

Vec
Vpt(Point p, Point q) {
	return V(p.x-q.x, p.y-q.y);
}