X-Git-Url: http://gitweb.pgornicz.com/gitweb.cgi?a=blobdiff_plain;f=src%2FVector2.cpp;h=d13ea958e4b080a904b74f69fa71b75e088396c2;hb=9ae1c0798cff2d1ed816bccb0723bd5a4ca97194;hp=8028aa3a06936b20f6704428411bb11eda5262d1;hpb=379cc454ab369431b3bdd2fe97cb2e6a1d68c26d;p=physics.git diff --git a/src/Vector2.cpp b/src/Vector2.cpp index 8028aa3..d13ea95 100644 --- a/src/Vector2.cpp +++ b/src/Vector2.cpp @@ -1,119 +1,162 @@ +/* + * 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 "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) { + +} +Vector2::~Vector2() +{ + } +/// ***** Public Class Methods ***** + void Vector2::zero() { - x = 0; - y = 0; + x = 0; + y = 0; } void Vector2::unit() { - float len = length(); + float len = length(); - x /= len; - y /= len; + x /= len; + y /= len; } float Vector2::angle() const { - //return atan2A(y,x); - return 0; + //TODO + //return atan2A(y,x); + return 0; } float Vector2::length() const { - return sqrt(x*x + y*y); + return sqrt(sqrLength()); +} +float Vector2::sqrLength() const +{ + return x*x + y*y; +} + +float Vector2::dot(const Vector2& v) const +{ + return x*v.x + y*v.y; } string Vector2::toString() const { - char* strX = new char[50]; // long just to be safe - char* strY = new char[50]; // long just to be safe - sprintf(strX, "%f", x); - sprintf(strY, "%f", y); + // 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; - string val = (string)"Vector2 x: " + strX + ", y: " + strY; - delete []strX; // deletes the memory allocated, not just what is used by sprintf - delete []strY; // deletes the memory allocated, not just what is used by sprintf + // deletes the allocated memory, not just what is used by sprintf + delete []strX; + delete []strY; - return val; + return val; } 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(x+vec.x, y+vec.y); } Vector2 Vector2::subtract(const Vector2& vec) const { - return Vector2(x-vec.x, y-vec.y); + return Vector2(x-vec.x, y-vec.y); } Vector2 Vector2::multiply(float c) const { - return Vector2(x*c, y*c); + return Vector2(x*c, y*c); } Vector2 Vector2::divide(float c) const { - return Vector2(x/c, y/c); + return Vector2(x/c, y/c); } +/// ***** Public Methods ***** Vector2 operator+(const Vector2& vec1, const Vector2& vec2) { - return vec1.add(vec2); + return vec1.add(vec2); } Vector2 operator-(const Vector2& vec1, const Vector2& vec2) { - return vec1.subtract(vec2); + return vec1.subtract(vec2); } Vector2 operator*(float c, const Vector2& vec) { - return vec.multiply(c); + return vec.multiply(c); } Vector2 operator*(const Vector2& vec, float c) { - return vec.multiply(c); + return vec.multiply(c); } Vector2 operator/(const Vector2& vec, float c) { - return vec.divide(c); + return vec.divide(c); } void operator+=(Vector2& vec1, const Vector2& vec2) { - vec1.x += vec2.x; - vec1.y += vec2.y; + vec1.x += vec2.x; + vec1.y += vec2.y; } void operator-=(Vector2& vec1, const Vector2& vec2) { - vec1.x -= vec2.x; - vec1.y -= vec2.y; + vec1.x -= vec2.x; + vec1.y -= vec2.y; } void operator*=(Vector2& vec, float c) { - vec.x *= c; - vec.y *= c; + vec.x *= c; + vec.y *= c; } void operator/=(Vector2& vec, float c) { - vec.x /= c; - vec.y /= c; + vec.x /= c; + vec.y /= c; }