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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef __VECTOR_H__
#define __VECTOR_H__
/**
* A real-valued 2-dimensional vector.
* Positive x is towards the right; positive y is towards the top.
* vector_t is defined here instead of vector.c because it is passed *by value*.
*/
typedef struct {
double x;
double y;
} vector_t;
/**
* The zero vector, i.e. (0, 0).
* "extern" declares this global variable without allocating memory for it.
* You will need to define "const vector_t VEC_ZERO = ..." in vector.c.
*/
extern const vector_t VEC_ZERO;
/**
* Adds two vectors.
* Performs the usual componentwise vector sum.
*
* @param v1 the first vector
* @param v2 the second vector
* @return v1 + v2
*/
vector_t vec_add(vector_t v1, vector_t v2);
/**
* Subtracts two vectors.
* Performs the usual componentwise vector difference.
*
* @param v1 the first vector
* @param v2 the second vector
* @return v1 - v2
*/
vector_t vec_subtract(vector_t v1, vector_t v2);
/**
* Computes the additive inverse a vector.
* This is equivalent to multiplying by -1.
*
* @param v the vector whose inverse to compute
* @return -v
*/
vector_t vec_negate(vector_t v);
/**
* Multiplies a vector by a scalar.
* Performs the usual componentwise product.
*
* @param scalar the number to multiply the vector by
* @param v the vector to scale
* @return scalar * v
*/
vector_t vec_multiply(double scalar, vector_t v);
/**
* Computes the dot product of two vectors.
* See https://en.wikipedia.org/wiki/Dot_product#Algebraic_definition.
*
* @param v1 the first vector
* @param v2 the second vector
* @return v1 . v2
*/
double vec_dot(vector_t v1, vector_t v2);
/**
* Computes the cross product of two vectors,
* which lies along the z-axis.
* See https://en.wikipedia.org/wiki/Cross_product#Computing_the_cross_product.
*
* @param v1 the first vector
* @param v2 the second vector
* @return the z-component of v1 x v2
*/
double vec_cross(vector_t v1, vector_t v2);
/**
* Rotates a vector by an angle around (0, 0).
* The angle is given in radians.
* Positive angles are counterclockwise, according to the right hand rule.
* See https://en.wikipedia.org/wiki/Rotation_matrix.
* (You can derive this matrix by noticing that rotation by a fixed angle
* is linear and then computing what it does to (1, 0) and (0, 1).)
*
* @param v the vector to rotate
* @param angle the angle to rotate the vector
* @return v rotated by the given angle
*/
vector_t vec_rotate(vector_t v, double angle);
#endif // #ifndef __VECTOR_H__