Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7644 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
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
26#include <math.h>
[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 */
[6615]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; }
[4992]45  /** @param v The vector to add @returns the addition between two vectors (this + v) */
[6615]46  inline Vector2D operator+ (const Vector2D& v) const { return Vector2D(x + v.x, y + v.y); };
[4992]47  /** @param v The vector to add @returns the addition between two vectors (this + v) */
[6615]48  inline Vector2D operator+ (const sVec2D& v) const { return Vector2D(x + v[0], y + v[1]); };
[4836]49  /** @param v The vector to add  @returns the addition between two vectors (this += v) */
[6615]50  inline const Vector2D& operator+= (const Vector2D& v) { this->x += v.x; this->y += v.y; return *this; };
[4836]51  /** @param v The vector to substract  @returns the substraction between two vectors (this - v) */
[6615]52  inline const Vector2D& operator+= (const sVec2D& v) { this->x += v[0]; this->y += v[1]; return *this; };
[4836]53  /** @param v The vector to substract  @returns the substraction between two vectors (this - v) */
[6615]54  inline Vector2D operator- (const Vector2D& v) const { return Vector2D(x - v.x, y - v.y); }
[4836]55  /** @param v The vector to substract  @returns the substraction between two vectors (this - v) */
[6615]56  inline Vector2D operator- (const sVec2D& v) const { return Vector2D(x - v[0], y - v[1]); }
[4836]57  /** @param v The vector to substract  @returns the substraction between two vectors (this -= v) */
[6615]58  inline const Vector2D& operator-= (const Vector2D& v) { this->x -= v.x; this->y -= v.y; return *this; };
[4836]59  /** @param v The vector to substract  @returns the substraction between two vectors (this -= v) */
[6615]60  inline const Vector2D& operator-= (const sVec2D& v) { this->x -= v[0]; this->y -= v[1]; return *this; };
[4836]61  /** @param v the second vector  @returns The dotProduct between two vector (this (dot) v) */
[6615]62  inline float operator* (const Vector2D& v) const { return x * v.x + y * v.y; };
[4836]63  /** @todo strange */
[6615]64  inline const Vector2D& operator*= (const Vector2D& v) { this->x *= v.x; this->y *= v.y; return *this; };
[4836]65  /** @param f a factor to multiply the vector with @returns the vector multiplied by f (this * f) */
[6615]66  inline Vector2D operator* (float f) const { return Vector2D(x * f, y * f); };
[4836]67  /** @param f a factor to multiply the vector with @returns the vector multiplied by f (this *= f) */
[6615]68  inline const Vector2D& operator*= (float f) { this->x *= f; this->y *= f; return *this; };
[4836]69  /** @param f a factor to divide the vector with @returns the vector divided by f (this / f) */
[6615]70  inline Vector2D operator/ (float f) const { return (unlikely(f == 0.0)) ? Vector2D(0,0):Vector2D(this->x / f, this->y / f); };
[4836]71  /** @param f a factor to divide the vector with @returns the vector divided by f (this /= f) */
[6615]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; };
[4992]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 */
[6615]74  inline const Vector2D& operator= (const Vector2D& v) { this->x = v.x; this->y = v.y; return *this; };
[4992]75  /** copy constructor* @param v the sVec3D to assign to this vector. @returns the vector v */
[6615]76  inline const Vector2D& operator= (const sVec2D& v) { this->x = v[0]; this->y = v[1]; }
[4836]77  /** @param v: the other vector \return the dot product of the vectors */
[6615]78  float dot (const Vector2D& v) const { return x*v.x+y*v.y; };
[4992]79  /** scales the this vector with v* @param v the vector to scale this with */
[6615]80  void scale(const Vector2D& v) {   x *= v.x;  y *= v.y; };
[4836]81  /** @returns the length of the vector */
[6615]82  inline float len() const { return sqrt (x*x+y*y); }
[4992]83  /** normalizes the vector */
[6615]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();
[3541]87
[3966]88  void debug() const;
[4476]89
90 public:
[6615]91  float    x;     //!< The x Coordinate of the Vector2D.
92  float    y;     //!< The y Coordinate of the Vector2D.
[2043]93};
94
95
[6615]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)
[4476]98
[5008]99
[6615]100#endif /* __VECTOR2D_H_ */
[5052]101
Note: See TracBrowser for help on using the repository browser.