2 * Copyright (C) 2003, 2006, 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
29 #include "FractionalLayoutRect.h"
30 #include "FloatRect.h"
38 IntRect::IntRect(const FloatRect& r)
39 : m_location(IntPoint(static_cast<int>(r.x()), static_cast<int>(r.y())))
40 , m_size(IntSize(static_cast<int>(r.width()), static_cast<int>(r.height())))
44 IntRect::IntRect(const FractionalLayoutRect& r)
45 : m_location(flooredIntPoint(r.location()))
46 , m_size(flooredIntSize(r.size()))
50 bool IntRect::intersects(const IntRect& 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 IntRect::contains(const IntRect& other) const
60 return x() <= other.x() && maxX() >= other.maxX()
61 && y() <= other.y() && maxY() >= other.maxY();
64 void IntRect::intersect(const IntRect& other)
66 int l = max(x(), other.x());
67 int t = max(y(), other.y());
68 int r = min(maxX(), other.maxX());
69 int b = min(maxY(), other.maxY());
71 // Return a clean empty rectangle for non-intersecting cases.
72 if (l >= r || t >= b) {
81 m_size.setWidth(r - l);
82 m_size.setHeight(b - t);
85 void IntRect::unite(const IntRect& other)
87 // Handle empty special cases first.
95 int l = min(x(), other.x());
96 int t = min(y(), other.y());
97 int r = max(maxX(), other.maxX());
98 int b = max(maxY(), other.maxY());
102 m_size.setWidth(r - l);
103 m_size.setHeight(b - t);
106 void IntRect::uniteIfNonZero(const IntRect& other)
108 // Handle empty special cases first.
109 if (!other.width() && !other.height())
111 if (!width() && !height()) {
116 int left = min(x(), other.x());
117 int top = min(y(), other.y());
118 int right = max(maxX(), other.maxX());
119 int bottom = max(maxY(), other.maxY());
121 m_location.setX(left);
122 m_location.setY(top);
123 m_size.setWidth(right - left);
124 m_size.setHeight(bottom - top);
127 void IntRect::scale(float s)
129 m_location.setX((int)(x() * s));
130 m_location.setY((int)(y() * s));
131 m_size.setWidth((int)(width() * s));
132 m_size.setHeight((int)(height() * s));
135 static inline int distanceToInterval(int pos, int start, int end)
144 IntSize IntRect::differenceToPoint(const IntPoint& point) const
146 int xdistance = distanceToInterval(point.x(), x(), maxX());
147 int ydistance = distanceToInterval(point.y(), y(), maxY());
148 return IntSize(xdistance, ydistance);
151 IntRect unionRect(const Vector<IntRect>& rects)
155 size_t count = rects.size();
156 for (size_t i = 0; i < count; ++i)
157 result.unite(rects[i]);
162 } // namespace WebCore