Skip to content

Instantly share code, notes, and snippets.

@pingpoli
Last active September 9, 2020 20:21
Show Gist options
  • Save pingpoli/3b2a0bc470ad147a838d0822cda04108 to your computer and use it in GitHub Desktop.
Save pingpoli/3b2a0bc470ad147a838d0822cda04108 to your computer and use it in GitHub Desktop.
class vec3
{
public:
float x;
float y;
float z;
vec3( const float x = 0 , const float y = 0 , const float z = 0 ) : x(x) , y(y) , z(z) {}
// add another vector to a vector like a += v;
inline vec3 operator+=( const vec3& v )
{
this->x += v.x;
this->y += v.y;
this->z += v.z;
return *this;
}
};
// add two vectors together like vec3 c = a + b;
inline vec3 operator+( const vec3& a , const vec3& b )
{
return vec3( a.x+b.x , a.y+b.y , a.z+b.z );
}
// multiply a vector with a scalar like vec3 a = 3.5f * v; // this would need a second overload with the other order so vec3 a = v * 3.5f; would work as well, e.g. operator*( vec3 , float )
inline vec3 operator*( const float f , const vec3& v )
{
return vec3( f*v.x , f*v.y , f*v.z );
}
// print the vec3 directly using std::cout << vec3 << std::endl;
inline std::ostream& operator<<( std::ostream& os , const vec3& v )
{
os << "[ " << v.x << " , " << v.y << " , " << v.z << " ]";
return os;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment