Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/shared_lib/src/lib/math/vector2D.h @ 8397

Last change on this file since 8397 was 6616, checked in by bensch, 19 years ago

orxonox/trunk: taken the quaternion outside of Vector.cc to quaternion.cc/h

File size: 5.3 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16/*!
17 * @file vector2D.h
18 * A basic 2D Vector math framework
19 *
20 * Contains classes to handle vectors, lines, rotations and planes
21*/
22
23#ifndef __VECTOR2D_H_
24#define __VECTOR2D_H_
25
26#include <math.h>
27#include "compiler.h"
28
29//! small and performant 2D vector
30typedef float sVec2D[2];
31
32//! 2D Vector2D
33/**
34 *       Class to handle 2D Vector2Ds
35 */
36class Vector2D {
37 public:
38  Vector2D (float x, float y) : x(x), y(y) {}  //!< assignment constructor
39  Vector2D () : x(0), y(0) {}
40
41  /** @param v: the Vecor to compare with this one @returns true, if the Vecors are the same, false otherwise */
42  inline bool operator== (const Vector2D& v) const { return (this->x==v.x && this->y==v.y)?true:false; };
43  /** @param index The index of the "array" @returns the x/y coordinate */
44  inline float operator[] (float index) const { return ( index == 0)? this->x : this->y; }
45  /** @param v The vector to add @returns the addition between two vectors (this + v) */
46  inline Vector2D operator+ (const Vector2D& v) const { return Vector2D(x + v.x, y + v.y); };
47  /** @param v The vector to add @returns the addition between two vectors (this + v) */
48  inline Vector2D operator+ (const sVec2D& v) const { return Vector2D(x + v[0], y + v[1]); };
49  /** @param v The vector to add  @returns the addition between two vectors (this += v) */
50  inline const Vector2D& operator+= (const Vector2D& v) { this->x += v.x; this->y += v.y; return *this; };
51  /** @param v The vector to substract  @returns the substraction between two vectors (this - v) */
52  inline const Vector2D& operator+= (const sVec2D& v) { this->x += v[0]; this->y += v[1]; return *this; };
53  /** @param v The vector to substract  @returns the substraction between two vectors (this - v) */
54  inline Vector2D operator- (const Vector2D& v) const { return Vector2D(x - v.x, y - v.y); }
55  /** @param v The vector to substract  @returns the substraction between two vectors (this - v) */
56  inline Vector2D operator- (const sVec2D& v) const { return Vector2D(x - v[0], y - v[1]); }
57  /** @param v The vector to substract  @returns the substraction between two vectors (this -= v) */
58  inline const Vector2D& operator-= (const Vector2D& v) { this->x -= v.x; this->y -= v.y; return *this; };
59  /** @param v The vector to substract  @returns the substraction between two vectors (this -= v) */
60  inline const Vector2D& operator-= (const sVec2D& v) { this->x -= v[0]; this->y -= v[1]; return *this; };
61  /** @param v the second vector  @returns The dotProduct between two vector (this (dot) v) */
62  inline float operator* (const Vector2D& v) const { return x * v.x + y * v.y; };
63  /** @todo strange */
64  inline const Vector2D& operator*= (const Vector2D& v) { this->x *= v.x; this->y *= v.y; return *this; };
65  /** @param f a factor to multiply the vector with @returns the vector multiplied by f (this * f) */
66  inline Vector2D operator* (float f) const { return Vector2D(x * f, y * f); };
67  /** @param f a factor to multiply the vector with @returns the vector multiplied by f (this *= f) */
68  inline const Vector2D& operator*= (float f) { this->x *= f; this->y *= f; return *this; };
69  /** @param f a factor to divide the vector with @returns the vector divided by f (this / f) */
70  inline Vector2D operator/ (float f) const { return (unlikely(f == 0.0)) ? Vector2D(0,0):Vector2D(this->x / f, this->y / f); };
71  /** @param f a factor to divide the vector with @returns the vector divided by f (this /= f) */
72  inline const Vector2D& operator/= (float f) {if (unlikely(f == 0.0)) {this->x=0;this->y=0;} else {this->x /= f; this->y /= f;} return *this; };
73  /**  copy constructor @todo (i do not know it this is faster) @param v the vector to assign to this vector. @returns the vector v */
74  inline const Vector2D& operator= (const Vector2D& v) { this->x = v.x; this->y = v.y; return *this; };
75  /** copy constructor* @param v the sVec3D to assign to this vector. @returns the vector v */
76  inline const Vector2D& operator= (const sVec2D& v) { this->x = v[0]; this->y = v[1]; }
77  /** @param v: the other vector \return the dot product of the vectors */
78  float dot (const Vector2D& v) const { return x*v.x+y*v.y; };
79  /** scales the this vector with v* @param v the vector to scale this with */
80  void scale(const Vector2D& v) {   x *= v.x;  y *= v.y; };
81  /** @returns the length of the vector */
82  inline float len() const { return sqrt (x*x+y*y); }
83  /** normalizes the vector */
84  inline void normalize() { float l = len(); if( unlikely(l == 0.0))return; this->x=this->x/l; this->y=this->y/l; };
85  Vector2D getNormalized() const;
86  Vector2D abs();
87
88  void debug() const;
89
90 public:
91  float    x;     //!< The x Coordinate of the Vector2D.
92  float    y;     //!< The y Coordinate of the Vector2D.
93};
94
95
96/** an easy way to create a Random Vector2D @param sideLength the length of the Vector2D (x not sqrt(x^2...)) */
97#define VECTOR2D_RAND(sideLength)  (Vector2D((float)rand()/RAND_MAX -.5, (float)rand()/RAND_MAX -.5) * sideLength)
98
99
100#endif /* __VECTOR2D_H_ */
101
Note: See TracBrowser for help on using the repository browser.