Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/gui/src/lib/math/vector2D.h @ 7897

Last change on this file since 7897 was 7876, checked in by bensch, 19 years ago

fancy mouse-cursor :)

File size: 5.5 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 */
[7876]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 */
[6615]78  inline const Vector2D& operator= (const sVec2D& v) { this->x = v[0]; this->y = v[1]; }
[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; };
[4992]81  /** scales the this vector with v* @param v the vector to scale this with */
[6615]82  void scale(const Vector2D& v) {   x *= v.x;  y *= v.y; };
[4836]83  /** @returns the length of the vector */
[6615]84  inline float len() const { return sqrt (x*x+y*y); }
[4992]85  /** normalizes the vector */
[6615]86  inline void normalize() { float l = len(); if( unlikely(l == 0.0))return; this->x=this->x/l; this->y=this->y/l; };
87  Vector2D getNormalized() const;
88  Vector2D abs();
[3541]89
[3966]90  void debug() const;
[4476]91
92 public:
[6615]93  float    x;     //!< The x Coordinate of the Vector2D.
94  float    y;     //!< The y Coordinate of the Vector2D.
[2043]95};
96
97
[6615]98/** an easy way to create a Random Vector2D @param sideLength the length of the Vector2D (x not sqrt(x^2...)) */
99#define VECTOR2D_RAND(sideLength)  (Vector2D((float)rand()/RAND_MAX -.5, (float)rand()/RAND_MAX -.5) * sideLength)
[4476]100
[5008]101
[6615]102#endif /* __VECTOR2D_H_ */
[5052]103
Note: See TracBrowser for help on using the repository browser.