Created and tested vec3.
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
#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);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef UTILS_VEC_HPP
|
||||
#define UTILS_VEC_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <cmath>
|
||||
|
||||
class vec3
|
||||
{
|
||||
private:
|
||||
float x, y, z;
|
||||
|
||||
public:
|
||||
vec3() : x(0), y(0), z(0) {}
|
||||
vec3(float x, float y, float z) : x(x), y(y), z(z) {}
|
||||
|
||||
float getX() const { return x; }
|
||||
float getY() const { return y; }
|
||||
float getZ() const { return z; }
|
||||
|
||||
void setX(float val) { this->x = val; }
|
||||
void setY(float val) { this->y = val; }
|
||||
void setZ(float val) { this->z = val; }
|
||||
|
||||
vec3 operator+(const vec3 &other) const;
|
||||
vec3 operator-(const vec3 &other) const;
|
||||
vec3 operator*(float scalar) const;
|
||||
vec3 operator/(float scalar) const;
|
||||
|
||||
vec3 dot(const vec3 &other) const;
|
||||
vec3 cross(const vec3 &other) const;
|
||||
vec3 normalize() const;
|
||||
};
|
||||
|
||||
#endif // !UTILS_VEC_HPP
|
||||
Reference in New Issue
Block a user