2 * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2005 Nokia. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "FloatRect.h"
30 #include "FloatConversion.h"
34 #include <wtf/MathExtras.h>
41 FloatRect::FloatRect(const IntRect& r) : m_location(r.location()), m_size(r.size())
45 FloatRect FloatRect::narrowPrecision(double x, double y, double width, double height)
47 return FloatRect(narrowPrecisionToFloat(x), narrowPrecisionToFloat(y), narrowPrecisionToFloat(width), narrowPrecisionToFloat(height));
50 bool FloatRect::intersects(const FloatRect& other) const
52 // Checking emptiness handles negative widths as well as zero.
53 return !isEmpty() && !other.isEmpty()
54 && x() < other.maxX() && other.x() < maxX()
55 && y() < other.maxY() && other.y() < maxY();
58 bool FloatRect::contains(const FloatRect& other) const
60 return x() <= other.x() && maxX() >= other.maxX()
61 && y() <= other.y() && maxY() >= other.maxY();
64 void FloatRect::intersect(const FloatRect& other)
66 float l = max(x(), other.x());
67 float t = max(y(), other.y());
68 float r = min(maxX(), other.maxX());
69 float b = min(maxY(), other.maxY());
71 // Return a clean empty rectangle for non-intersecting cases.
72 if (l >= r || t >= b) {
79 setLocationAndSizeFromEdges(l, t, r, b);
82 void FloatRect::unite(const FloatRect& other)
84 // Handle empty special cases first.
92 float l = min(x(), other.x());
93 float t = min(y(), other.y());
94 float r = max(maxX(), other.maxX());
95 float b = max(maxY(), other.maxY());
97 setLocationAndSizeFromEdges(l, t, r, b);
100 void FloatRect::uniteIfNonZero(const FloatRect& other)
102 // Handle empty special cases first.
103 if (!other.width() && !other.height())
105 if (!width() && !height()) {
110 float left = min(x(), other.x());
111 float top = min(y(), other.y());
112 float right = max(maxX(), other.maxX());
113 float bottom = max(maxY(), other.maxY());
115 setLocationAndSizeFromEdges(left, top, right, bottom);
118 void FloatRect::scale(float sx, float sy)
120 m_location.setX(x() * sx);
121 m_location.setY(y() * sy);
122 m_size.setWidth(width() * sx);
123 m_size.setHeight(height() * sy);
126 void FloatRect::fitToPoints(const FloatPoint& p0, const FloatPoint& p1)
128 float left = min(p0.x(), p1.x());
129 float top = min(p0.y(), p1.y());
130 float right = max(p0.x(), p1.x());
131 float bottom = max(p0.y(), p1.y());
133 setLocationAndSizeFromEdges(left, top, right, bottom);
137 // Helpers for 3- and 4-way max and min.
139 template <typename T>
140 T min3(const T& v1, const T& v2, const T& v3)
142 return min(min(v1, v2), v3);
145 template <typename T>
146 T max3(const T& v1, const T& v2, const T& v3)
148 return max(max(v1, v2), v3);
151 template <typename T>
152 T min4(const T& v1, const T& v2, const T& v3, const T& v4)
154 return min(min(v1, v2), min(v3, v4));
157 template <typename T>
158 T max4(const T& v1, const T& v2, const T& v3, const T& v4)
160 return max(max(v1, v2), max(v3, v4));
163 } // anonymous namespace
165 void FloatRect::fitToPoints(const FloatPoint& p0, const FloatPoint& p1, const FloatPoint& p2)
167 float left = min3(p0.x(), p1.x(), p2.x());
168 float top = min3(p0.y(), p1.y(), p2.y());
169 float right = max3(p0.x(), p1.x(), p2.x());
170 float bottom = max3(p0.y(), p1.y(), p2.y());
172 setLocationAndSizeFromEdges(left, top, right, bottom);
175 void FloatRect::fitToPoints(const FloatPoint& p0, const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& p3)
177 float left = min4(p0.x(), p1.x(), p2.x(), p3.x());
178 float top = min4(p0.y(), p1.y(), p2.y(), p3.y());
179 float right = max4(p0.x(), p1.x(), p2.x(), p3.x());
180 float bottom = max4(p0.y(), p1.y(), p2.y(), p3.y());
182 setLocationAndSizeFromEdges(left, top, right, bottom);
185 static inline int safeFloatToInt(float x)
187 static const int s_intMax = std::numeric_limits<int>::max();
188 static const int s_intMin = std::numeric_limits<int>::min();
190 if (x >= static_cast<float>(s_intMax))
192 if (x < static_cast<float>(s_intMin))
194 return static_cast<int>(x);
197 IntRect enclosingIntRect(const FloatRect& rect)
199 float left = floorf(rect.x());
200 float top = floorf(rect.y());
201 float width = ceilf(rect.maxX()) - left;
202 float height = ceilf(rect.maxY()) - top;
204 return IntRect(safeFloatToInt(left), safeFloatToInt(top),
205 safeFloatToInt(width), safeFloatToInt(height));
208 FloatRect mapRect(const FloatRect& r, const FloatRect& srcRect, const FloatRect& destRect)
210 if (srcRect.width() == 0 || srcRect.height() == 0)
213 float widthScale = destRect.width() / srcRect.width();
214 float heightScale = destRect.height() / srcRect.height();
215 return FloatRect(destRect.x() + (r.x() - srcRect.x()) * widthScale,
216 destRect.y() + (r.y() - srcRect.y()) * heightScale,
217 r.width() * widthScale, r.height() * heightScale);