?? geometry.h
字號:
{public: f_inline Sector() { }// f_inline Sector(const Rectangle &inOther) : _Left(inOther.Left()), _Top(inOther.Top()), _Right(inOther.Right()), _Bottom(inOther.Bottom()) { } f_inline Sector(Vector3f start, Vector3f direction, float radius, AngDeg alpha) :_start(start), _direction(direction), _radius(radius), _alpha(alpha) { } f_inline void Set(Vector3f start, Vector3f direction, float radius, AngDeg alpha) { _start = start; _direction = direction; _radius = radius; _alpha = alpha; } // member access f_inline Vector3f Start() const { return _start; } f_inline Vector3f Direction() const { return _direction; } f_inline float Radius() const { return _radius; } f_inline AngDeg Alpha() const { return _alpha; }/* f_inline void Normalize() { if (_Right < _Left) gSwap(_Left, _Right); if (_Bottom < _Top) gSwap(_Top, _Bottom); } f_inline void Widen(float inDelta) { _Left-=inDelta; _Top-=inDelta; _Right+=inDelta; _Bottom+=inDelta; } f_inline void Widen(float inDeltaWidth, float inDeltaHeight) { _Right+=inDeltaWidth; _Bottom+=inDeltaHeight; } f_inline void Widen(float inDeltaLeft, float inDeltaTop, float inDeltaRight, float inDeltaBottom) { _Left-=inDeltaLeft; _Top-=inDeltaTop; _Right+=inDeltaRight; _Bottom+=inDeltaBottom; } f_inline void Shrink(float inDelta) { _Left+=inDelta; _Top+=inDelta; _Right-=inDelta; _Bottom-=inDelta; } f_inline void Shrink(float inDeltaWidth, float inDeltaHeight) { _Right-=inDeltaWidth; _Bottom-=inDeltaHeight; } f_inline void Shrink(float inDeltaLeft, float inDeltaTop, float inDeltaRight, float inDeltaBottom) { _Left+=inDeltaLeft; _Top+=inDeltaTop; _Right-=inDeltaRight; _Bottom-=inDeltaBottom; } f_inline void Offset(float inDeltaX, float inDeltaY) { _Left+=inDeltaX; _Top+=inDeltaY; _Right+=inDeltaX; _Bottom+=inDeltaY; } f_inline bool Intersects(const Rectangle &b) const { return !(_Left > b._Right || _Right < b._Left || _Top > b._Bottom || _Bottom < b._Top); } // assignment f_inline Rectangle& operator=(const Rectangle &inOther) { _Left=inOther.Left(); _Top=inOther.Top(); _Right=inOther.Right(); _Bottom=inOther.Bottom(); return *this; } // comparison f_inline bool operator==(const Rectangle &inRHS) const { return (_Left==inRHS.Left()) && (_Top==inRHS.Top()) && (_Right==inRHS.Right()) && (_Bottom==inRHS.Bottom()); } f_inline bool operator!=(const Rectangle &inRHS) const { return (_Left!=inRHS.Left()) || (_Top!=inRHS.Top()) || (_Right!=inRHS.Right()) || (_Bottom!=inRHS.Bottom()); } f_inline bool operator[](const Vector3f &p) const { return ( p.x()<_Right && p.x()>_Left && p.y()<_Top && p.y()>_Bottom); }*/ f_inline Sector& operator=(const Sector &inOther) { _start=inOther.Start(); _direction=inOther.Direction(); _radius=inOther.Radius(); _alpha=inOther.Alpha(); return *this; } bool isInside(const Vector3f &point);private: Vector3f _start; Vector3f _direction; float _radius; AngDeg _alpha;};/*****************************************************************************//*********************** CLASS LINE2 ******************************************//*****************************************************************************//*!This class contains the representation of a line in 2D. A line is defined by the formula ay + bx + c = 0. The coefficients a, b and c are stored and used in the calculations. */template <typename T>class Line2{ // a line is defined by the formula: ay + bx + c = 0 T _a; /*!< This is the a coefficient in the line ay + bx + c = 0 */ T _b; /*!< This is the b coefficient in the line ay + bx + c = 0 */ T _c; /*!< This is the c coefficient in the line ay + bx + c = 0 */public: Line2( T a, T b, T c ){ _a=a; _b=b; _c=c; } T getACoefficient() const { return _a;} T getBCoefficient() const { return _b;} T getCCoefficient() const { return _c;} // print methods /*! This method prints the line information to the specified output stream. \param os output stream to which output is written. */ void show( ostream& os){ os << *this;} /*! This function prints the line to the specified output stream in the format y = ax + b. \param os output stream to which output is written \param l line that is written to output stream \return output sream to which output is appended. */ friend ostream& operator <<(ostream & os, Line2<T> l) { T a = l.getACoefficient(); T b = l.getBCoefficient(); T c = l.getCCoefficient(); // ay + bx + c = 0 -> y = -b/a x - c/a if( a == 0 ) os << "x = " << -c/b; else { os << "y = "; if( b != 0 ) os << -b/a << "x "; if( c > 0 ) os << "- " << fabs(c/a); else if( c < 0 ) os << "+ " << fabs(c/a); } return os; } /*! get intersection points with this line This method returns the intersection point between the current Line and the specified line. \param line line with which the intersection should be calculated. \return Vector2f position that is the intersection point. */ Vector2f getIntersection( Line2<T> line ) const { Vector2f pos(0,0); T x, y; if( ( _a / _b ) == (line.getACoefficient() / line.getBCoefficient() )) return pos; // lines are parallel, no intersection if( _a == 0 ) // bx + c = 0 and a2*y + b2*x + c2 = 0 ==> x = -c/b { // calculate x using the current line x = -_c/_b; // and calculate the y using the second line y = line.getYGivenX(x); } else if( line.getACoefficient() == 0 ) { // ay + bx + c = 0 and b2*x + c2 = 0 ==> x = -c2/b2 x = -line.getCCoefficient()/line.getBCoefficient(); // calculate x using y = getYGivenX(x); // 2nd line and calculate y using current line } // ay + bx + c = 0 and a2y + b2*x + c2 = 0 // y = (-b2/a2)x - c2/a2 // bx = -a*y - c => bx = -a*(-b2/a2)x -a*(-c2/a2) - c ==> // ==> a2*bx = a*b2*x + a*c2 - a2*c ==> x = (a*c2 - a2*c)/(a2*b - a*b2) // calculate x using the above formula and the y using the current line else { x = (_a*line.getCCoefficient() - line.getACoefficient()*_c)/ (line.getACoefficient()*_b - _a*line.getBCoefficient()); y = getYGivenX(x); } return Vector2f( x, y ); } /*! This method returns the closest point on a line to a given position. \param pos point to which closest point should be determined \return Vector2f closest point on line to 'pos'. */ Vector2f getPointOnLineClosestTo( const Vector2f &pos ) const { Line2<T> l2 = getTangentLine( pos ); // get tangent line return getIntersection( l2 ); // and intersection between the two lines } /*! This method returns the tangent line to a Vector2f. This is the line between the specified position and the closest point on the line to this position. \param pos Vector2f point with which tangent line is calculated. \return Line line tangent to this position */ Line2<T> getTangentLine( const Vector2f &pos ) const { // ay + bx + c = 0 -> y = (-b/a)x + (-c/a) // tangent: y = (a/b)*x + C1 -> by - ax + C2 = 0 => C2 = ax - by // with pos.y = y, pos.x = x return Line2<T>( _b, -_a, _a*pos.x() - _b*pos.y() ); } /*! This method returns the distance between a specified position and the closest point on the given line. \param pos position to which distance should be calculated \return double indicating the distance to the line. */ T getDistanceToPoint( const Vector2f &pos ) const { return (getPointOnLineClosestTo( pos )-pos).Length(); } /*! This method determines whether the projection of a point on the current line lies between two other points ('point1' and 'point2') that lie on the same line. \param pos point of which projection is checked. \param point1 first point on line \param point2 second point on line \return true when projection of 'pos' lies between 'pos1' and 'pos2'.*/ bool isInBetween( const Vector2f &pos, const Vector2f &pos1,const Vector2f &pos2) const { Vector2f posP = getPointOnLineClosestTo( pos ); // get closest point T dist = (pos1-pos2).Length(); // get distance between 2 pos // if the distance from both points to the projection is smaller than this // dist, the pos lies in between. return (posP-pos1).Length() <= dist && (posP-pos2).Length() <= dist; } /*! calculate associated variables in the line This method calculates the y coordinate given the x coordinate \param x coordinate \return y coordinate on this line */ T getYGivenX( T x ) const { if( _a == 0 ) { cerr << __FILE__<<__LINE__<<__FUNCTION__<<" Cannot calculate Y coordinate: "<< endl; return 0; } // ay + bx + c = 0 ==> ay = -(b*x + c)/a return -(_b*x+_c)/_a; } /*! This method calculates the x coordinate given the x coordinate \param y coordinate \return x coordinate on this line */ T getXGivenY( T y ) const { if( _b == 0 ) { cerr << __FILE__<<__LINE__<<__FUNCTION__<<"Cannot calculate X coordinate\n"<<endl; return 0; } // ay + bx + c = 0 ==> bx = -(a*y + c)/a return -(_a*y+_c)/_b; } /*! static methods to make a line using an easier representation. This method creates a line given two points. \param pos1 first point \param pos2 second point \return line that passes through the two specified points. */ static Line2<T> makeLineFromTwoPoints( const Vector2f &pos1, const Vector2f &pos2 ) { // 1*y + bx + c = 0 => y = -bx - c // with -b the direction coefficient (or slope) // and c = - y - bx T dA, dB, dC; T dTemp = pos2.x() - pos1.x(); // determine the slope if( fabs(dTemp) < EPSILON ) { // ay + bx + c = 0 with vertical slope=> a = 0, b = 1 dA = 0.0; dB = 1.0; } else { // y = (-b)x -c with -b the slope of the line dA = 1.0; dB = -(pos2.y() - pos1.y())/dTemp; } // ay + bx + c = 0 ==> c = -a*y - b*x dC = - dA*pos2.y() - dB * pos2.x(); return Line2<T>( dA, dB, dC ); } /*! This method creates a line given a position and an angle. \param vec position through which the line passes \param angle direction of the line. \return line that goes through position 'pos' with angle 'angle'. */ static Line2<T> makeLineFromPositionAndAngle( const Vector2f &pos, AngDeg angle ) { // calculate point somewhat further in direction 'angle' and make // line from these two points. return makeLineFromTwoPoints( pos, pos+projection(pol2xyz(Polar(1,angle,0)))); }};typedef Line2<float> Line2f;#endif /* _GEOMETRY_H */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -