Description
Submission timestamps will be checked and enforced strictly by the CourseWeb; late submissions will not be accepted. Check the due date of this lab on the CourseWeb. Remember that, per the course syllabus, if you are not marked by your recitation instructor as having attended a recitation, your score will be cut in half.
For this lab, you are going to create your own library for vector and matrix operations. In this class, we are mainly use 4 1 matrices (column vectors) and 4 4 matrices. It is a good idea for you to create your own library to perform their operation. For this lab, you must create two les, header le and source le. You can name your le which ever you want (.h) for header le and (.c) for source le.
Vector and its Operations
A vector is a matrix (4 1) or column vector as shown below:

3
x

y 7

7
4z 5
w
In OpenGL (C programming) environment, a 4 1 column vector is simply a four consecutive region in the memory that contains four oatingpoint value. For simplicity, we can create a new type in your header le called vec4 as follows:
typedef struct
{
GLfloat x;
GLfloat y;
GLfloat z;
GLfloat w;
} vec4;
Note that if your system does not support GLfloat yet, just simply use float. GLfloat and float are identical. With the above declaration, a program can create the following vector:
2 3
1
627
v = 6 7
435
4
using the following code:
#include “common.h”
int main(void)
{
vec4 v = {1, 2, 3, 4};
:
}
CS 1566  Introduction to Computer Graphics Page 1
Lab 2: Library for Vector and Matrix Operation
Note that you can also use a onedimensional array of type GLfloat (or float) with four elements to represent a 4 1 column vector as shown below:
typedef float vec4[4];
int main(void)
{
vec4 v = {1,2,3,4};
:
}
In this section, create the following functions in your source le (and do not forget to put your function signatures into your header le). Note that you can change names and how your function passes values whichever way you prefer.
Prints a 4 1 column vector on the console screen: This function will be very useful to help you debug your code. To reduce the amount of space to print, you can simply print a vector like a row vector and limit each oatingpoint number to just two digits after decimal point (e.g., [1.23 2.34 3.45 4.56]).
ScalarVector Multiplication: Given a scalar value s (a oatingpoint number) and a vector v (4 1), this function should produce the result s v which is a 4 1 column vector.

2
y
3
=
2
y
3
6
x
7
6
x
7
z
z
6
w
7
6
w
7
4
5
4
5
VectorVector Addition: Given two vectors v_{1} and v_{2}, this function should produce the result v_{1} + v_{2} which is a column vector.

^{2}y_{1} ^{3}
+
^{2}y_{2} ^{3}
_{=} ^{2} y_{1}
+ y_{2} ^{3}
6
x_{1}
7
x_{2}
7
6
x_{1}
+ x_{2}
7
z_{1}
^{6}z_{2}
z_{1} + z_{2}
6
w_{1}
7
^{6}w_{2}
7
6
w_{1}
+ w_{2}
7
4
5
4
5
4
5
VectorVector Subtraction Given two vectors v_{1} and v_{2}, this function should produce the
result v_{1} v_{2} which is a column vector.

^{2}y_{1} ^{3}
^{2}y_{2} ^{3}
_{=} ^{2} y_{1}
y_{2} ^{3}
6
x_{1}
7
x_{2}
7
6
x_{1}
x_{2}
7
z_{1}
^{6}z_{2}
z_{1}
z_{2}
6
^{w}1
7
^{6}w_{2}
7
6
^{w}1
^{w}2
7
4
5
4
5
4
5
Magnitude of a Vector The magnitude of a fourelement vector v denoted by jvj is as
follows:
p
jvj = x^{2} + y^{2} + z^{2} + w^{2}
CS 1566  Introduction to Computer Graphics Page 2
Lab 2: Library for Vector and Matrix Operation
Normalize A normalized vector is a vector with magnitude 1. A normalized of a vector v is a vector v^ codirectional with v and jv^j = 1. The vector v^ can be calculated by

v^ = ^{1}
2
y
3
x
6
z
7
jvj
6
w
7
4
5
Dot product: Given two vectors v_{1} and v_{2}, this function should produce the result v_{1} v_{2}.
Note that the result is a scalar value (a oatingpoint number).
2323
x_{1} x_{2}
^{6y}^{1} ^{7 6y}^{2} ^{7} = (x x ) + (y y ) + (z z ) + (w w )
6767 _{1} _{2} _{1} _{2} _{1} _{2} _{1} _{2}
4z_{1} 5 4z_{2} 5
w_{1} w_{2}
Cross product: Given two vectors v_{1} and v_{2}, this function should produce the result v_{1} v_{2} which is a column vector. Note that the fourth element of the result vector should always be 0. The calculation should ignore the fourth elements of both v_{1} and v_{2}. Do not forget about the righthand rule.

^{2}y_{1}
3
^{2}y_{2} ^{3} _{=}
^{2}(z_{1}
x_{2})
(x_{1}
z_{2})^{3}
6
x_{1}
7
6
x_{2}
7
6
(y_{1}
z_{2})
(z_{1}
y_{2})
7
^{z}1
^{z}2
(x_{1} y_{2})
(y_{1}
x_{2})
6
^{w}1
7
6
^{w}2
7
6
0:0
7
4
5
4
5
4
5
Again, you can name your functions and their signatures whichever way you want.
Matrix and its Operations
In OpenGL (C programming) environment, a 4 4 matrix is simply a four consecutive region in the memory that contains 16 oatingpoint value organized according to Column Major. With the declaration of a fourelement column vector, a 4 4 matrix can be created as follows:
typedef struct
{
vec4 x;
vec4 y;
vec4 z;
vec4 w;
} mat4;
Note that the above mat4 data type is a column major. Each column is a vector (vec4). With the above data type, we can declare the following 4 4 matrix:
2
1
6 5
6
4 9
13
using the following code snippet:
6 
7 
8 
3 
2 
3 
4 

10 
11 
12^{7} 

5 

14 
15 
16^{7} 
CS 1566  Introduction to Computer Graphics Page 3
Lab 2: Library for Vector and Matrix Operation
mat4 m = {{1,5,9,13},{2,6,10,14},{3,7,11,15},{4,8,12,16}};
You can also use a 16element onedimensional array of oatingpoints as shown below:
typedef float mat4[16];
However, do not forget that it must be a column major. Suppose a 4 4 matrix is declared (from onedimensional array of oatingpoints) as shown below:
typedef float mat4[16];
int main(void)
{
mat4 m = …;
:
}
the actual matrix represented by the above declaration is shown below:

2
m[0]
m[4]
m[8]
m[12]
3
6
m[1]
m[5]
m[9]
m[13]
7
m[2]
m[6]
m[10]
m[14]
4
5
6
m[3]
m[7]
m[11]
m[15]
7
In this section, create the following functions for matrix operations:
Print a matrix on the console screen for debugging purpose
ScalarMatrix multiplication: Given a scalar value s and a matrix m, this function should
produce the result s m which is a matrix.

^{2}a_{21}
^{a}22
^{a}23
a_{24}^{3}
=
^{2} a_{21}
a_{22}
a_{23}
a_{24}^{3}
^{a}11
^{a}12
^{a}13
^{a}14
7
a_{11}
a_{12}
a_{13}
a_{14}
7
^{6}a_{31} a_{32}
^{a}33
^{a}34
^{6} a_{31}
a_{32}
a_{33}
a_{34}
^{6}a_{41}
^{a}42
^{a}43
^{a}44
7
6
^{a}41
^{a}42
^{a}43
^{a}44
7
4
5
4
5
MatrixMatrix Addition: Given two matrices m_{1} and m_{2}, this function should produce
the result m_{1} + m_{2} which is a matrix.

2
^{a}21
^{a}22
^{a}23
^{a}24
3
+
2
^{b}21
^{b}22
^{b}23
^{b}24
3
=
^{2}a_{21}
^{+} ^{b}21
^{a}22
^{+} ^{b}22
^{a}23
^{+} ^{b}23
^{a}24
^{+} ^{b}24
3
6
^{a}11
^{a}12
^{a}13
^{a}14
7
6
^{b}11
^{b}12
^{b}13
^{b}14
7
^{a}11
+ b_{11}
^{a}12
+ b_{12}
^{a}13
+ b_{13}
^{a}14
+ b_{14}
7
^{a}31
^{a}32
^{a}33
^{a}34
^{b}31
^{b}32
^{b}33
^{b}34
^{6}a_{31} + b_{31}
^{a}32 ^{+} ^{b}32
^{a}33 ^{+} ^{b}33
^{a}34
^{+} ^{b}34
6
^{a}41
^{a}42
^{a}43
^{a}44
7
6
^{b}41
^{b}42
^{b}43
^{b}44
7
^{6}a_{41}
+ b_{41}
^{a}42 ^{+} ^{b}42
^{a}43 ^{+} ^{b}43
^{a}44
+ b_{44}
7
4
5
4
5
4
5
MatrixMatrix Subtraction: Given two matrices m_{1} and m_{2}, this function should produce

the result m_{1}
m_{2} which is a matrix.
3
^{2}a_{21}
3
2
^{a}21
^{a}22
^{a}23
^{a}24
3
2
^{b}21
^{b}22
^{b}23
^{b}24
=
^{b}21
^{a}22
^{b}22
^{a}23
^{b}23
^{a}24
^{b}24
6
^{a}11
^{a}12
^{a}13
^{a}14
7
6
^{b}11
^{b}12
^{b}13
^{b}14
7
^{a}11
^{b}11
^{a}12
^{b}12
^{a}13
^{b}13
^{a}14
^{b}14
7
^{a}31
^{a}32
^{a}33
^{a}34
^{b}31
^{b}32
^{b}33
^{b}34
^{6}a_{31}
^{b}31
^{a}32
^{b}32
^{a}33
^{b}33
^{a}34
^{b}34
6
^{a}41
^{a}42
^{a}43
^{a}44
7
6
^{b}41
^{b}42
^{b}43
^{b}44
7
^{6}a_{41}
^{b}41
^{a}42
^{b}42
^{a}43
^{b}43
^{a}44
^{b}44
7
4
5
4
5
4
5
CS 1566  Introduction to Computer Graphics Page 4
Lab 2: Library for Vector and Matrix Operation
MatrixMatrix multiplication: Given two matrices m_{1} and m_{2}, this function should
produce the result m_{1} m_{2} which is a matrix.

^{2}a_{21}
^{a}22
^{a}23
^{a}24
3
2
^{b}21
^{b}22
^{b}23
^{b}24
3
=
^{2}c_{21}
^{c}22
^{c}23
^{c}24
3
^{a}11
^{a}12
^{a}13
^{a}14
7
6
^{b}11
^{b}12
^{b}13
^{b}14
7
^{c}11
^{c}12
^{c}13
^{c}14
7
^{6}a_{31}
^{a}32
^{a}33
^{a}34
^{b}31
^{b}32
^{b}33
^{b}34
^{6}c_{31}
^{c}32
^{c}33
^{c}34
^{6}a_{41}
^{a}42
^{a}43
^{a}44
7
6
^{b}41
^{b}42
^{b}43
^{b}44
7
^{6}c_{41}
^{c}42
^{c}43
^{c}44
7
4
5
4
5
4
5
where
c_{11} = (a_{11} b_{11}) + (a_{12} b_{21}) + (a_{13} b_{31}) + (a_{14} c_{21} = (a_{21} b_{11}) + (a_{22} b_{21}) + (a_{23} b_{31}) + (a_{24} c_{31} = (a_{31} b_{11}) + (a_{32} b_{21}) + (a_{33} b_{31}) + (a_{34} c_{41} = (a_{41} b_{11}) + (a_{42} b_{21}) + (a_{43} b_{31}) + (a_{44} c_{12} = (a_{11} b_{12}) + (a_{12} b_{22}) + (a_{13} b_{32}) + (a_{14} c_{22} = (a_{21} b_{12}) + (a_{22} b_{22}) + (a_{23} b_{32}) + (a_{24} c_{32} = (a_{31} b_{12}) + (a_{32} b_{22}) + (a_{33} b_{32}) + (a_{34} c_{42} = (a_{41} b_{12}) + (a_{42} b_{22}) + (a_{43} b_{32}) + (a_{44} c_{13} = (a_{11} b_{13}) + (a_{12} b_{23}) + (a_{13} b_{33}) + (a_{14} c_{23} = (a_{21} b_{13}) + (a_{22} b_{23}) + (a_{23} b_{33}) + (a_{24} c_{33} = (a_{31} b_{13}) + (a_{32} b_{23}) + (a_{33} b_{33}) + (a_{34} c_{43} = (a_{41} b_{13}) + (a_{42} b_{23}) + (a_{43} b_{33}) + (a_{44} c_{14} = (a_{11} b_{14}) + (a_{12} b_{24}) + (a_{13} b_{34}) + (a_{14} c_{24} = (a_{21} b_{14}) + (a_{22} b_{24}) + (a_{23} b_{34}) + (a_{24} c_{34} = (a_{31} b_{14}) + (a_{32} b_{24}) + (a_{33} b_{34}) + (a_{34} c_{44} = (a_{41} b_{14}) + (a_{42} b_{24}) + (a_{43} b_{34}) + (a_{44}
b_{41}) b_{41}) b_{41}) b_{41}) b_{42}) b_{42}) b_{42}) b_{42}) b_{43}) b_{43}) b_{43}) b_{43}) b_{44}) b_{44}) b_{44}) b_{44})
Inverse of a Matrix: Given a matrix m this function should produce the result m ^{1} which is a matrix. It may be a good idea for you to test by simply calculate whether m m ^{1} = 1 and m ^{1} m = 1. See the lecture slides on how to calculate the inverse of a 4 4 matrix.
Transpose of a Matrix: Given a matrix m this function should produce the result m^{T}
which is a matrix.

2
^{a}21
^{a}22
^{a}23
^{a}24
3
T
^{2}a_{12}
^{a}22
^{a}32
^{a}42
3
=
6
^{a}11
^{a}12
^{a}13
^{a}14
7
^{a}11
^{a}21
^{a}31
^{a}41
7
^{a}31
^{a}32
^{a}33
^{a}34
^{6}a_{13}
^{a}23
^{a}33
^{a}43
6
^{a}41
^{a}42
^{a}43
^{a}44
7
^{6}a_{14}
^{a}24
^{a}34
^{a}44
7
4
5
4
5
MatrixVector Multiplication: Given a matrix m and a vector v, this function should
produce the result m v which is a vector.

^{2}a_{21}
^{a}22
^{a}23
^{a}24
3
^{2}y ^{3} _{=}
^{2}(a_{21}
x) + (a_{22}
y) + (a_{23}
z) + (a_{24}
w)^{3}
^{a}11
^{a}12
^{a}13
^{a}14
7
6
x
7
(a_{11}
x) + (a_{12}
y) + (a_{13}
z) + (a_{14}
w)
7
^{6}a_{31} a_{32}
^{a}33
^{a}34
z
^{6}(a_{31} x) + (a_{32} y) + (a_{33} z) + (a_{34} w)
^{6}a_{41}
^{a}42
^{a}43
^{a}44
7
6
w
7
^{6}(a_{41}
x) + (a_{42}
y) + (a_{43}
z) + (a_{44}
w)
7
4
5
4
5
4
5
Again, you can name your functions and their signatures whichever way you want.
CS 1566  Introduction to Computer Graphics Page 5
Lab 2: Library for Vector and Matrix Operation
Test Your Library
Again, your library should be in two les, a header le and a source le. Now, you should test your library a little bit. For this section, create a new program named test.c. In this program, create the following vectors and matrices:

v_{1}
=
2_{2}3
v_{2}
=
2_{6}3
m_{1} = ^{2}
5
6
7
8
3
_{m}_{2} _{=} ^{2} 8
7
6
5 ^{3}
1
7
5
7
6
1
2
3
4
7
6
4
3
2
1
7
6_{3}
6_{7}
9
10
11
12
12
11
10
9
6_{4}
7
6_{8}
7
6
13
14
15
16
7
6
16
15
14
13^{7}
4 5
4 5
4
5
4
5
and a scalar value s = 3:0. The test program should print out the following result in this order:
s v_{1}
v_{1} + v_{2} v_{1} v_{2}
jvj v^
v_{1} v_{2} v_{1} v_{2} s m_{1}
m_{1} + m_{2} m_{1} m_{2}
m_{1} m_{2}
m_{1} ^{1}
m^{T}_{1}
m_{1} v_{1}
Submission
Zip your header le, source le, and test le into a le named lab02.zip. Submit your lab02.zip le via CourseWeb before the due date stated on the CourseWeb.
CS 1566  Introduction to Computer Graphics Page 6