Imported vanilla contents from upstream
[construo.git] / vector2d.hxx
blobcddf5c964f76a1cd8bc45cb0f537dbc5d2991305
1 // $Id: vector2d.hxx,v 1.8 2003/07/28 22:46:48 grumbel Exp $
2 //
3 // Construo - A wire-frame construction gamee
4 // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #ifndef HEADER_VECTOR2D_HXX
21 #define HEADER_VECTOR2D_HXX
23 #include <math.h>
24 #include <iostream>
26 /** */
27 class Vector2d
29 public:
30 float x;
31 float y;
33 Vector2d ()
34 : x(0), y(0)
37 Vector2d (float x_, float y_)
38 : x (x_), y (y_)
41 inline
42 void operator+= (const Vector2d& vec) {
43 x += vec.x;
44 y += vec.y;
47 inline
48 void operator-= (const Vector2d& vec) {
49 x -= vec.x;
50 y -= vec.y;
53 inline
54 void operator*= (float f) {
55 x *= f;
56 y *= f;
59 inline
60 Vector2d operator+ (const Vector2d& vec) const {
61 return Vector2d(x + vec.x, y + vec.y);
64 inline
65 float dot(const Vector2d& vec) const {
66 return (x * vec.x) + (y * vec.y);
69 inline
70 Vector2d operator- () const {
71 return Vector2d(-x, -y);
74 inline
75 Vector2d operator- (const Vector2d& vec) const {
76 return Vector2d(x - vec.x, y - vec.y);
79 inline
80 Vector2d operator* (float f) const {
81 return Vector2d(x * f, y * f);
84 /** @return the length of the vector, also known as norm */
85 inline
86 float norm() const {
87 return sqrt (x*x + y*y);
91 inline
92 void normalize() {
93 float f = norm();
94 if (f!=0)
96 x /= f;
97 y /= f;
101 static
102 float distance(const Vector2d& a, const Vector2d& b) {
103 return (a - b).norm();
107 inline
108 std::ostream& operator << (std::ostream& os, const Vector2d& v)
110 return os << "[" << v.x << ", " << v.y << "]";
113 #endif
115 /* EOF */