KHTML
FloatRect.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
00027 #include "config.h"
00028 #include "FloatRect.h"
00029
00030 #include "FloatConversion.h"
00031 #include "IntRect.h"
00032 #include <algorithm>
00033
00034 using std::max;
00035 using std::min;
00036
00037 namespace WebCore {
00038
00039 FloatRect::FloatRect(const IntRect& r) : m_location(r.location()), m_size(r.size())
00040 {
00041 }
00042
00043 FloatRect FloatRect::narrowPrecision(double x, double y, double width, double height)
00044 {
00045 return FloatRect(narrowPrecisionToFloat(x), narrowPrecisionToFloat(y), narrowPrecisionToFloat(width), narrowPrecisionToFloat(height));
00046 }
00047
00048 bool FloatRect::intersects(const FloatRect& other) const
00049 {
00050
00051 return !isEmpty() && !other.isEmpty()
00052 && x() < other.right() && other.x() < right()
00053 && y() < other.bottom() && other.y() < bottom();
00054 }
00055
00056 bool FloatRect::contains(const FloatRect& other) const
00057 {
00058 return x() <= other.x() && right() >= other.right()
00059 && y() <= other.y() && bottom() >= other.bottom();
00060 }
00061
00062 void FloatRect::intersect(const FloatRect& other)
00063 {
00064 float l = max(x(), other.x());
00065 float t = max(y(), other.y());
00066 float r = min(right(), other.right());
00067 float b = min(bottom(), other.bottom());
00068
00069
00070 if (l >= r || t >= b) {
00071 l = 0;
00072 t = 0;
00073 r = 0;
00074 b = 0;
00075 }
00076
00077 m_location.setX(l);
00078 m_location.setY(t);
00079 m_size.setWidth(r - l);
00080 m_size.setHeight(b - t);
00081 }
00082
00083 void FloatRect::unite(const FloatRect& other)
00084 {
00085
00086 if (other.isEmpty())
00087 return;
00088 if (isEmpty()) {
00089 *this = other;
00090 return;
00091 }
00092
00093 float l = min(x(), other.x());
00094 float t = min(y(), other.y());
00095 float r = max(right(), other.right());
00096 float b = max(bottom(), other.bottom());
00097
00098 m_location.setX(l);
00099 m_location.setY(t);
00100 m_size.setWidth(r - l);
00101 m_size.setHeight(b - t);
00102 }
00103
00104 void FloatRect::scale(float s)
00105 {
00106 m_location.setX(x() * s);
00107 m_location.setY(y() * s);
00108 m_size.setWidth(width() * s);
00109 m_size.setHeight(height() * s);
00110 }
00111
00112 IntRect enclosingIntRect(const FloatRect& rect)
00113 {
00114 int l = static_cast<int>(rect.x());
00115 int t = static_cast<int>(rect.y());
00116
00117
00118
00119
00120 int r = static_cast<int>(rect.right() + 0.5f);
00121 int b = static_cast<int>(rect.bottom() + 0.5f);
00122 return IntRect(l, t, r - l, b - t);
00123 }
00124
00125 }