49 lines
1.1 KiB
C++
49 lines
1.1 KiB
C++
#include "utils_vec.hpp"
|
|
|
|
vec3 vec3::operator+(const vec3 &other) const
|
|
{
|
|
return vec3(x + other.x, y + other.y, z + other.z);
|
|
}
|
|
|
|
vec3 vec3::operator-(const vec3 &other) const
|
|
{
|
|
return vec3(x - other.x, y - other.y, z - other.z);
|
|
}
|
|
|
|
vec3 vec3::operator*(float scalar) const
|
|
{
|
|
return vec3(x * scalar, y * scalar, z * scalar);
|
|
}
|
|
|
|
vec3 vec3::operator/(float scalar) const
|
|
{
|
|
if (scalar == 0)
|
|
{
|
|
fprintf(stderr, "Error: Division by zero in vec3::operator/.\n");
|
|
}
|
|
return vec3(x / scalar, y / scalar, z / scalar);
|
|
}
|
|
|
|
vec3 vec3::dot(const vec3 &other) const
|
|
{
|
|
return vec3(x * other.x, y * other.y, z * other.z);
|
|
}
|
|
|
|
vec3 vec3::cross(const vec3 &other) const
|
|
{
|
|
return vec3(
|
|
y * other.z - z * other.y,
|
|
z * other.x - x * other.z,
|
|
x * other.y - y * other.x);
|
|
}
|
|
|
|
vec3 vec3::normalize() const
|
|
{
|
|
float length = sqrt(x * x + y * y + z * z);
|
|
if (length == 0)
|
|
{
|
|
fprintf(stderr, "Error: Normalization of zero vector in vec3::normalize.\n");
|
|
return vec3(0, 0, 0); // Return zero vector if normalization fails
|
|
}
|
|
return vec3(x / length, y / length, z / length);
|
|
} |