X-Git-Url: http://gitweb.pgornicz.com/gitweb.cgi?a=blobdiff_plain;f=src%2FVector2.cpp;h=2a8c8af0c2402dd7c12f1d4853fdf5acee0a45e5;hb=bcd46b8f4ee0d9b5ed6d983793f962ff66efa3aa;hp=4d4b85add9546abc4aabd8fb3f6a11bba420ed82;hpb=617dcc71d9a71663f63fb56ffac2505b45bf91b9;p=physics.git diff --git a/src/Vector2.cpp b/src/Vector2.cpp index 4d4b85a..2a8c8af 100644 --- a/src/Vector2.cpp +++ b/src/Vector2.cpp @@ -1,24 +1,34 @@ +/* + * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + #include "Vector2.h" +#include "debug.h" + #include "mathw.h" /// ***** Constructors/Destructors ***** Vector2::Vector2() - : x(0), y(0) -{ - -} -Vector2::Vector2(float x, float y) - : x(x), y(y) -{ - -} -Vector2::Vector2(const Vector2& vec) - : x(vec.x), y(vec.y) + : m_fX(0), m_fY(0) { } -Vector2::~Vector2() +Vector2::Vector2(float fX, float fY) + : m_fX(fX), m_fY(fY) { } @@ -27,67 +37,69 @@ Vector2::~Vector2() void Vector2::zero() { - x = 0; - y = 0; + m_fX = 0; + m_fY = 0; } void Vector2::unit() { - float len = length(); + float fLen = length(); - x /= len; - y /= len; + m_fX /= fLen; + m_fY /= fLen; } float Vector2::angle() const { //TODO - //return atan2A(y,x); + DASSERT(false); + //return atan2A(m_fY,m_fX); return 0; } float Vector2::length() const { - return sqrt(x*x + y*y); + return sqrt(sqrLength()); +} +float Vector2::sqrLength() const +{ + return this->dot(*this); +} + +float Vector2::dot(const Vector2& vec) const +{ + return m_fX * vec.m_fX + m_fY * vec.m_fY; } string Vector2::toString() const { // long just to be safe - char* strX = new char[50]; - char* strY = new char[50]; - - sprintf(strX, "%f", x); - sprintf(strY, "%f", y); - - string val = (string)"Vector2 x: " + strX + ", y: " + strY; + char rgchars[100]; - // deletes the allocated memory, not just what is used by sprintf - delete []strX; - delete []strY; + sprintf(rgchars, "Vector2 X: %f, Y: %f", m_fX, m_fY); - return val; + return rgchars; } void Vector2::print() const { - printf("%s\n",toString().c_str()); + printf("%s\n", toString().c_str()); } Vector2 Vector2::add(const Vector2& vec) const { - return Vector2(x+vec.x, y+vec.y); + return Vector2(m_fX + vec.m_fX, m_fY + vec.m_fY); } Vector2 Vector2::subtract(const Vector2& vec) const { - return Vector2(x-vec.x, y-vec.y); + return Vector2(m_fX - vec.m_fX, m_fY - vec.m_fY); } -Vector2 Vector2::multiply(float c) const +Vector2 Vector2::multiply(float f) const { - return Vector2(x*c, y*c); + return Vector2(m_fX * f, m_fY * f); } -Vector2 Vector2::divide(float c) const +Vector2 Vector2::divide(float f) const { - return Vector2(x/c, y/c); + return Vector2(m_fX / f, m_fY / f); } /// ***** Public Methods ***** @@ -100,37 +112,37 @@ Vector2 operator-(const Vector2& vec1, const Vector2& vec2) { return vec1.subtract(vec2); } -Vector2 operator*(float c, const Vector2& vec) +Vector2 operator*(float f, const Vector2& vec) { - return vec.multiply(c); + return vec.multiply(f); } -Vector2 operator*(const Vector2& vec, float c) +Vector2 operator*(const Vector2& vec, float f) { - return vec.multiply(c); + return vec.multiply(f); } -Vector2 operator/(const Vector2& vec, float c) +Vector2 operator/(const Vector2& vec, float f) { - return vec.divide(c); + return vec.divide(f); } void operator+=(Vector2& vec1, const Vector2& vec2) { - vec1.x += vec2.x; - vec1.y += vec2.y; + vec1.m_fX += vec2.m_fX; + vec1.m_fY += vec2.m_fY; } void operator-=(Vector2& vec1, const Vector2& vec2) { - vec1.x -= vec2.x; - vec1.y -= vec2.y; + vec1.m_fX -= vec2.m_fX; + vec1.m_fY -= vec2.m_fY; } -void operator*=(Vector2& vec, float c) +void operator*=(Vector2& vec, float f) { - vec.x *= c; - vec.y *= c; + vec.m_fX *= f; + vec.m_fY *= f; } -void operator/=(Vector2& vec, float c) +void operator/=(Vector2& vec, float f) { - vec.x /= c; - vec.y /= c; + vec.m_fX /= f; + vec.m_fY /= f; }