Skip to content
Snippets Groups Projects
Commit 0349c1af authored by Alfred Burgess's avatar Alfred Burgess
Browse files

Added basic Vec2 class

parent 385d1204
No related merge requests found
......@@ -6,7 +6,8 @@
enum GenerationMethod
{
Random
Random,
Cave
};
#include "save-manager.h"
......@@ -15,5 +16,6 @@ enum GenerationMethod
#include "world-map-3d.h"
#include "chunk.h"
#include "logger.h"
#include "maths/maths.h"
#endif // MAIN_H
#ifndef __MATHEMATICS_INCLUDE_H__
#define __MATHEMATICS_INCLUDE_H__
#include "maths/vec2.h"
#endif // __MATHEMATICS_INCLUDE_H__
#ifndef __MATHS__VEC2_H__
#define __MATHS__VEC2_H__
#include <cmath>
class Vec2
{
public:
float x = 0, y = 0;
Vec2();
Vec2( float xIn, float yIn );
bool operator == ( const Vec2 rhs ) const;
Vec2 operator + ( const Vec2 rhs ) const;
Vec2 operator * ( const Vec2 rhs ) const;
Vec2 operator - ( const Vec2 rhs ) const;
Vec2 operator * ( const float scale ) const;
void operator += ( const Vec2 & rhs );
void operator -= ( const Vec2 & rhs );
void operator *= ( const Vec2 & rhs );
void operator *= ( const float rhs );
float length();
void normalise();
};
#endif // __MATHS__VEC2_H__
#include "maths/vec2.h"
Vec2::Vec2()
: Vec2( 0.0, 0.0 ) {}
Vec2::Vec2( float xIn, float yIn )
: x( xIn ), y( yIn )
{};
bool Vec2::operator == ( const Vec2 rhs ) const {
return (
abs( this->x - rhs.x ) < 0.01
) && (
abs( this->y - rhs.y ) < 0.01
);
}
Vec2 Vec2::operator + ( const Vec2 rhs ) const { return Vec2( this->x + rhs.x, this->y + rhs.y ); }
Vec2 Vec2::operator - ( const Vec2 rhs ) const { return Vec2( this->x - rhs.x, this->y - rhs.y ); }
Vec2 Vec2::operator * ( const Vec2 rhs ) const { return Vec2( this->x * rhs.x, this->y * rhs.y ); }
Vec2 Vec2::operator * ( const float scale ) const { return Vec2( this->x * scale, this->y * scale ); }
void Vec2::operator += ( const Vec2 & rhs ) { this->x += rhs.x; this->y += rhs.y; }
void Vec2::operator -= ( const Vec2 & rhs ) { this->x -= rhs.x; this->y -= rhs.y; }
void Vec2::operator *= ( const Vec2 & rhs ) { this->x *= rhs.x; this->y *= rhs.y; }
void Vec2::operator *= ( const float scale ) { this->x *= scale; this->y *= scale; }
float Vec2::length()
{ return sqrtf( this->x*this->x + this->y*this->y ); }
void Vec2::normalise()
{
float mag = this->length();
this->x /= mag;
this->y /= mag;
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment