Design Discussion
-
After reading the OOD vs Procedural threads a couple days ago, I started thinking. I'm curious to see how some of the people on here would implement a simple library. Here are the basic requirements (use any language you like): *) Will hold values for a 3D vector of real numbers (maximum precision, Cartesian Coordinate system) *) Must be able to perform basic vector-only math (e.g. matrix math is not required) such as Dot-Product, Cross-Product, addition, subraction, normalization, magnitude, scalar multiplication, etc. *) Must be able to return vector values as a text-readable string I'll post my solution as a response to this (so it doesn't necessarily influence other replies).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
After reading the OOD vs Procedural threads a couple days ago, I started thinking. I'm curious to see how some of the people on here would implement a simple library. Here are the basic requirements (use any language you like): *) Will hold values for a 3D vector of real numbers (maximum precision, Cartesian Coordinate system) *) Must be able to perform basic vector-only math (e.g. matrix math is not required) such as Dot-Product, Cross-Product, addition, subraction, normalization, magnitude, scalar multiplication, etc. *) Must be able to return vector values as a text-readable string I'll post my solution as a response to this (so it doesn't necessarily influence other replies).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
My Solution:
// Vector3D.h #include <string> #include <sstream> #include <math.h> namespace VectorLibrary { class Vector3D { public: Vector3D() : _x(0.0), _y(0.0), _z(0.0) {} Vector3D(const Vector3D& r) : _x(r._x), _y(r._y), _z(r._z) {} Vector3D(double x, double y, double z) : _x(x), _y(y), _z(z) {} ~Vector3D() {} Vector3D& operator= (const Vector3D& r) { SetX(r.GetX()); SetY(r.GetY()); SetZ(r.GetZ()); return *this; } double GetX() const { return _x; } double GetY() const { return _y; } double GetZ() const { return _z; } void SetX(double x) { _x = x; } void SetY(double y) { _y = y; } void SetZ(double z) { _z = z; } double Magnitude() { return sqrt((_x * _x) + (_y * _y) + (_z * _z)); } void Normalize() { double mag = Magnitude(); if (mag > 0) { _x /= mag; _y /= mag; _z /= mag; } } std::string asString() { std::stringstream s; s << "(" << _x << ", " << _y << ", " << _z << ")"; return s.str(); } private: double _x; double _y; double _z; }; inline Vector3D ScalarMultiply(double scalar, const Vector3D& v) { Vector3D vTemp(scalar * v.GetX(), scalar * v.GetY(), scalar * v.GetZ()); return vTemp; } // for convienence inline Vector3D operator* (double scalar, const Vector3D& v) { return ScalarMultiply(scalar, v); } inline Vector3D operator* (const Vector3D& v, double scalar) { return ScalarMultiply(scalar, v); } inline Vector3D& operator*=(Vector3D& v, double scalar) { v = ScalarMultiply(v, scalar); return v; } inline Vector3D operator- (const Vector3D& l, const Vector3D& r) { Vector3D v(l.GetX() - r.GetX(), l.GetY() - r.GetY(), l.GetZ() - r.GetZ()); return v; } inline Vector3D& operator-=(Vector3D& l, const Vector3D& r) { Vector3D v = l - r; l = v; return l; } inline Vector3D operator+ (const Vector3D& l, const Vector3D& r) { Vector3D v(l.GetX() + r.GetX(), l.GetY() + r.GetY(), l.GetZ() + r.GetZ()); return v; } inline Vector3D& operator-=(Vector3D& l, const Vector3D& r) { Vector3D v = l + r; l = v; return l; } inline double DotProduct(const Vector3D& l, const Vector3D& r) { double ret = (l.GetX() * r.GetX()) + (l.GetY() * r.GetY()) + (l.GetZ() * r.GetZ()); return ret; } inline Vector3D CrossProduct(const Vector3D& l, const Vector3D& r) { Vector3D v( (l.GetY() * r.GetZ()) - (l.GetZ() * r.GetY()), (l.GetZ() * r.GetX()) - (l.GetX() * r.GetZ()), (l.GetX() * r.GetY()) - (l.GetY() *