Concept Questions for HW01
September 30, 2024About 1 min
Concept Questions for HW01
Question 1
Mathematical Difference:
- vec4 operator*(const vec4 &v) const (Line 89): This multiplies a matrix (mat4) by a column vector (vec4), i.e., m * v. The matrix applies a transformation to the vector.
- vec4 operator*(const vec4 &v, const mat4 &m) (Line 106): This multiplies a row vector (vec4) by a matrix (mat4), i.e., v * m. This may result in a different transformation, as the multiplication order is reversed.
Explanation of const:
- const vec4 &v: The vector v is passed as a constant reference, meaning it cannot be modified inside the function.
- const mat4 &m: The matrix m is passed as a constant reference, ensuring it isn't modified.
- const after the function: Ensures that the matrix object calling the function is not modified.
Question 2
First function (line 25):
float operator[](unsigned int index) const;
This version can only be used when the
vec4
object is const, as it provides read-only access to the vector's elements.Situation:
If you have a
const vec4
object, you can only use this version to access its elements because the vector cannot be modified. For example:const vec4 v(1.0f, 2.0f, 3.0f, 4.0f); float x = v[0]; // Accesses the first element, but cannot modify it.
Second function (line 28):
float& operator[](unsigned int index);
This version can only be used with non-const
vec4
objects, as it provides modifiable access to the elements.Situation:
When you want to modify an element of a
vec4
, you use this version. For instance:vec4 v(1.0f, 2.0f, 3.0f, 4.0f); v[0] = 5.0f; // Modifies the first element of the vector.
In summary:
- The first function is used when reading values from a
const vec4
. - The second function is used when you need to modify elements in a non-const vec4.