2D Vector Representation

Started by
11 comments, last by ScottC 17 years, 7 months ago
Quote:Original post by Verminox
Yes but I'm not making a console app. I needed the string to be stored in a buffer to be used later with SDL_ttf to display on the screen, and specifically the std::sprintf() to format the floating numbers to the precision required and return them in string form... unfortnuately I don't know a way to make use of std::sprintf() for std::string... so I had to use a char buffer :\
This can all be done using streaming and stream manipulators. There may be a small learning curve involved, but (IMO) if you're going to program in C++ you'll be much better off using C++ idioms and constructs and the C++ standard library.

Don't worry though, it's a transition many people (especially self-taught) have to make. You learn from enough outdated tutorials or dig through enough Quake engine source code, and you think everything has to be '#define this' and 'char* that'. Then you gain a little more experience and start to discover that there are better solutions available in C++. At least that's how it went for me (ah, the days of tracking down endless char*-related bugs :).
Advertisement
Haha you are right on that one jyk... I know a bit about streams but I'll look more closely into it. Nothing like a char* less source code. Thanks.
--------------------------------------Amaze your friends! Astound your family! Kennify your text!
Even though you solved the problem, take my vectors, there are more overloaded operators. Ignore the vector multiplication, I added that without thinking. :)

Also why does your variable class contain two floats with the exact same name?

//Vector.hclass vec2f{public:	vec2f();	vec2f(float x, float y);	float GetX() const;	float GetY() const;	void SetX(float x);	void SetY(float y);	vec2f operator-=(const vec2f &b);	vec2f operator+=(const vec2f &b);	vec2f operator*=(const vec2f &b);	vec2f operator/=(const vec2f &b);	vec2f operator*=(const float &b);	vec2f operator/=(const float &b);	vec2f operator*=(const double &b);	vec2f operator/=(const double &b);private:	float x, y;};vec2f operator-(const vec2f &a, const vec2f &b);vec2f operator+(const vec2f &a, const vec2f &b);vec2f operator*(const vec2f &a, const vec2f &b);vec2f operator/(const vec2f &a, const vec2f &b);vec2f operator*(const vec2f &a, const float &b);vec2f operator/(const vec2f &a, const float &b);vec2f operator*(const vec2f &a, const double &b);vec2f operator/(const vec2f &a, const double &b);vec2f operator*(const vec2f &a, const int &b);vec2f operator/(const vec2f &a, const int &b);//Vector.cppvec2f::vec2f(){	x = y = 0;}vec2f::vec2f(float x, float y){	this->x = x; this->y = y;}float vec2f::GetX() const{	return x;}float vec2f::GetY() const{	return y;}void vec2f::SetX(float x){ this->x = x; }void vec2f::SetY(float y){ this->y = y; }vec2f operator-(const vec2f &a, const vec2f &b){	return vec2f(a.GetX() - b.GetX(), a.GetY() - b.GetY());}vec2f operator+(const vec2f &a, const vec2f &b){	return vec2f(a.GetX() + b.GetX(), a.GetY() + b.GetY());}vec2f operator*(const vec2f &a, const vec2f &b){	return vec2f(a.GetX() * b.GetX(), a.GetY() * b.GetY());}vec2f operator/(const vec2f &a, const vec2f &b){	return vec2f(a.GetX() / b.GetX(), a.GetY() / b.GetY());}vec2f vec2f::operator-=(const vec2f &b){	this->SetX(this->GetX() - b.GetX()); this->SetY(this->GetY() - b.GetY());	return *this;}vec2f vec2f::operator+=(const vec2f &b){	this->SetX(this->GetX() + b.GetX()); this->SetY(this->GetY() + b.GetY());	return *this;}vec2f vec2f::operator*=(const vec2f &b){	this->SetX(this->GetX() * b.GetX()); this->SetY(this->GetY() * b.GetY());	return *this;}vec2f vec2f::operator/=(const vec2f &b){	this->SetX(this->GetX() / b.GetX()); this->SetY(this->GetY() / b.GetY());	return *this;}vec2f operator*(const vec2f &a, const float &b){	return vec2f(a.GetX() * b, a.GetY() * b);}vec2f operator/(const vec2f &a, const float &b){	return vec2f(a.GetX() / b, a.GetY() / b);}vec2f operator*(const vec2f &a, const double &b){	return vec2f(a.GetX() * b, a.GetY() * b);}vec2f operator/(const vec2f &a, const double &b){	return vec2f(a.GetX() / b, a.GetY() / b);}vec2f vec2f::operator*=(const float &b){	this->SetX(this->GetX() * b); this->SetY(this->GetY() * b);	return *this;}vec2f vec2f::operator/=(const float &b){	this->SetX(this->GetX() / b); this->SetY(this->GetY() / b);	return *this;}vec2f vec2f::operator*=(const double &b){	this->SetX(this->GetX() * b); this->SetY(this->GetY() * b);	return *this;}vec2f vec2f::operator/=(const double &b){	this->SetX(this->GetX() / b); this->SetY(this->GetY() / b);	return *this;}

This topic is closed to new replies.

Advertisement