First commit

This commit is contained in:
David 2021-08-20 23:27:59 +02:00
commit b75663feab
7 changed files with 348 additions and 0 deletions

73
vec3.h Normal file
View file

@ -0,0 +1,73 @@
#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