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

Go to the source code of this file.

Data Structures

struct  s_tuple
 Tuple means a list of ordered things. The struct t_tuple stores coordinates for a left-handed coordinate system, i.e, the thumb of your left hand points to x in the right direction, the fingers point to y and curling the fingers towards the palm point to z. More...
 
struct  s_color
 The struct s_color stores the color values of ray tracing. It contains a field for red, green and blue values. If all components are 1, the final color is white. Otherwise, if all of them are 0, the resulting color is black. Therefore, each field has a range between 0 and 1. More...
 

Typedefs

typedef struct s_tuple t_tuple
 Tuple means a list of ordered things. The struct t_tuple stores coordinates for a left-handed coordinate system, i.e, the thumb of your left hand points to x in the right direction, the fingers point to y and curling the fingers towards the palm point to z. More...
 
typedef t_tuple t_point
 The type t_point is a syntatic sugar for any structure t_tuple whose w's field equals to 1, i.e., the tuple is a point.
 
typedef t_tuple t_vector
 The type t_point is a syntatic sugar for any structure t_tuple whose w's field equals to 0, i.e., the tuple is a vector.
 
typedef struct s_color t_color
 The struct s_color stores the color values of ray tracing. It contains a field for red, green and blue values. If all components are 1, the final color is white. Otherwise, if all of them are 0, the resulting color is black. Therefore, each field has a range between 0 and 1. More...
 

Functions

t_tuple tuple (double x, double y, double z, double w)
 The function tuple() instances a new material of struct type t_tuple with the given values passed as parameter. More...
 
t_tuple point (double x, double y, double z)
 This function is a simplification of the function tuple(). It will create a point with the given coordinates passed as parameter and set the field w to 1. More...
 
t_tuple vector (double x, double y, double z)
 This function is a simplification of the function tuple(). It will create a point with the given coordinates passed as parameter and set the field w to 0. More...
 
t_tuple add (t_tuple a, t_tuple b)
 The add() function sums two tuples. A vector plus a point results in a point (w values: 1 + 0 = 1). A point plus a point results in a point (w values: 0 + 0 = 0). A tuple plus a tuple results in neither a vector or a point (w values: 1 + 1 = 2). More...
 
t_tuple subtract (t_tuple a, t_tuple b)
 The subtract() function subtract two tuples. A vector minus a point results in a point (w values: 1 - 0 = 1). A point minus a point results in a point (w values: 0 - 0 = 0). A tuple minus a tuple results in a tuple with a w of 0, which is another vector representing the change in direction between the two vectors. A point minus a vector results in a w of -1 (0 - 1 = 1), which is neither a point or a vector. When this case happens, it's necessary to use counterpart function negate(). More...
 
t_tuple negate (t_tuple a)
 The function negate() returns the opposite vector of a given vector passed as parameter. If a vector poins from a surface toward a light source, the opposite vector points from the light source back to the surface. More...
 
t_tuple multiply (t_tuple a, double scalar)
 The function multiply() multiplies a vector by a scalar value, i.e., it changes the vector length uniformly. More...
 
t_tuple divide (t_tuple a, double scalar)
 The function divide() divides a vector by a scalar value, i.e., it changes the vector length uniformly. More...
 
double magnitude (t_tuple vector)
 The function magnitude() gets the magnitude or length of a given vector passed as parameter. Vectors with magnitudes of 1 are called unit vectors or direction vectors. More...
 
t_tuple normalize (t_tuple vector)
 The function normalize() takes an arbitrary vector and converts it into a unit vector. It keeps the calculations anchored relative to a common scale, i.e., keeps all vectors speaking the same language. More...
 
double dot (t_tuple a, t_tuple b)
 The function dot() computes the sum of the products of the corresponding components of each vector. The smaller the dot product, the larger the angle between the vectors. A dot product of 1 means the vectors are identical and a dot product of -1 means they point in opposite directions. Thus, if the two vectors are unit vectors, the dot product is the cosine of them. More...
 
t_tuple cross (t_tuple a, t_tuple b)
 The function cross() multiplies vectors, instead of tuples. After all, the ray traces only needs a three-dimensional value anyway. The order of the operands matters and the result is a vector perpendicular to both of the original vectors. Changes to the operand order changes the direction of the resulting perpendicular vector. More...
 
t_tuple reflect (t_tuple in, t_tuple normal)
 This function returns the result of reflecting the in vector around the normal vector. More...
 
t_color new_color (double red, double green, double blue)
 The function new_color() instances a new color of struct type s_color with the given values passed as parameter. More...
 
t_color add_color (t_color a, t_color b)
 The function add_color() combines the intensity of each RGB color component of a and b. Adding (1, 0, 0) to (0, 1, 0) results in (1, 1, 0), i.e., adding red to green results in yellow. More...
 
t_color subtract_color (t_color a, t_color b)
 The function subtract_color() decreases the intensity of each RGB color component of a and b. Subtracting (1, 0, 0) to (0, 1, 0) results in (1, 0, 0), i.e., subtracting red from green results in red. More...
 
t_color multiply_color (t_color color, double scalar)
 The function multiply_color() scales the intensities of each RGB color component of the parameter color by a constant parameter scalar. For example, multiplying (1, 0, 0) by 0.5 or 1.5 result in (0.5, 0, 0) or (1.5, 0, 0). In other words, the red components were halved or doubled. More...
 
t_color hadamard_product (t_color a, t_color b)
 The function hadamard_product() blends two color together by multiplying corresponding components of each color. This process is known as Hadamard product or Schur product. For example, to view a yellow-green surface under reddish-purple light would result in a red-like color because the red component would be highest among the RGB values. More...
 

Typedef Documentation

◆ t_color

typedef struct s_color t_color

The struct s_color stores the color values of ray tracing. It contains a field for red, green and blue values. If all components are 1, the final color is white. Otherwise, if all of them are 0, the resulting color is black. Therefore, each field has a range between 0 and 1.

Parameters
redRepresents the red value of the RGB scale.
greenRepresents the green value of the RGB scale.
blueRepresents the blue value of the RGB scale.

◆ t_tuple

typedef struct s_tuple t_tuple

Tuple means a list of ordered things. The struct t_tuple stores coordinates for a left-handed coordinate system, i.e, the thumb of your left hand points to x in the right direction, the fingers point to y and curling the fingers towards the palm point to z.

Parameters
xRepresents the value of the x-axis coordinate.
yRepresents the value of the y-axis coordinate.
zRepresents the value of the z-axis coordinate.
wDistinguishes the t_tuple between a vector or a point. If w = 0, then the tuple is a vector. Otherwise, if w = 1, the tuple is a point.

Function Documentation

◆ add()

t_tuple add ( t_tuple  a,
t_tuple  b 
)

The add() function sums two tuples. A vector plus a point results in a point (w values: 1 + 0 = 1). A point plus a point results in a point (w values: 0 + 0 = 0). A tuple plus a tuple results in neither a vector or a point (w values: 1 + 1 = 2).

Parameters
aReceives the first tuple to sum.
bReceives the second tuple to sum.
Returns
Returns a new tuple by adding the corresponding components of each of the operands.

◆ add_color()

t_color add_color ( t_color  a,
t_color  b 
)

The function add_color() combines the intensity of each RGB color component of a and b. Adding (1, 0, 0) to (0, 1, 0) results in (1, 1, 0), i.e., adding red to green results in yellow.

Parameters
aReceives the first color to sum.
bReceives the second color to sum.
Returns
Returns a new color with combined intensity resultant of the sum of each RGB color component of a and b.

◆ cross()

t_tuple cross ( t_tuple  a,
t_tuple  b 
)

The function cross() multiplies vectors, instead of tuples. After all, the ray traces only needs a three-dimensional value anyway. The order of the operands matters and the result is a vector perpendicular to both of the original vectors. Changes to the operand order changes the direction of the resulting perpendicular vector.

Parameters
aReceives the first vector to be multiplied.
bReceives the second vector to be multiplied.
Returns
Returns a new vector which is perpendicular to both vectors passed as parameter to the function.

◆ divide()

t_tuple divide ( t_tuple  a,
double  scalar 
)

The function divide() divides a vector by a scalar value, i.e., it changes the vector length uniformly.

Parameters
aReceives the t_tuple vector to be scaled by division.
scalarReceives a double value to shrink the vector.
Returns
Returns a new vector with each component of the given tuple divided by the given scalar passed as parameter.

◆ dot()

double dot ( t_tuple  a,
t_tuple  b 
)

The function dot() computes the sum of the products of the corresponding components of each vector. The smaller the dot product, the larger the angle between the vectors. A dot product of 1 means the vectors are identical and a dot product of -1 means they point in opposite directions. Thus, if the two vectors are unit vectors, the dot product is the cosine of them.

Parameters
aReceives the first vector for the dot product.
bReceives the second vector for the dot product.
Returns
Returns a value which is the dot product that states the relationship between two vectors in terms of direction.

◆ hadamard_product()

t_color hadamard_product ( t_color  a,
t_color  b 
)

The function hadamard_product() blends two color together by multiplying corresponding components of each color. This process is known as Hadamard product or Schur product. For example, to view a yellow-green surface under reddish-purple light would result in a red-like color because the red component would be highest among the RGB values.

Parameters
aReceives the first color to be blended.
bReceives the second color to be blended.
Returns
Returns a new blended color from Schur product of a and b.

◆ magnitude()

double magnitude ( t_tuple  vector)

The function magnitude() gets the magnitude or length of a given vector passed as parameter. Vectors with magnitudes of 1 are called unit vectors or direction vectors.

Parameters
vectorReceives the vector to its magnitude value.
Returns
Returns the magnitude value of the length.

◆ multiply()

t_tuple multiply ( t_tuple  a,
double  scalar 
)

The function multiply() multiplies a vector by a scalar value, i.e., it changes the vector length uniformly.

Parameters
aReceives the t_tuple vector to be scaled by multiplication.
scalarReceives a double value to stretch out the vector.
Returns
Returns a new vector with each component of the given tuple multiplied by the given scalar passed as parameter.

◆ multiply_color()

t_color multiply_color ( t_color  color,
double  scalar 
)

The function multiply_color() scales the intensities of each RGB color component of the parameter color by a constant parameter scalar. For example, multiplying (1, 0, 0) by 0.5 or 1.5 result in (0.5, 0, 0) or (1.5, 0, 0). In other words, the red components were halved or doubled.

Parameters
colorReceives the color to be scaled.
scalarReceives the scale factor to be applied for each color value.
Returns
Returns a new color with halved or intensified RGB color values resulting from the scalar multiplication.

◆ negate()

t_tuple negate ( t_tuple  a)

The function negate() returns the opposite vector of a given vector passed as parameter. If a vector poins from a surface toward a light source, the opposite vector points from the light source back to the surface.

Parameters
aReceives the vector to be negated.
Returns
Returns the negated vector of the vector passed as parameter by doing vector a minus a vector with zero values.

◆ new_color()

t_color new_color ( double  red,
double  green,
double  blue 
)

The function new_color() instances a new color of struct type s_color with the given values passed as parameter.

Parameters
redReceives a red color value for the RGB scale.
greenReceives a green color value for the RGB scale.
blueReceives a blue color value for the RGB scale.
Returns
Returns an instance of a color with the values passed as parameter.

◆ normalize()

t_tuple normalize ( t_tuple  vector)

The function normalize() takes an arbitrary vector and converts it into a unit vector. It keeps the calculations anchored relative to a common scale, i.e., keeps all vectors speaking the same language.

Parameters
vectorReceives the vector to be normalized to a unit vector.
Returns
Returns a new vector that was normalized.

◆ point()

t_tuple point ( double  x,
double  y,
double  z 
)

This function is a simplification of the function tuple(). It will create a point with the given coordinates passed as parameter and set the field w to 1.

Parameters
xReceives a value for x-axis coordinate.
yReceives a value for y-axis coordinate.
zReceives a value for z-axis coordinate.
Returns
Returns an instance of a point with the given coordinates passed as parameter and w set to 1.

◆ reflect()

t_tuple reflect ( t_tuple  in,
t_tuple  normal 
)

This function returns the result of reflecting the in vector around the normal vector.

Parameters
inA struct of type t_tuple containing the vector to be reflected.
normalA struct of type t_tuple storing the normal vector to be used as reference for the reflection of the in vector.
Returns
Returns the reflected vector from in and out as reference orientation.

◆ subtract()

t_tuple subtract ( t_tuple  a,
t_tuple  b 
)

The subtract() function subtract two tuples. A vector minus a point results in a point (w values: 1 - 0 = 1). A point minus a point results in a point (w values: 0 - 0 = 0). A tuple minus a tuple results in a tuple with a w of 0, which is another vector representing the change in direction between the two vectors. A point minus a vector results in a w of -1 (0 - 1 = 1), which is neither a point or a vector. When this case happens, it's necessary to use counterpart function negate().

Parameters
aReceives the first tuple to subtract.
bReceives the second tuple to subtract.
Returns
Returns a new tuple by subtracting the corresponding components of each of the operands.

◆ subtract_color()

t_color subtract_color ( t_color  a,
t_color  b 
)

The function subtract_color() decreases the intensity of each RGB color component of a and b. Subtracting (1, 0, 0) to (0, 1, 0) results in (1, 0, 0), i.e., subtracting red from green results in red.

Parameters
aReceives the first color to decrease.
bReceives the second color to decrease.
Returns
Returns a new color with decreased intensity resultant of the subtraction of each RGB color component of a and b.

◆ tuple()

t_tuple tuple ( double  x,
double  y,
double  z,
double  w 
)

The function tuple() instances a new material of struct type t_tuple with the given values passed as parameter.

Parameters
xReceives a value for x-axis coordinate.
yReceives a value for y-axis coordinate.
zReceives a value for z-axis coordinate.
wReceives 1 if the tuple is point or 0 if the tuple is a vector.
Returns
Returns an instance of a tuple with the values passed as parameter.

◆ vector()

t_tuple vector ( double  x,
double  y,
double  z 
)

This function is a simplification of the function tuple(). It will create a point with the given coordinates passed as parameter and set the field w to 0.

Parameters
xReceives a value for x-axis coordinate.
yReceives a value for y-axis coordinate.
zReceives a value for z-axis coordinate.
Returns
Returns an instance of a point with the given coordinates passed as parameter and w set to 0.