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::isExpressibleAsIntRect() const
52 return isWithinIntRange(x()) && isWithinIntRange(y())
53 && isWithinIntRange(width()) && isWithinIntRange(height())
54 && isWithinIntRange(maxX()) && isWithinIntRange(maxY());
57 bool FloatRect::intersects(const FloatRect& other) const
59 // Checking emptiness handles negative widths as well as zero.
60 return !isEmpty() && !other.isEmpty()
61 && x() < other.maxX() && other.x() < maxX()
62 && y() < other.maxY() && other.y() < maxY();
65 bool FloatRect::contains(const FloatRect& other) const
67 return x() <= other.x() && maxX() >= other.maxX()
68 && y() <= other.y() && maxY() >= other.maxY();
71 void FloatRect::intersect(const FloatRect& other)
73 float l = max(x(), other.x());
74 float t = max(y(), other.y());
75 float r = min(maxX(), other.maxX());
76 float b = min(maxY(), other.maxY());
78 // Return a clean empty rectangle for non-intersecting cases.
79 if (l >= r || t >= b) {
86 setLocationAndSizeFromEdges(l, t, r, b);
89 void FloatRect::unite(const FloatRect& other)
91 // Handle empty special cases first.
99 float l = min(x(), other.x());
100 float t = min(y(), other.y());
101 float r = max(maxX(), other.maxX());
102 float b = max(maxY(), other.maxY());
104 setLocationAndSizeFromEdges(l, t, r, b);
107 void FloatRect::uniteIfNonZero(const FloatRect& other)
109 // Handle empty special cases first.
110 if (!other.width() && !other.height())
112 if (!width() && !height()) {
117 float left = min(x(), other.x());
118 float top = min(y(), other.y());
119 float right = max(maxX(), other.maxX());
120 float bottom = max(maxY(), other.maxY());
122 setLocationAndSizeFromEdges(left, top, right, bottom);
125 void FloatRect::scale(float sx, float sy)
127 m_location.setX(x() * sx);
128 m_location.setY(y() * sy);
129 m_size.setWidth(width() * sx);
130 m_size.setHeight(height() * sy);
133 void FloatRect::fitToPoints(const FloatPoint& p0, const FloatPoint& p1)
135 float left = min(p0.x(), p1.x());
136 float top = min(p0.y(), p1.y());
137 float right = max(p0.x(), p1.x());
138 float bottom = max(p0.y(), p1.y());
140 setLocationAndSizeFromEdges(left, top, right, bottom);
144 // Helpers for 3- and 4-way max and min.
146 template <typename T>
147 T min3(const T& v1, const T& v2, const T& v3)
149 return min(min(v1, v2), v3);
152 template <typename T>
153 T max3(const T& v1, const T& v2, const T& v3)
155 return max(max(v1, v2), v3);
158 template <typename T>
159 T min4(const T& v1, const T& v2, const T& v3, const T& v4)
161 return min(min(v1, v2), min(v3, v4));
164 template <typename T>
165 T max4(const T& v1, const T& v2, const T& v3, const T& v4)
167 return max(max(v1, v2), max(v3, v4));
170 } // anonymous namespace
172 void FloatRect::fitToPoints(const FloatPoint& p0, const FloatPoint& p1, const FloatPoint& p2)
174 float left = min3(p0.x(), p1.x(), p2.x());
175 float top = min3(p0.y(), p1.y(), p2.y());
176 float right = max3(p0.x(), p1.x(), p2.x());
177 float bottom = max3(p0.y(), p1.y(), p2.y());
179 setLocationAndSizeFromEdges(left, top, right, bottom);
182 void FloatRect::fitToPoints(const FloatPoint& p0, const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& p3)
184 float left = min4(p0.x(), p1.x(), p2.x(), p3.x());
185 float top = min4(p0.y(), p1.y(), p2.y(), p3.y());
186 float right = max4(p0.x(), p1.x(), p2.x(), p3.x());
187 float bottom = max4(p0.y(), p1.y(), p2.y(), p3.y());
189 setLocationAndSizeFromEdges(left, top, right, bottom);
192 IntRect enclosingIntRect(const FloatRect& rect)
194 float left = floorf(rect.x());
195 float top = floorf(rect.y());
196 float width = ceilf(rect.maxX()) - left;
197 float height = ceilf(rect.maxY()) - top;
199 return IntRect(clampToInteger(left), clampToInteger(top),
200 clampToInteger(width), clampToInteger(height));
203 FloatRect mapRect(const FloatRect& r, const FloatRect& srcRect, const FloatRect& destRect)
205 if (srcRect.width() == 0 || srcRect.height() == 0)
208 float widthScale = destRect.width() / srcRect.width();
209 float heightScale = destRect.height() / srcRect.height();
210 return FloatRect(destRect.x() + (r.x() - srcRect.x()) * widthScale,
211 destRect.y() + (r.y() - srcRect.y()) * heightScale,
212 r.width() * widthScale, r.height() * heightScale);