miniRT
By Marcelo Magalhães and Ygor G. Sena, 2023.
matrices.h File Reference
#include "libft.h"
#include "tuples.h"
Include dependency graph for matrices.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  s_matrix
 The struct t_matrix stores a matrix up to 4x4 and size of 16. A matrix is a grid of numbers that can be manipulated as a single unit. Matrices and their operations are the foundational basis which will helps us to manipulate points, vectors and, ultimately, shapes. More...
 
struct  s_shearing
 The struct t_shearing is a helper type to allow shearing operation on matrices. Shearing (or skew) is a complex operation where the coordinate is changed in proportion to the other two coordinates. When doing it to a matrix, it means that x will change in proportion to y and z, y will change in proportion to x and z and, lastly, z will change in proportion to x and y. For example, to apply shearing to the x axis of a given matrix, we pass to shearing() a struct of type t_shearing containing the y and z values. For instance, when done to the y axis, we pass p1 and p2 which stands for x and z. Lastly, when applying it to the z axis, we pass x and y as p1 and p2, respectively. More...
 

Macros

#define MAX   4
 
#define SIZE   16
 

Typedefs

typedef struct s_matrix t_matrix
 The struct t_matrix stores a matrix up to 4x4 and size of 16. A matrix is a grid of numbers that can be manipulated as a single unit. Matrices and their operations are the foundational basis which will helps us to manipulate points, vectors and, ultimately, shapes. More...
 
typedef struct s_shearing t_shearing
 The struct t_shearing is a helper type to allow shearing operation on matrices. Shearing (or skew) is a complex operation where the coordinate is changed in proportion to the other two coordinates. When doing it to a matrix, it means that x will change in proportion to y and z, y will change in proportion to x and z and, lastly, z will change in proportion to x and y. For example, to apply shearing to the x axis of a given matrix, we pass to shearing() a struct of type t_shearing containing the y and z values. For instance, when done to the y axis, we pass p1 and p2 which stands for x and z. Lastly, when applying it to the z axis, we pass x and y as p1 and p2, respectively. More...
 

Functions

t_matrix multiply_mx_mx (t_matrix a, t_matrix b)
 This function multiplies two matrices. "Mx" means matrix. For more information about how matrix multiplication works, refer to the book TRTC on page 28. More...
 
t_tuple multiply_tp_mx (t_matrix a, t_tuple b)
 This function multiplies a matrix by a tuple. "Mx" means matrix and "tp" means tuple. For more information about how vector and matrix multiplication works, refer to the following link quoted by author J. Buck: https://betterexplained.com/articles/linear-algebra-guide/. More...
 
t_matrix transpose (t_matrix t)
 This function transposes the matrix passed to the function as parameter. In other words, it turns the first row into the first column, the second row into the second column, and so forth. Thus, it's importante to remember that the transpose of the identity matrix always gives the own identity matrix as result. More...
 
t_matrix inverse (t_matrix t)
 This function get the inverse of a given matrix. That is, if you multiply some matrix A by another matrix B, producing C, you can multiply C by the inverse of B to get A again. To invert the matrix, this function employs a method known as cofactor expansion. Additionally, there some interesting properties of the inverse: 1) if you invert an identity matrix, the result is also an identity matrix. 2) when you multiply a matrix by its inverse, you get as result the identity matrix. 3) The inverse of the transpose of a matrix and the transpose of the inverse only commute if the matrix is symmetric or orthogonal. Otherwise, the result of the two operations will always be different. More...
 
t_matrix translation (double x, double y, double z)
 This function translates a given matrix. Translation is a transformation that moves a point. Thus, it's important to note that the inverse of a translation matrix is another translation matrix that moves points in reverse. More...
 
t_matrix scaling (double x, double y, double z)
 This function scales a given matrix by multiplication. When applied to an object centered at the origin, this transformation scales all points on the object, effectively making it larger (if the scale value is greater than 1) or smaller (if the scale value is less than 1). Note that reflection is scaling by a negative value. More...
 
t_matrix shearing (t_shearing x, t_shearing y, t_shearing z)
 This function applies shearing (or skew) transformation to a given matrix. It has the effect of making straight lines slanted. Each coordinate is changed in proportion to the other two coordinates. When doing it to a matrix, it means that x will change in proportion to y and z, y will change in proportion to x and z and, lastly, z will change in proportion to x and y. More...
 
t_matrix rotation_x (double rad)
 This function rotates a given matrix around the x-axis. More...
 
t_matrix rotation_y (double rad)
 This function rotates a given matrix around the y-axis. More...
 
t_matrix rotation_z (double rad)
 This function rotates a given matrix around the z-axis. More...
 
double get_determinant (t_matrix t)
 This function gets the determinant of a given matrix. The determinant is a number that is derived from the elements of a matrix. The name comes from the use of matrices to solve systems of equations, where it’s used to determine whether or not the system has a solution. If the determinant is zero, then the corresponding system of equations has no solution. More...
 
t_matrix get_submatrix (t_matrix t, size_t del_row, size_t del_col)
 This functions gets the submatrix of a given matrix. That is, what is left when you delete a single row and column from a matrix. The submatrix of a 4x4 matrix is 3x3 and the submatrix of a 3x3 matrix is 2x2. More...
 
double get_minor (t_matrix t, size_t row, size_t col)
 This function gets the minor of a given matrix. To put it differently, the minor of an element at row i and column j is the determinant of the submatrix at (i,j). More...
 
double get_cofactor (t_matrix t, size_t row, size_t col)
 This function gets the cofactor of a given matrix. Cofactors are minors that have (possibly) had their sign changed. If row + column equals to an odd number as result, the minor must be negated. Therefore, if the row and column identifies a spot with a +, then the minor’s sign doesn’t change. If the row and column identifies a spot with a -, then the minor must be negated. More...
 
t_matrix get_identity_matrix (void)
 This fucntion gets a new matrix with the special attribute of being an identity matrix or multiplicative identity. That is, if you multiply any matrix or tuple by the identity matrix, you get back the matrix or tuple you started with. More...
 
t_shearing to_shear (double a, double b)
 
t_matrix create_matrix (const double table[MAX][MAX], size_t size)
 This function creates a matrix up to 4x4 and, therefore, size of 16 (4x4). For instance, a matrix of 3x3 will have a size of 9(3x3). More...
 
int compare_matrix (t_matrix a, t_matrix b)
 The function compare_matrix() checks if two matrices are identical or not. The matrices passed as parameter to the function are compared with ft_memcmp(). More...
 
t_bool is_invertible (t_matrix t)
 This function checks if a given matrix is invertible or not. Not every matrix is invertible. An matrix is invertible if its determinant is a non-zero number. If its determinant is 0, then the given matrix is not invertible. More...
 
t_matrix rotation_matrix (t_vector vector)
 Calculates the rotation matrix for a given vector. More...
 

Typedef Documentation

◆ t_matrix

typedef struct s_matrix t_matrix

The struct t_matrix stores a matrix up to 4x4 and size of 16. A matrix is a grid of numbers that can be manipulated as a single unit. Matrices and their operations are the foundational basis which will helps us to manipulate points, vectors and, ultimately, shapes.

Parameters
matrixStores a matrix.
sizeStores the size of the matrix. For example, a matrix of 3x3 has a size of 9. For instance, a 2x2 matrix has a size of 4.

◆ t_shearing

typedef struct s_shearing t_shearing

The struct t_shearing is a helper type to allow shearing operation on matrices. Shearing (or skew) is a complex operation where the coordinate is changed in proportion to the other two coordinates. When doing it to a matrix, it means that x will change in proportion to y and z, y will change in proportion to x and z and, lastly, z will change in proportion to x and y. For example, to apply shearing to the x axis of a given matrix, we pass to shearing() a struct of type t_shearing containing the y and z values. For instance, when done to the y axis, we pass p1 and p2 which stands for x and z. Lastly, when applying it to the z axis, we pass x and y as p1 and p2, respectively.

Parameters
p1The first value to be used as proportion for shearing.
p2The second value to be used as proportion for shearing.

Function Documentation

◆ compare_matrix()

int compare_matrix ( t_matrix  a,
t_matrix  b 
)

The function compare_matrix() checks if two matrices are identical or not. The matrices passed as parameter to the function are compared with ft_memcmp().

Parameters
aReceives the first matrix to be compared.
bReceives the second matrix to be compared.
Returns
The function returns 0 if the matrices are identical. Otherwise, it returns a non-zero integer.

◆ create_matrix()

t_matrix create_matrix ( const double  table[MAX][MAX],
size_t  size 
)

This function creates a matrix up to 4x4 and, therefore, size of 16 (4x4). For instance, a matrix of 3x3 will have a size of 9(3x3).

Parameters
tableA bidimensional array of const double containing the matrix.
sizeA size_t value containing how much elements the array has.
Returns
Returns successfully a new matrix created with ft_memmove() from the array and array's size passed as parameter to the function.

◆ get_cofactor()

double get_cofactor ( t_matrix  t,
size_t  row,
size_t  col 
)

This function gets the cofactor of a given matrix. Cofactors are minors that have (possibly) had their sign changed. If row + column equals to an odd number as result, the minor must be negated. Therefore, if the row and column identifies a spot with a +, then the minor’s sign doesn’t change. If the row and column identifies a spot with a -, then the minor must be negated.

Parameters
tReceives the matrix from which the cofactor will be calculated.
rowReceives the row value from which the cofactor will be calculated.
colReceives the column value from which the cofactor will be calculated.
Returns
Returns the cofactor of the matrix passed to the function as parameter.

◆ get_determinant()

double get_determinant ( t_matrix  t)

This function gets the determinant of a given matrix. The determinant is a number that is derived from the elements of a matrix. The name comes from the use of matrices to solve systems of equations, where it’s used to determine whether or not the system has a solution. If the determinant is zero, then the corresponding system of equations has no solution.

Parameters
tReceives the matrix to calculate its determinant.
Returns
Returns a double value which is the determinant of the matrix passed to the function as parameter.

◆ get_identity_matrix()

t_matrix get_identity_matrix ( void  )

This fucntion gets a new matrix with the special attribute of being an identity matrix or multiplicative identity. That is, if you multiply any matrix or tuple by the identity matrix, you get back the matrix or tuple you started with.

Returns
Returns a new instance of an identity matrix.

◆ get_minor()

double get_minor ( t_matrix  t,
size_t  row,
size_t  col 
)

This function gets the minor of a given matrix. To put it differently, the minor of an element at row i and column j is the determinant of the submatrix at (i,j).

Parameters
tReceives the matrix from which the minor will be calculated.
rowReceives the row value from which the minor will be calculated.
colReceives the column value from which the minor will be calculated.
Returns
Returns the minor of the matrix passed to the function as parameter.

◆ get_submatrix()

t_matrix get_submatrix ( t_matrix  t,
size_t  del_row,
size_t  del_col 
)

This functions gets the submatrix of a given matrix. That is, what is left when you delete a single row and column from a matrix. The submatrix of a 4x4 matrix is 3x3 and the submatrix of a 3x3 matrix is 2x2.

Parameters
tReceives the matrix from which the submatrix will be calculated.
del_rowReceives a size_t value which stands for the row to be deleted when calculating the submatrix.
del_colReceives a size_t value which stands for the column to be deleted when calculating the submatrix.
Returns
Returns the submatrix of the matrix passed to the function as parameter.

◆ inverse()

t_matrix inverse ( t_matrix  t)

This function get the inverse of a given matrix. That is, if you multiply some matrix A by another matrix B, producing C, you can multiply C by the inverse of B to get A again. To invert the matrix, this function employs a method known as cofactor expansion. Additionally, there some interesting properties of the inverse: 1) if you invert an identity matrix, the result is also an identity matrix. 2) when you multiply a matrix by its inverse, you get as result the identity matrix. 3) The inverse of the transpose of a matrix and the transpose of the inverse only commute if the matrix is symmetric or orthogonal. Otherwise, the result of the two operations will always be different.

Parameters
tReceives the matrix to be inverted.
Returns
Returns a new matrix which is the inversion of the matrix passed to the function as parameter.

◆ is_invertible()

t_bool is_invertible ( t_matrix  t)

This function checks if a given matrix is invertible or not. Not every matrix is invertible. An matrix is invertible if its determinant is a non-zero number. If its determinant is 0, then the given matrix is not invertible.

Parameters
tReceives a matrix to be checked.
Returns
Returns TRUE if the given matrix is invertible. On the contrary, it returns FALSE if it's not invertible.

◆ multiply_mx_mx()

t_matrix multiply_mx_mx ( t_matrix  a,
t_matrix  b 
)

This function multiplies two matrices. "Mx" means matrix. For more information about how matrix multiplication works, refer to the book TRTC on page 28.

Parameters
aReceives the first matrix to be multiplied.
bReceives the second matrix to be multiplied.
Returns
Returns a new matrix which is the product of the two matrices passed to the function as parameter.

◆ multiply_tp_mx()

t_tuple multiply_tp_mx ( t_matrix  a,
t_tuple  b 
)

This function multiplies a matrix by a tuple. "Mx" means matrix and "tp" means tuple. For more information about how vector and matrix multiplication works, refer to the following link quoted by author J. Buck: https://betterexplained.com/articles/linear-algebra-guide/.

Parameters
aReceives the matrix to be multiplied.
bReceives the tuple to be multiplied.
Returns
Returns a new tuple which is the product of the two variables passed to the function as parameter.

◆ rotation_matrix()

t_matrix rotation_matrix ( t_vector  vector)

Calculates the rotation matrix for a given vector.

Parameters
vectorThe vector for which the rotation matrix will be calculated.
Returns
The rotation matrix for the given vector.

◆ rotation_x()

t_matrix rotation_x ( double  rad)

This function rotates a given matrix around the x-axis.

Parameters
radReceives the rotation around x axis to be performed in radians.
Returns
Returns a new rotated matrix after the rotation passed to the function as parameter.

◆ rotation_y()

t_matrix rotation_y ( double  rad)

This function rotates a given matrix around the y-axis.

Parameters
radReceives the rotation around y axis to be performed in radians.
Returns
Returns a new rotated matrix after the rotation passed to the function as parameter.

◆ rotation_z()

t_matrix rotation_z ( double  rad)

This function rotates a given matrix around the z-axis.

Parameters
radReceives the rotation around z axis to be performed in radians.
Returns
Returns a new rotated matrix after the rotation passed to the function as parameter.

◆ scaling()

t_matrix scaling ( double  x,
double  y,
double  z 
)

This function scales a given matrix by multiplication. When applied to an object centered at the origin, this transformation scales all points on the object, effectively making it larger (if the scale value is greater than 1) or smaller (if the scale value is less than 1). Note that reflection is scaling by a negative value.

Parameters
xReceives the x-axis value to scale.
yReceives the y-axis value to scale.
zReceives the z-axis value to scale.
Returns
Returns a new scaled matrix from the coordinates passed to the function as parameter.

◆ shearing()

t_matrix shearing ( t_shearing  x,
t_shearing  y,
t_shearing  z 
)

This function applies shearing (or skew) transformation to a given matrix. It has the effect of making straight lines slanted. Each coordinate is changed in proportion to the other two coordinates. When doing it to a matrix, it means that x will change in proportion to y and z, y will change in proportion to x and z and, lastly, z will change in proportion to x and y.

Parameters
xReceives a struct t_shearing storing the y and z values to change x in proportion to them.
yReceives a struct t_shearing storing the x and z values to change y in proportion to them.
zReceives a struct t_shearing storing the x and y values to change z in proportion to them.
Returns
Returns a new matrix after the shearing operation from values passed to the function as parameter.

◆ translation()

t_matrix translation ( double  x,
double  y,
double  z 
)

This function translates a given matrix. Translation is a transformation that moves a point. Thus, it's important to note that the inverse of a translation matrix is another translation matrix that moves points in reverse.

Parameters
xReceives the x-axis value of the translation.
yReceives the y-axis value of the translation.
zReceives the z-axis value of the translation.
Returns
Returns a new translated matrix from the coordinates passed to the function as parameter.

◆ transpose()

t_matrix transpose ( t_matrix  t)

This function transposes the matrix passed to the function as parameter. In other words, it turns the first row into the first column, the second row into the second column, and so forth. Thus, it's importante to remember that the transpose of the identity matrix always gives the own identity matrix as result.

Parameters
tReceives the matrix to be transposed.
Returns
Returns a new matrix which is the transposition of the matrix passed to the function as parameter.