Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7914 in orxonox.OLD for branches/gui/src/lib/math


Ignore:
Timestamp:
May 28, 2006, 3:13:43 AM (19 years ago)
Author:
bensch
Message:

orxonox/gui: simple almost finished implementation of a Rectangle Class, that should greatly improve work-flow in the GUI (most widgets are Rectangled :)

Location:
branches/gui/src/lib/math
Files:
2 edited
2 copied

Legend:

Unmodified
Added
Removed
  • branches/gui/src/lib/math/Makefile.am

    r7033 r7914  
    1313                plane.cc \
    1414                line.cc \
     15                rect2D.cc \
    1516                rotation_OBSOLETE.cc
    1617
     
    2324                plane.h \
    2425                line.h \
     26                rect2D.h \
    2527                rotation_OBSOLETE.h
  • branches/gui/src/lib/math/rect2D.cc

    r7913 r7914  
    1919#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_MATH
    2020
    21 #include "vector2D.h"
     21#include "rect2D.h"
    2222#ifdef DEBUG
    2323  #include "debug.h"
     
    2727#endif
    2828
    29 using namespace std;
    30 
    31 /////////////
    32 /* VECTORS */
    33 /////////////
    34 /**
    35  *  returns the this-vector normalized to length 1.0
    36  * @todo there is some error in this function, that i could not resolve. it just does not, what it is supposed to do.
    37  */
    38 Vector2D Vector2D::getNormalized() const
    39 {
    40   float l = this->len();
    41   if(unlikely(l == 1.0 || l == 0.0))
    42     return *this;
    43   else
    44     return (*this / l);
    45 }
    46 
    47 /**
    48  *  Vector2D is looking in the positive direction on all axes after this
    49 */
    50 Vector2D Vector2D::abs()
    51 {
    52   Vector2D v(fabs(x), fabs(y));
    53   return v;
    54 }
    55 
    56 
    57 
    58 /**
    59  *  Outputs the values of the Vector2D
    60  */
    61 void Vector2D::debug() const
    62 {
    63   PRINT(0)("x: %f; y: %f", x, y);
    64   PRINT(0)(" lenght: %f", len());
    65   PRINT(0)("\n");
    66 }
     29/**
     30 * @brief creates a new Rectangle with
     31 * @param x the left border
     32 * @param y the top border
     33 * @param width: the width
     34 * @param height the height
     35 */
     36Rect2D::Rect2D(float x, float y, float width, float height)
     37{
     38  this->setLeft(x), this->setTop(y);
     39  this->setSize(width, height);
     40}
     41
     42/**
     43 * @brief creates a new Rectangle with
     44 * @param topLeft the top-left corner
     45 * @param bottomRight the lower-right corner
     46 */
     47Rect2D::Rect2D(const Vector2D& topLeft, const Vector2D& bottomRight )
     48{
     49  this->setTopLeft(topLeft);
     50  this->setBottomRight(bottomRight);
     51}
     52
     53/**
     54 * @brief creates a new Rectangle with
     55 * @param rect the rectangle to copy.
     56 */
     57Rect2D::Rect2D(const Rect2D& rect)
     58{
     59  *this = rect;
     60}
     61
     62/**
     63 * @brief copies the Values of rect to this rect
     64 * @param rect copy the values from.
     65 * @returns this reference.
     66 */
     67Rect2D& Rect2D::operator=(const Rect2D& rect)
     68{
     69  this->_topLeft = rect._topLeft;
     70  this->_bottomRight = rect._bottomRight;
     71  return *this;
     72}
     73
     74bool Rect2D::operator==(const Rect2D& rect) const
     75{
     76  return (this->_topLeft == rect._topLeft &&
     77      this->_bottomRight == rect._bottomRight);
     78}
     79
     80bool Rect2D::operator!=(const Rect2D& rect) const
     81{
     82  return (this->_topLeft != rect._topLeft ||
     83      this->_bottomRight != rect._bottomRight);
     84}
     85
     86
     87Rect2D Rect2D::operator+(const Rect2D& rect) const
     88  {
     89    return Rect2D(this->_topLeft + rect._topLeft,
     90                  this->_bottomRight + rect._bottomRight);
     91  }
     92
     93
     94Rect2D& Rect2D::operator+=(const Rect2D& rect)
     95{
     96  this->_topLeft += rect._topLeft;
     97  this->_bottomRight += rect._bottomRight;
     98  return *this;
     99}
     100
     101
     102Rect2D Rect2D::operator-(const Rect2D& rect) const
     103{
     104  return Rect2D(this->_topLeft - rect._topLeft,
     105                this->_bottomRight - rect._bottomRight);
     106}
     107
     108Rect2D& Rect2D::operator-=(const Rect2D& rect)
     109{
     110  this->_topLeft -= rect._topLeft;
     111  this->_bottomRight -= rect._bottomRight;
     112  return *this;
     113}
     114
     115Rect2D Rect2D::operator&(const Rect2D& rect) const
     116  {
     117    return this->intersection(rect);
     118  }
     119
     120Rect2D& Rect2D::operator&=(const Rect2D& rect)
     121{
     122  *this = this->intersection(rect);
     123}
     124
     125Rect2D Rect2D::operator*(const Vector2D& scaling) const
     126{
     127#warning implements this...
     128  //this->scale(scaling);
     129}
     130
     131
     132void Rect2D::setWidth(float width)
     133{
     134  this->_bottomRight.x = this->_topLeft.x + width;
     135}
     136
     137
     138void Rect2D::setHeight(float height)
     139{
     140  this->_bottomRight.y = this->_topLeft.y + height;
     141}
     142
     143void Rect2D::setSize(float width, float height)
     144{
     145  this->_bottomRight = this->_topLeft + Vector2D(width, height);
     146}
     147
     148void Rect2D::setSize(const Vector2D& size)
     149{
     150  this->_bottomRight = this->_topLeft + size;
     151}
     152
     153void Rect2D::setCenter(const Vector2D& center)
     154{
     155  this->move(center - this->center());
     156}
     157
     158void Rect2D::scaleX(float x)
     159{
     160  this->_bottomRight.x = this->_topLeft.x + this->width() * x;
     161}
     162
     163void Rect2D::scaleY(float y)
     164{
     165  this->_bottomRight.y = this->_topLeft.y + this->height() * y;
     166}
     167
     168void Rect2D::scale(float x, float y)
     169{
     170  this->scale(Vector2D(x,y));
     171}
     172
     173void Rect2D::scale(const Vector2D& v)
     174{
     175  this->_bottomRight = this->_topLeft + this->size().internalMultipy(v);
     176
     177}
     178
     179void Rect2D::scaleCentered(float x, float y)
     180{
     181  this->scaleCentered(Vector2D(x, y));
     182}
     183
     184void Rect2D::scaleCentered(const Vector2D& v)
     185{
     186#warning implement this
     187}
     188
     189void Rect2D::moveX(float x)
     190{
     191  this->_topLeft.x += x;
     192  this->_bottomRight.x += x;
     193}
     194
     195void Rect2D::moveY(float y)
     196{
     197  this->_topLeft.y += y;
     198  this->_bottomRight.y += y;
     199}
     200void Rect2D::move(const Vector2D& v)
     201{
     202  this->_topLeft += v;
     203  this->_bottomRight += v;
     204}
     205
     206void Rect2D::swapTopBottom()
     207{
     208  float tmp = this->top();
     209  this->setTop(this->bottom());
     210  this->setBottom(tmp);
     211}
     212
     213void Rect2D::swapLeftRight()
     214{
     215  float tmp = this->left();
     216  this->setLeft(this->right());
     217  this->setRight(tmp);
     218}
     219
     220
     221void Rect2D::normalize()
     222{
     223  if (this->top() > this->bottom())
     224    this->swapTopBottom();
     225  if (this->left() > this->right())
     226    this->swapLeftRight();
     227}
     228
     229Rect2D Rect2D::normalized() const
     230{
     231  Rect2D norm(*this);
     232  norm.normalize();
     233  return norm;
     234}
     235
     236
     237Rect2D Rect2D::intersection(const Rect2D& intersector) const
     238{
     239#warning implement
     240}
     241
     242bool Rect2D::intersects(const Rect2D& intersector) const
     243  {
     244#warning implement
     245
     246  }
     247Rect2D Rect2D::unite(const Rect2D& rect)
     248  {
     249#warning implement
     250  }
     251bool Rect2D::contains(const Rect2D& rect) const
     252  {
     253    return (this->top() <= rect.top() &&
     254        this->bottom() >= rect.bottom() &&
     255        this->left() <= rect.left() &&
     256        this->right() >= rect.right());
     257  }
     258bool Rect2D::contains(const Vector2D& point) const
     259  {
     260    return (this->top() <= point.y &&
     261        this->bottom() >= point.y &&
     262        this->left() <= point.x &&
     263        this->right() >= point.x);
     264  }
     265
     266const Rect2D& Rect2D::slerp(const Rect2D& rect, float value)
     267{
     268  this->_topLeft.slerp(rect._topLeft, value);
     269  this->_bottomRight.slerp(rect._bottomRight, value);
     270
     271  return *this;
     272}
     273
  • branches/gui/src/lib/math/rect2D.h

    r7913 r7914  
    2121*/
    2222
    23 #ifndef __VECTOR2D_H_
    24 #define __VECTOR2D_H_
     23#ifndef __RECT2D_H_
     24#define __RECT2D_H_
    2525
    26 #include <math.h>
    27 #include "compiler.h"
     26#include <cmath>
     27#include "vector2D.h"
    2828
    29 //! small and performant 2D vector
    30 typedef float sVec2D[2];
     29//! 2D Rectangle 2D
     30/**
     31 *       Class to handle 2-Dimensional Rectangles.
     32 */
     33class Rect2D
     34{
     35public:
     36  Rect2D(float x, float y, float width, float height);
     37  Rect2D(const Vector2D& topLeft, const Vector2D& bottomRight );
     38  Rect2D(const Rect2D& rect);
    3139
    32 //! 2D Vector2D
    33 /**
    34  *       Class to handle 2D Vector2Ds
    35  */
    36 class Vector2D {
    37  public:
    38   Vector2D (float x, float y) : x(x), y(y) {}  //!< assignment constructor
    39   Vector2D () : x(0), y(0) {}
     40  Rect2D& operator=(const Rect2D& rect);
     41  bool operator==(const Rect2D& rect) const;
     42  bool operator!=(const Rect2D& rect) const;
    4043
    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]; }
    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   /** scales the this vector with v* @param v the vector to scale this with */
    82   void scale(const Vector2D& v) {   x *= v.x;  y *= v.y; };
    83   /** @returns the length of the vector */
    84   inline float len() const { return sqrt (x*x+y*y); }
    85   /** normalizes the vector */
    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();
     44  Rect2D operator+(const Rect2D& rect) const;
     45  Rect2D& operator+=(const Rect2D& rect);
    8946
    90   void debug() const;
     47  Rect2D operator-(const Rect2D& rect) const;
     48  Rect2D& operator-=(const Rect2D& rect);
    9149
    92  public:
    93   float    x;     //!< The x Coordinate of the Vector2D.
    94   float    y;     //!< The y Coordinate of the Vector2D.
     50  Rect2D operator&(const Rect2D& rect) const;
     51  Rect2D& operator&=(const Rect2D& rect);
     52
     53  Rect2D operator*(const Vector2D& scaling) const;
     54
     55
     56
     57  /** @returns the top of the Rectangle */
     58  inline float top() const { return _topLeft.y; };
     59  /** @returns the bottom of the Rectangle */
     60  inline float bottom() const { return _bottomRight.y; };
     61  /** @returns the left border of the Rectangle */
     62  inline float left() const { return _topLeft.x; };
     63  /** @returns the right border of the rectangle */
     64  inline float right() const { return _bottomRight.x; };
     65  /** @returns the Center of the Rectangle */
     66  inline Vector2D center() const { return (_topLeft+_bottomRight)/ 2.0; }
     67  /** @returns the width of the Rectangle */
     68  inline float width() const { return (right() - left()); };
     69  /** @returns the height of the rectangle */
     70  inline float height() const { return (bottom() - top()); };
     71  /** @returns the Size of the Rectangle */
     72  inline Vector2D size() const { return Vector2D(width(), height()); }
     73  /** @returns the area of the Rectangle */
     74  float area() const { return width()*height(); }
     75
     76  /** @param top sets the top border of the Rectangle */
     77  inline void setTop(float top) { _topLeft.y = top; };
     78  /** @param bottom the bottom border of the Rectangle */
     79  inline void setBottom(float bottom) { _bottomRight.y = bottom; };
     80  /** @param left the left border of the Rectangle */
     81  inline void setLeft(float left) { _topLeft.x = left; };
     82  /** @param right the right border of the Rectangle */
     83  inline void setRight(float right) { _bottomRight.x = right; };
     84  /** @param topLeft the top left corner of the Rectangle */
     85  void setTopLeft(const Vector2D& topLeft) { _topLeft = topLeft; };
     86  /** @param bottomRight the lower right corner of the Rectangle */
     87  void setBottomRight(const Vector2D& bottomRight) { _bottomRight = bottomRight; };
     88
     89  void setWidth(float width);
     90  void setHeight(float height);
     91  void setSize(float width, float height);
     92  void setSize(const Vector2D& size);
     93  void setCenter(const Vector2D& center);
     94
     95  void scaleX(float x);
     96  void scaleY(float y);
     97  void scale(float x, float y);
     98  void scale(const Vector2D& v);
     99  void scaleCentered(float x, float y);
     100  void scaleCentered(const Vector2D& v);
     101
     102  void moveX(float x);
     103  void moveY(float y);
     104  void move(const Vector2D& v);
     105
     106  void swapTopBottom();
     107  void swapLeftRight();
     108  void normalize();
     109  Rect2D normalized() const;
     110
     111
     112  Rect2D intersection(const Rect2D& intersector) const;
     113  bool intersects(const Rect2D& intersector) const;
     114  Rect2D unite(const Rect2D& rect);
     115  bool contains(const Rect2D& rect) const;
     116  bool contains(const Vector2D& point) const;
     117
     118  const Rect2D& slerp(const Rect2D& rect, float value);
     119
     120private:
     121  Vector2D _topLeft;
     122  Vector2D _bottomRight;
    95123};
    96124
    97125
    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)
     126#endif /* __RECT2D_H_ */
    100127
    101 
    102 #endif /* __VECTOR2D_H_ */
    103 
  • branches/gui/src/lib/math/vector2D.h

    r7876 r7914  
    7979  /** @param v: the other vector \return the dot product of the vectors */
    8080  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; };
    8183  /** scales the this vector with v* @param v the vector to scale this with */
    8284  void scale(const Vector2D& v) {   x *= v.x;  y *= v.y; };
     
    8789  Vector2D getNormalized() const;
    8890  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; }
    8994
    9095  void debug() const;
Note: See TracChangeset for help on using the changeset viewer.