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