aboutsummaryrefslogtreecommitdiff
path: root/vector.c
blob: 3595c1fcb774a5ebc14df072c09461ae5d69c2f2 (plain)
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 <u.h>
#include <libc.h>
#include <draw.h>
#include "dat.h"
#include "fns.h"

Vector
Vec(double x, double y)
{
	return (Vector){x, y};
}

Vector
addvec(Vector v, Vector u)
{
	return (Vector){v.x+u.x, v.y+u.y};
}

Vector
subvec(Vector v, Vector u)
{
	return (Vector){v.x-u.x, v.y-u.y};
}

Vector
mulvec(Vector v, double s)
{
	return (Vector){v.x*s, v.y*s};
}

double
dotvec(Vector v, Vector u)
{
	return v.x*u.x + v.y*u.y;
}

Vector
normvec(Vector v)
{
	double len;

	len = hypot(v.x, v.y);
	if(len == 0)
		return (Vector){0, 0};
	v.x /= len;
	v.y /= len;
	return v;
}

Vector
rotatevec(Vector p, double θ, Vector c)
{
	Vector r;

	p = subvec(p, c);
	r.x = p.x*cos(θ) - p.y*sin(θ);
	r.y = p.x*sin(θ) + p.y*cos(θ);
	r = addvec(r, c);
	return r;
}