Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: output of EventListener Nicer

File size: 5.9 KB
RevLine 
[5420]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:
[6616]12   main-programmer: Benjamin Grauer
[5420]13   co-programmer: ...
14*/
15
[4578]16/*!
[6616]17 * @file vector2D.h
18 * A basic 2D Vector math framework
[5008]19 *
20 * Contains classes to handle vectors, lines, rotations and planes
[4578]21*/
[2043]22
[6615]23#ifndef __VECTOR2D_H_
24#define __VECTOR2D_H_
[2043]25
[7919]26#include <cmath>
[3860]27#include "compiler.h"
[2043]28
[5672]29//! small and performant 2D vector
30typedef float sVec2D[2];
31
[6615]32//! 2D Vector2D
[2043]33/**
[6615]34 *       Class to handle 2D Vector2Ds
35 */
36class Vector2D {
[4476]37 public:
[6615]38  Vector2D (float x, float y) : x(x), y(y) {}  //!< assignment constructor
39  Vector2D () : x(0), y(0) {}
[2043]40
[5052]41  /** @param v: the Vecor to compare with this one @returns true, if the Vecors are the same, false otherwise */
[7919]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); };
[6615]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; }
[4992]47  /** @param v The vector to add @returns the addition between two vectors (this + v) */
[6615]48  inline Vector2D operator+ (const Vector2D& v) const { return Vector2D(x + v.x, y + v.y); };
[4992]49  /** @param v The vector to add @returns the addition between two vectors (this + v) */
[6615]50  inline Vector2D operator+ (const sVec2D& v) const { return Vector2D(x + v[0], y + v[1]); };
[4836]51  /** @param v The vector to add  @returns the addition between two vectors (this += v) */
[6615]52  inline const Vector2D& operator+= (const Vector2D& v) { this->x += v.x; this->y += v.y; return *this; };
[4836]53  /** @param v The vector to substract  @returns the substraction between two vectors (this - v) */
[6615]54  inline const Vector2D& operator+= (const sVec2D& v) { this->x += v[0]; this->y += v[1]; return *this; };
[4836]55  /** @param v The vector to substract  @returns the substraction between two vectors (this - v) */
[6615]56  inline Vector2D operator- (const Vector2D& v) const { return Vector2D(x - v.x, y - v.y); }
[4836]57  /** @param v The vector to substract  @returns the substraction between two vectors (this - v) */
[6615]58  inline Vector2D operator- (const sVec2D& v) const { return Vector2D(x - v[0], y - v[1]); }
[4836]59  /** @param v The vector to substract  @returns the substraction between two vectors (this -= v) */
[6615]60  inline const Vector2D& operator-= (const Vector2D& v) { this->x -= v.x; this->y -= v.y; return *this; };
[4836]61  /** @param v The vector to substract  @returns the substraction between two vectors (this -= v) */
[6615]62  inline const Vector2D& operator-= (const sVec2D& v) { this->x -= v[0]; this->y -= v[1]; return *this; };
[4836]63  /** @param v the second vector  @returns The dotProduct between two vector (this (dot) v) */
[6615]64  inline float operator* (const Vector2D& v) const { return x * v.x + y * v.y; };
[4836]65  /** @todo strange */
[6615]66  inline const Vector2D& operator*= (const Vector2D& v) { this->x *= v.x; this->y *= v.y; return *this; };
[4836]67  /** @param f a factor to multiply the vector with @returns the vector multiplied by f (this * f) */
[6615]68  inline Vector2D operator* (float f) const { return Vector2D(x * f, y * f); };
[4836]69  /** @param f a factor to multiply the vector with @returns the vector multiplied by f (this *= f) */
[6615]70  inline const Vector2D& operator*= (float f) { this->x *= f; this->y *= f; return *this; };
[4836]71  /** @param f a factor to divide the vector with @returns the vector divided by f (this / f) */
[6615]72  inline Vector2D operator/ (float f) const { return (unlikely(f == 0.0)) ? Vector2D(0,0):Vector2D(this->x / f, this->y / f); };
[4836]73  /** @param f a factor to divide the vector with @returns the vector divided by f (this /= f) */
[6615]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; };
[4992]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 */
[6615]76  inline const Vector2D& operator= (const Vector2D& v) { this->x = v.x; this->y = v.y; return *this; };
[4992]77  /** copy constructor* @param v the sVec3D to assign to this vector. @returns the vector v */
[8148]78  inline const Vector2D& operator= (const sVec2D& v) { this->x = v[0]; this->y = v[1]; return *this; }
[4836]79  /** @param v: the other vector \return the dot product of the vectors */
[6615]80  float dot (const Vector2D& v) const { return x*v.x+y*v.y; };
[7919]81  /** @param v multipy each entry with each other @returns this reference */
[8148]82  const Vector2D& internalMultipy(const Vector2D& v) { this->x *= v.x; this->y *= y; return *this; };
[4992]83  /** scales the this vector with v* @param v the vector to scale this with */
[6615]84  void scale(const Vector2D& v) {   x *= v.x;  y *= v.y; };
[4836]85  /** @returns the length of the vector */
[6615]86  inline float len() const { return sqrt (x*x+y*y); }
[4992]87  /** normalizes the vector */
[6615]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();
[3541]91
[7919]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
[8035]95  /** @returns a Vector of (0,0) */
96  static const Vector2D& nullVector() { static Vector2D nullVector; return nullVector; };
97
[3966]98  void debug() const;
[4476]99
100 public:
[6615]101  float    x;     //!< The x Coordinate of the Vector2D.
102  float    y;     //!< The y Coordinate of the Vector2D.
[2043]103};
104
105
[6615]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)
[4476]108
[5008]109
[6615]110#endif /* __VECTOR2D_H_ */
[5052]111
Note: See TracBrowser for help on using the repository browser.