Open
Graph Drawing
Framework

 v.2012.05
 

IntersectionRectangle.h
Go to the documentation of this file.
00001 /*
00002  * $Revision: 2299 $
00003  * 
00004  * last checkin:
00005  *   $Author: gutwenger $ 
00006  *   $Date: 2012-05-07 15:57:08 +0200 (Mon, 07 May 2012) $ 
00007  ***************************************************************/
00008  
00049 #ifdef _MSC_VER
00050 #pragma once
00051 #endif
00052 
00053 #ifndef OGDF_INTERSECTION_RECTANGLE_H
00054 #define OGDF_INTERSECTION_RECTANGLE_H
00055 
00056 
00057 #define RECT_CMP_EPS  1e-06
00058 
00059 
00060 #include <ogdf/basic/geometry.h>
00061 #include <math.h>
00062 
00063 
00064 namespace ogdf {
00065 
00066     
00067 class OGDF_EXPORT IntersectionRectangle {
00068 
00069 private:
00070 
00071     DPoint m_p1; // lower left Point
00072     DPoint m_p2; // upper right Point
00073     double m_area;
00074     DPoint m_center;
00075 
00076 public:
00077 
00078     // constructs zero area rectangle
00079     IntersectionRectangle() : m_p1(), m_p2(), m_area(0.0), m_center() {}
00080     //constructs rectangle with diagonal from p1 to p2
00081     IntersectionRectangle(const DPoint &p1, const DPoint &p2) : m_p1(p1), m_p2(p2) { init(); }
00082     //copy constructor
00083     IntersectionRectangle(const IntersectionRectangle &dr) :
00084         m_p1(dr.m_p1), m_p2(dr.m_p2), m_area(dr.m_area), m_center(dr.m_center){};
00085     //constructs rectangle with diagonal from (x1,y1) to (x2,y2)    
00086     IntersectionRectangle(double x1, double y1, double x2, double y2) {
00087         m_p1.m_x = x1; m_p1.m_y = y1; m_p2.m_x = x2; m_p2.m_y = y2;
00088         init();
00089     }
00090     //constructs rectangle with diagonal dl
00091     IntersectionRectangle(const DLine &dl) : m_p1(dl.start()), m_p2(dl.end())
00092         { init(); }
00093     // constructs a rectangle from the center point, width and height
00094     IntersectionRectangle(const DPoint &, double , double);
00095 
00096     // returns true if two rectangles have the same coordinates
00097     bool operator==(const IntersectionRectangle &dr) const {
00098         return m_p1 == dr.m_p1 && m_p2 == dr.m_p2;
00099     }
00100     // returns true if two rectangles have different coordinates
00101     bool operator!=(const IntersectionRectangle &dr) const {
00102         return !(*this == dr);
00103     }
00104 
00105     // assignment
00106     IntersectionRectangle &operator= (const IntersectionRectangle &dr) {
00107         if (this != &dr) { // don't assign myself
00108             m_p1 = dr.m_p1;
00109             m_p2 = dr.m_p2;
00110             m_center = dr.m_center;
00111             m_area = dr.m_area;
00112         }
00113         return *this;
00114     }
00115 
00116     // returns the width of the rectangle
00117     double width() const {
00118         return m_p2.m_x - m_p1.m_x;
00119     }
00120     //returns the height of the rectangle
00121     double height() const {
00122         return m_p2.m_y - m_p1.m_y;
00123     }
00124     //returns the center of the rectangle
00125     DPoint center() const { return m_center;}
00126     //returns the area of the rectangle
00127     double area() const { return m_area;}
00128 
00129 
00130     // returns rect-defining vertices
00131     const DPoint &p1() const { return m_p1; }
00132     const DPoint &p2() const { return m_p2; }
00133     // tests if p is inside the rectangle modulo the comparison epsilon
00134     bool inside(const DPoint &p) const {
00135         if ((p.m_x + RECT_CMP_EPS) < m_p1.m_x ||
00136             (p.m_x - RECT_CMP_EPS) > m_p2.m_x ||
00137             (p.m_y + RECT_CMP_EPS) < m_p1.m_y ||
00138             (p.m_y - RECT_CMP_EPS) > m_p2.m_y)
00139             return false;
00140         return true;
00141     }
00142     // tests if *this and the argument rectangle intersect
00143     bool intersects(const IntersectionRectangle &) const;
00144     // returns the rectangle resulting from intersection of this and argument.
00145     // Returns a rectangle with zero width and height and center (0,0) if intersection
00146     // is empty.
00147     IntersectionRectangle intersection(const IntersectionRectangle &) const;
00148     // computes distance between two rectangles
00149     double distance(const IntersectionRectangle &) const;
00150     //moves the rectangle such that its center is at the given point
00151     void move(const DPoint &);
00152 
00153 private:
00154     // makes sure, that m_p1 <= m_p2, default after construction, sets area and center
00155     void init();
00156     // swaps the two y-coordinates
00157     void yInvert() { swap(m_p1.m_y, m_p2.m_y); }
00158     // swaps the two x-coordinates
00159     void xInvert() { swap(m_p1.m_x, m_p2.m_x); }
00160     // functions for computing bounding lines
00161     DLine bottom() const { return DLine(m_p1.m_x,m_p1.m_y,m_p2.m_x,m_p1.m_y);}
00162     DLine top() const { return DLine(m_p1.m_x, m_p2.m_y, m_p2.m_x,m_p2.m_y);}
00163     DLine left() const { return DLine(m_p1.m_x, m_p1.m_y, m_p1.m_x, m_p2.m_y);}
00164     DLine right() const { return DLine(m_p2.m_x, m_p1.m_y, m_p2.m_x, m_p2.m_y);}
00165     // computes distance between parallel line segments
00166     double parallelDist(const DLine &, const DLine &) const;
00167     // computes distance between two points
00168     double pointDist(const DPoint &p1, const DPoint &p2) const {
00169         return sqrt((p1.m_y - p2.m_y) * (p1.m_y - p2.m_y) + (p1.m_x - p2.m_x) * (p1.m_x - p2.m_x));
00170     }
00171 friend ostream& operator<<(ostream &,const IntersectionRectangle &);
00172 };
00173 
00174 //the point comparer is needed for sorting points and storing them in
00175 //sorted sequences
00176 class PointComparer {
00177 public:
00178     static int compare(const DPoint &p1, const DPoint &p2) {
00179         if(p1.m_x > p2.m_x) return 1;
00180         if(p1.m_x < p2.m_x) return -1;
00181         if(p1.m_y > p2.m_y) return 1;
00182         if(p1.m_y < p2.m_y) return -1;
00183         return 0;
00184     }
00185     OGDF_AUGMENT_STATICCOMPARER(DPoint)
00186 };
00187 
00188 }
00189 #endif