00001 //---------------------------------------------------------------------------- 00002 /** @file SgRect.cpp 00003 See SgRect.h 00004 */ 00005 //---------------------------------------------------------------------------- 00006 00007 #include "SgSystem.h" 00008 #include "SgRect.h" 00009 00010 #include <iostream> 00011 #include <limits> 00012 00013 using namespace std; 00014 00015 //---------------------------------------------------------------------------- 00016 00017 SgRect::SgRect() 00018 : m_left(numeric_limits<int>::max()), 00019 m_right(numeric_limits<int>::min()), 00020 m_top(numeric_limits<int>::max()), 00021 m_bottom(numeric_limits<int>::min()) 00022 { } 00023 00024 std::ostream& operator<<(std::ostream& stream, const SgRect& rect) 00025 { 00026 stream << "((" << rect.Left() << ',' << rect.Top() << ")," 00027 << '(' << rect.Right() << ',' << rect.Bottom() << "))"; 00028 return stream; 00029 } 00030 00031 void SgRect::Include(SgPoint p) 00032 { 00033 int x = SgPointUtil::Col(p); 00034 int y = SgPointUtil::Row(p); 00035 if (x < m_left) 00036 m_left = x; 00037 if (x > m_right) 00038 m_right = x; 00039 if (y < m_top) 00040 m_top = y; 00041 if (y > m_bottom) 00042 m_bottom = y; 00043 } 00044 00045 void SgRect::Include(const SgRect& rect) 00046 { 00047 if (rect.m_left < m_left) 00048 m_left = rect.m_left; 00049 if (rect.m_right > m_right) 00050 m_right = rect.m_right; 00051 if (rect.m_top < m_top) 00052 m_top = rect.m_top; 00053 if (rect.m_bottom > m_bottom) 00054 m_bottom = rect.m_bottom; 00055 } 00056 00057 void SgRect::Intersect(const SgRect& rect) 00058 { 00059 m_left = max(m_left, rect.m_left); 00060 m_right = min(m_right, rect.m_right); 00061 m_top = max(m_top, rect.m_top); 00062 m_bottom = min(m_bottom, rect.m_bottom); 00063 } 00064 00065 SgPoint SgRect::Center() const 00066 { 00067 SG_ASSERT(! IsEmpty()); 00068 return SgPointUtil::Pt((m_left + m_right) / 2, (m_top + m_bottom) / 2); 00069 } 00070 00071 bool SgRect::InRect(SgPoint p) const 00072 { 00073 //AR: if (BorderSet[p]) return false; 00074 int x = SgPointUtil::Col(p); 00075 int y = SgPointUtil::Row(p); 00076 return (x >= m_left) && (x <= m_right) && (y >= m_top) && (y <= m_bottom); 00077 } 00078 00079 bool SgRect::Contains(const SgRect& rect) const 00080 { 00081 return (m_left <= rect.m_left) && (m_right >= rect.m_right) 00082 && (m_top <= rect.m_top) && (m_bottom >= rect.m_bottom); 00083 } 00084 00085 bool SgRect::Overlaps(const SgRect& rect2) const 00086 { 00087 return (((m_left >= rect2.m_left) && (m_left <= rect2.m_right)) 00088 || ((rect2.m_left >= m_left) && (rect2.m_left <= m_right))) 00089 && (((m_top >= rect2.m_top) && (m_top <= rect2.m_bottom)) 00090 || ((rect2.m_top >= m_top) && (rect2.m_top <= m_bottom))); 00091 } 00092 00093 void SgRect::Expand(int margin) 00094 { 00095 m_left -= margin; 00096 m_right += margin; 00097 m_top -= margin; 00098 m_bottom += margin; 00099 } 00100 00101 //---------------------------------------------------------------------------- 00102