1 // $Id: vector2d.hxx,v 1.8 2003/07/28 22:46:48 grumbel Exp $
3 // Construo - A wire-frame construction gamee
4 // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
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.
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
37 Vector2d (float x_
, float y_
)
42 void operator+= (const Vector2d
& vec
) {
48 void operator-= (const Vector2d
& vec
) {
54 void operator*= (float f
) {
60 Vector2d
operator+ (const Vector2d
& vec
) const {
61 return Vector2d(x
+ vec
.x
, y
+ vec
.y
);
65 float dot(const Vector2d
& vec
) const {
66 return (x
* vec
.x
) + (y
* vec
.y
);
70 Vector2d
operator- () const {
71 return Vector2d(-x
, -y
);
75 Vector2d
operator- (const Vector2d
& vec
) const {
76 return Vector2d(x
- vec
.x
, y
- vec
.y
);
80 Vector2d
operator* (float f
) const {
81 return Vector2d(x
* f
, y
* f
);
84 /** @return the length of the vector, also known as norm */
87 return sqrt (x
*x
+ y
*y
);
102 float distance(const Vector2d
& a
, const Vector2d
& b
) {
103 return (a
- b
).norm();
108 std::ostream
& operator << (std::ostream
& os
, const Vector2d
& v
)
110 return os
<< "[" << v
.x
<< ", " << v
.y
<< "]";