73 lines
1.1 KiB
C++
73 lines
1.1 KiB
C++
#ifndef VEC3_H
|
|
#define VEC3_H
|
|
|
|
#include <math.h>
|
|
#include <stdio.h>
|
|
|
|
struct vec3 {
|
|
/* Members */
|
|
double x;
|
|
double y;
|
|
double z;
|
|
|
|
/* Constructors */
|
|
|
|
// Default
|
|
vec3() { x = 0; y = 0; z = 0; };
|
|
|
|
// Constructor proper. Values default to 0
|
|
vec3(double x = 0, double y = 0, double z = 0)
|
|
{
|
|
this.x = x;
|
|
this.y = y;
|
|
this.z = z;
|
|
}
|
|
|
|
/* Overriden operators */
|
|
|
|
// - operator. Not to be confused with substraction
|
|
vec3 operator-()
|
|
{
|
|
return vec3(-x, -y, -z);
|
|
}
|
|
|
|
// straightforward vector sum
|
|
vec3& operator+=(const vec3 &v)
|
|
{
|
|
this.x += v.x;
|
|
this.y += v.y;
|
|
this.z += v.z;
|
|
}
|
|
|
|
// scalar multiplication
|
|
vec3& operator*=(const double t)
|
|
{
|
|
x *= t;
|
|
y *= t;
|
|
z *= t;
|
|
return *this;
|
|
}
|
|
|
|
// division by a scalar t
|
|
vec3& operator/=(const double t)
|
|
{
|
|
x /= t;
|
|
y /= t;
|
|
z /= t;
|
|
return *this;
|
|
}
|
|
|
|
/* Methods */
|
|
|
|
double length()
|
|
{
|
|
std::sqrt(x*x + y*y + z*z);
|
|
}
|
|
};
|
|
|
|
/* Type aliases */
|
|
typedef point3 vec3;
|
|
typedef color vec3;
|
|
|
|
|
|
#endif
|