I'll keep this short. I've never actually done professional C++. I don't really know any of the 'best practices'. I'd like to get some review on a simple class that I've made.
My Vector2d.h file:
#ifndef VECTOR2D_H
#define VECTOR2D_H
#include <cfloat>
#include <climits>
/*The Vector2d class is an object consisting of simply an x and
y value. Certain operators are overloaded to make it easier
for vector math to be performed.*/
class Vector2d {
public:
/*The x and y values are public to give easier access for
outside funtions. Accessors and mutators are not really
necessary*/
float x;
float y;
//Constructor assigns the inputs to x and y.
Vector2d();
Vector2d(float, float);
/*The following operators simply return Vector2ds that
have operations performed on the relative (x, y) values*/
Vector2d operator+(const Vector2d&) const;
Vector2d operator-(const Vector2d&) const;
Vector2d operator*(const Vector2d&) const;
Vector2d operator/(const Vector2d&) const;
//Check if the Vectors have the same values.
bool operator==(const Vector2d&) const;
/*Check which Vectors are closer or further from the
origin.*/
bool operator>(const Vector2d&) const;
bool operator<(const Vector2d&) const;
bool operator>=(const Vector2d&) const;
bool operator<=(const Vector2d&) const;
//Negate both the x and y values.
Vector2d operator-() const;
//Apply scalar operations.
Vector2d operator*(const float&) const;
Vector2d operator/(const float&) const;
//Product functions
static float DotProduct(const Vector2d&, const Vector2d&);
static float CrossProduct(const Vector2d&, const Vector2d&);
//Returns the length of the vector from the origin.
static float Magnitude(const Vector2d&);
//Return the unit vector of the input
static Vector2d Normal(const Vector2d&);
//Return a vector perpendicular to the left.
static Vector2d Perpendicular(const Vector2d&);
//Return true if two line segments intersect.
static bool Intersect(const Vector2d&, const Vector2d&, const Vector2d&, const Vector2d&);
//Return the point where two lines intersect.
static Vector2d GetIntersect(const Vector2d&, const Vector2d&, const Vector2d&, const Vector2d&);
};
#endif
And for my Vector2d.cpp file:
#include "Vector2d.h"
#include <cmath>
Vector2d::Vector2d()
{
x = 0.0;
y = 0.0;
}
Vector2d::Vector2d(float sourceX, float sourceY)
{
x = sourceX;
y = sourceY;
}
Vector2d Vector2d::operator+(const Vector2d &v) const
{
return Vector2d(x+v.x, y+v.y);
}
Vector2d Vector2d::operator-(const Vector2d &v) const
{
return Vector2d(x-v.x, y-v.y);
}
Vector2d Vector2d::operator*(const Vector2d &v) const
{
return Vector2d(x*v.x, y*v.y);
}
Vector2d Vector2d::operator/(const Vector2d &v) const
{
return Vector2d(x/v.x, y/v.y);
}
bool Vector2d::operator==(const Vector2d &v) const
{
return ((x == v.x) && (y == v.y));
}
bool Vector2d::operator>(const Vector2d &v) const
{
return (x*x + y*y) > (v.x*v.x + v.y*v.y);
}
bool Vector2d::operator<(const Vector2d &v) const
{
return (x*x + y*y) < (v.x*v.x + v.y*v.y);
}
bool Vector2d::operator>=(const Vector2d &v) const
{
return (x*x + y*y) > (v.x*v.x + v.y*v.y) ||
(x*x + y*y) == (v.x*v.x + v.y*v.y);
}
bool Vector2d::operator<=(const Vector2d &v) const
{
return (x*x + y*y) < (v.x*v.x + v.y*v.y) ||
(x*x + y*y) == (v.x*v.x + v.y*v.y);
}
Vector2d Vector2d::operator-() const
{
return Vector2d(-x, -y);
}
Vector2d Vector2d::operator*(const float& scalar) const
{
return Vector2d(x*scalar, y*scalar);
}
Vector2d Vector2d::operator/(const float& scalar) const
{
return Vector2d(x/scalar, y/scalar);
}
float Vector2d::DotProduct(const Vector2d &a, const Vector2d &b)
{
return ((a.x * b.x) + (a.y * b.y));
}
float Vector2d::CrossProduct(const Vector2d &a, const Vector2d &b)
{
return ((a.x * b.y) - (a.y * b.x));
}
float Vector2d::Magnitude(const Vector2d &v)
{
return sqrt((v.x * v.x) + (v.y * v.y));
}
Vector2d Vector2d::Normal(const Vector2d &v)
{
float magnitude = Magnitude(v);
return Vector2d(v.x / magnitude, v.y / magnitude);
}
Vector2d Vector2d::Perpendicular(const Vector2d &v)
{
return Vector2d(v.y, -v.x);
}
bool Vector2d::Intersect(const Vector2d &aa, const Vector2d &ab, const Vector2d &ba, const Vector2d &bb)
{
Vector2d p = aa;
Vector2d r = ab - aa;
Vector2d q = ba;
Vector2d s = bb - ba;
float t = CrossProduct((q - p), s) / CrossProduct(r, s);
float u = CrossProduct((q - p), r) / CrossProduct(r, s);
return (0.0 <= t && t <= 1.0) &&
(0.0 <= u && u <= 1.0);
}
Vector2d Vector2d::GetIntersect(const Vector2d &aa, const Vector2d &ab, const Vector2d &ba, const Vector2d &bb)
{
float pX = (aa.x*ab.y - aa.y*ab.x)*(ba.x - bb.x) -
(ba.x*bb.y - ba.y*bb.x)*(aa.x - ab.x);
float pY = (aa.x*ab.y - aa.y*ab.x)*(ba.y - bb.y) -
(ba.x*bb.y - ba.y*bb.x)*(aa.y - ab.y);
float denominator = (aa.x - ab.x)*(ba.y - bb.y) -
(aa.y - ab.y)*(ba.x - bb.x);
return Vector2d(pX / denominator, pY / denominator);
}
Thanks in advance!