Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/math/vector2D.h @ 9649

Last change on this file since 9649 was 8148, checked in by bensch, 19 years ago

trunk: output of EventListener Nicer

File size: 5.9 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 <cmath>
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); };
43  /** @param v: the Vector to negative-compare with this one @returns true if the two vectors are different */
44  inline bool operator!= (const Vector2D& v) const { return (this->x!=v.x && this->y!=v.y); };
45  /** @param index The index of the "array" @returns the x/y coordinate */
46  inline float operator[] (float index) const { return ( index == 0)? this->x : this->y; }
47  /** @param v The vector to add @returns the addition between two vectors (this + v) */
48  inline Vector2D operator+ (const Vector2D& v) const { return Vector2D(x + v.x, y + v.y); };
49  /** @param v The vector to add @returns the addition between two vectors (this + v) */
50  inline Vector2D operator+ (const sVec2D& v) const { return Vector2D(x + v[0], y + v[1]); };
51  /** @param v The vector to add  @returns the addition between two vectors (this += v) */
52  inline const Vector2D& operator+= (const Vector2D& v) { this->x += v.x; this->y += v.y; return *this; };
53  /** @param v The vector to substract  @returns the substraction between two vectors (this - v) */
54  inline const Vector2D& operator+= (const sVec2D& v) { this->x += v[0]; this->y += v[1]; return *this; };
55  /** @param v The vector to substract  @returns the substraction between two vectors (this - v) */
56  inline Vector2D operator- (const Vector2D& v) const { return Vector2D(x - v.x, y - v.y); }
57  /** @param v The vector to substract  @returns the substraction between two vectors (this - v) */
58  inline Vector2D operator- (const sVec2D& v) const { return Vector2D(x - v[0], y - v[1]); }
59  /** @param v The vector to substract  @returns the substraction between two vectors (this -= v) */
60  inline const Vector2D& operator-= (const Vector2D& v) { this->x -= v.x; this->y -= v.y; return *this; };
61  /** @param v The vector to substract  @returns the substraction between two vectors (this -= v) */
62  inline const Vector2D& operator-= (const sVec2D& v) { this->x -= v[0]; this->y -= v[1]; return *this; };
63  /** @param v the second vector  @returns The dotProduct between two vector (this (dot) v) */
64  inline float operator* (const Vector2D& v) const { return x * v.x + y * v.y; };
65  /** @todo strange */
66  inline const Vector2D& operator*= (const Vector2D& v) { this->x *= v.x; this->y *= v.y; return *this; };
67  /** @param f a factor to multiply the vector with @returns the vector multiplied by f (this * f) */
68  inline Vector2D operator* (float f) const { return Vector2D(x * f, y * f); };
69  /** @param f a factor to multiply the vector with @returns the vector multiplied by f (this *= f) */
70  inline const Vector2D& operator*= (float f) { this->x *= f; this->y *= f; return *this; };
71  /** @param f a factor to divide the vector with @returns the vector divided by f (this / f) */
72  inline Vector2D operator/ (float f) const { return (unlikely(f == 0.0)) ? Vector2D(0,0):Vector2D(this->x / f, this->y / f); };
73  /** @param f a factor to divide the vector with @returns the vector divided by f (this /= f) */
74  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; };
75  /**  copy constructor @todo (i do not know it this is faster) @param v the vector to assign to this vector. @returns the vector v */
76  inline const Vector2D& operator= (const Vector2D& v) { this->x = v.x; this->y = v.y; return *this; };
77  /** copy constructor* @param v the sVec3D to assign to this vector. @returns the vector v */
78  inline const Vector2D& operator= (const sVec2D& v) { this->x = v[0]; this->y = v[1]; return *this; }
79  /** @param v: the other vector \return the dot product of the vectors */
80  float dot (const Vector2D& v) const { return x*v.x+y*v.y; };
81  /** @param v multipy each entry with each other @returns this reference */
82  const Vector2D& internalMultipy(const Vector2D& v) { this->x *= v.x; this->y *= y; return *this; };
83  /** scales the this vector with v* @param v the vector to scale this with */
84  void scale(const Vector2D& v) {   x *= v.x;  y *= v.y; };
85  /** @returns the length of the vector */
86  inline float len() const { return sqrt (x*x+y*y); }
87  /** normalizes the vector */
88  inline void normalize() { float l = len(); if( unlikely(l == 0.0))return; this->x=this->x/l; this->y=this->y/l; };
89  Vector2D getNormalized() const;
90  Vector2D abs();
91
92  /** @param v the Vector to slerp to @param val 0 = stay 1 = at v */
93  void slerp(const Vector2D& v, float val) { *this += (*this - v) * val; }
94
95  /** @returns a Vector of (0,0) */
96  static const Vector2D& nullVector() { static Vector2D nullVector; return nullVector; };
97
98  void debug() const;
99
100 public:
101  float    x;     //!< The x Coordinate of the Vector2D.
102  float    y;     //!< The y Coordinate of the Vector2D.
103};
104
105
106/** an easy way to create a Random Vector2D @param sideLength the length of the Vector2D (x not sqrt(x^2...)) */
107#define VECTOR2D_RAND(sideLength)  (Vector2D((float)rand()/RAND_MAX -.5, (float)rand()/RAND_MAX -.5) * sideLength)
108
109
110#endif /* __VECTOR2D_H_ */
111
Note: See TracBrowser for help on using the repository browser.