KHTML
IntRect.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "config.h"
00027 #include "IntRect.h"
00028
00029 #include "FloatRect.h"
00030 #include <algorithm>
00031
00032 using std::max;
00033 using std::min;
00034
00035 namespace WebCore {
00036
00037 IntRect::IntRect(const FloatRect& r)
00038 : m_location(IntPoint(static_cast<int>(r.x()), static_cast<int>(r.y())))
00039 , m_size(IntSize(static_cast<int>(r.width()), static_cast<int>(r.height())))
00040 {
00041 }
00042
00043 bool IntRect::intersects(const IntRect& other) const
00044 {
00045
00046 return !isEmpty() && !other.isEmpty()
00047 && x() < other.right() && other.x() < right()
00048 && y() < other.bottom() && other.y() < bottom();
00049 }
00050
00051 bool IntRect::contains(const IntRect& other) const
00052 {
00053 return x() <= other.x() && right() >= other.right()
00054 && y() <= other.y() && bottom() >= other.bottom();
00055 }
00056
00057 void IntRect::intersect(const IntRect& other)
00058 {
00059 int l = max(x(), other.x());
00060 int t = max(y(), other.y());
00061 int r = min(right(), other.right());
00062 int b = min(bottom(), other.bottom());
00063
00064
00065 if (l >= r || t >= b) {
00066 l = 0;
00067 t = 0;
00068 r = 0;
00069 b = 0;
00070 }
00071
00072 m_location.setX(l);
00073 m_location.setY(t);
00074 m_size.setWidth(r - l);
00075 m_size.setHeight(b - t);
00076 }
00077
00078 void IntRect::unite(const IntRect& other)
00079 {
00080
00081 if (other.isEmpty())
00082 return;
00083 if (isEmpty()) {
00084 *this = other;
00085 return;
00086 }
00087
00088 int l = min(x(), other.x());
00089 int t = min(y(), other.y());
00090 int r = max(right(), other.right());
00091 int b = max(bottom(), other.bottom());
00092
00093 m_location.setX(l);
00094 m_location.setY(t);
00095 m_size.setWidth(r - l);
00096 m_size.setHeight(b - t);
00097 }
00098
00099 void IntRect::scale(float s)
00100 {
00101 m_location.setX((int)(x() * s));
00102 m_location.setY((int)(y() * s));
00103 m_size.setWidth((int)(width() * s));
00104 m_size.setHeight((int)(height() * s));
00105 }
00106
00107 }