Vector.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // Copyright (c) 2009 Brandon Jones
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. //
  8. // Permission is granted to anyone to use this software for any purpose,
  9. // including commercial applications, and to alter it and redistribute it
  10. // freely, subject to the following restrictions:
  11. //
  12. // 1. The origin of this software must not be misrepresented; you must not
  13. // claim that you wrote the original software. If you use this software
  14. // in a product, an acknowledgment in the product documentation would be
  15. // appreciated but is not required.
  16. //
  17. // 2. Altered source versions must be plainly marked as such, and must not be
  18. // misrepresented as being the original software.
  19. //
  20. // 3. This notice may not be removed or altered from any source
  21. // distribution.
  22. //
  23. #include "Vector.h"
  24. #include <math.h>
  25. using namespace Sqrat;
  26. Vec2::Vec2( void ) : x(0.0f), y(0.0f) {};
  27. Vec2::Vec2( const Vec2 &v ) : x(v.x), y(v.y) {};
  28. Vec2::Vec2( const float vx, const float vy ) : x(vx), y(vy) {};
  29. bool Vec2::operator ==( const Vec2 &v ) const {
  30. return (x == v.x && y == v.y);
  31. }
  32. Vec2 Vec2::operator -( void ) const {
  33. return Vec2(-x, -y);
  34. }
  35. Vec2 Vec2::operator +( const Vec2& v ) const {
  36. return Vec2(x + v.x, y + v.y);
  37. }
  38. Vec2 Vec2::operator -( const Vec2& v ) const {
  39. return Vec2(x - v.x, y - v.y);
  40. }
  41. Vec2 Vec2::operator *( const float f ) const {
  42. return Vec2(x * f, y * f);
  43. }
  44. Vec2 Vec2::operator /( const float f ) const {
  45. return Vec2(x / f, y / f);
  46. }
  47. Vec2& Vec2::operator =( const Vec2& v ) {
  48. x = v.x;
  49. y = v.y;
  50. return *this;
  51. }
  52. float Vec2::Length( void ) const {
  53. return sqrt( (x * x) + (y * y) );
  54. }
  55. float Vec2::Distance( const Vec2 &v ) const {
  56. return sqrt( (x-v.x) * (x-v.x) + (y-v.y) * (y-v.y) );
  57. }
  58. Vec2& Vec2::Normalize( void ) {
  59. float invLen = 1.0f / Length();
  60. x *= invLen;
  61. y *= invLen;
  62. return *this;
  63. }
  64. float Vec2::Dot( const Vec2 &v ) const {
  65. return x*v.x + y*v.y;
  66. }