Changeset 7914 in orxonox.OLD for branches/gui/src/lib/math/rect2D.h
- Timestamp:
- May 28, 2006, 3:13:43 AM (18 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/gui/src/lib/math/rect2D.h
r7913 r7914 21 21 */ 22 22 23 #ifndef __ VECTOR2D_H_24 #define __ VECTOR2D_H_23 #ifndef __RECT2D_H_ 24 #define __RECT2D_H_ 25 25 26 #include < math.h>27 #include " compiler.h"26 #include <cmath> 27 #include "vector2D.h" 28 28 29 //! small and performant 2D vector 30 typedef float sVec2D[2]; 29 //! 2D Rectangle 2D 30 /** 31 * Class to handle 2-Dimensional Rectangles. 32 */ 33 class Rect2D 34 { 35 public: 36 Rect2D(float x, float y, float width, float height); 37 Rect2D(const Vector2D& topLeft, const Vector2D& bottomRight ); 38 Rect2D(const Rect2D& rect); 31 39 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; 40 43 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); 89 46 90 void debug() const; 47 Rect2D operator-(const Rect2D& rect) const; 48 Rect2D& operator-=(const Rect2D& rect); 91 49 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 120 private: 121 Vector2D _topLeft; 122 Vector2D _bottomRight; 95 123 }; 96 124 97 125 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_ */ 100 127 101 102 #endif /* __VECTOR2D_H_ */103
Note: See TracChangeset
for help on using the changeset viewer.