aboutsummaryrefslogtreecommitdiff
path: root/vector3.c
blob: 933716e081d228b9cd141073bc7eb4ca1a0874c3 (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
#include <u.h>
#include <libc.h>
#include <draw.h>
#include "dat.h"
#include "fns.h"

Vector3
Vec3(double x, double y, double z)
{
	return (Vector3){x, y, z};
}

Vector3
addvec3(Vector3 v, Vector3 u)
{
	return (Vector3){v.x+u.x, v.y+u.y, v.z+u.z};
}

Vector3
subvec3(Vector3 v, Vector3 u)
{
	return (Vector3){v.x-u.x, v.y-u.y, v.z-u.z};
}

Vector3
mulvec3(Vector3 v, double s)
{
	return (Vector3){v.x*s, v.y*s, v.z*s};
}

double
dotvec3(Vector3 v, Vector3 u)
{
	return v.x*u.x + v.y*u.y + v.z*u.z;
}

Vector3
crossvec(Vector3 v, Vector3 u)
{
	return (Vector3){
		v.y*u.z - v.z*u.y,
		v.z*u.x - v.x*u.z,
		v.x*u.y - v.y*u.x
	};
}

Vector3
normvec3(Vector3 v)
{
	double len;

	len = hypot3(v.x, v.y, v.z);
	if(len == 0)
		return (Vector3){0, 0, 0};
	v.x /= len;
	v.y /= len;
	v.z /= len;
	return v;
}