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 "FloatRect.h"
37 IntRect::IntRect(const FloatRect& r)
38 : m_location(IntPoint(static_cast<int>(r.x()), static_cast<int>(r.y())))
39 , m_size(IntSize(static_cast<int>(r.width()), static_cast<int>(r.height())))
43 bool IntRect::intersects(const IntRect& other) const
45 // Checking emptiness handles negative widths as well as zero.
46 return !isEmpty() && !other.isEmpty()
47 && x() < other.maxX() && other.x() < maxX()
48 && y() < other.maxY() && other.y() < maxY();
51 bool IntRect::contains(const IntRect& other) const
53 return x() <= other.x() && maxX() >= other.maxX()
54 && y() <= other.y() && maxY() >= other.maxY();
57 void IntRect::intersect(const IntRect& other)
59 int l = max(x(), other.x());
60 int t = max(y(), other.y());
61 int r = min(maxX(), other.maxX());
62 int b = min(maxY(), other.maxY());
64 // Return a clean empty rectangle for non-intersecting cases.
65 if (l >= r || t >= b) {
74 m_size.setWidth(r - l);
75 m_size.setHeight(b - t);
78 void IntRect::unite(const IntRect& other)
80 // Handle empty special cases first.
88 int l = min(x(), other.x());
89 int t = min(y(), other.y());
90 int r = max(maxX(), other.maxX());
91 int b = max(maxY(), other.maxY());
95 m_size.setWidth(r - l);
96 m_size.setHeight(b - t);
99 void IntRect::uniteIfNonZero(const IntRect& other)
101 // Handle empty special cases first.
102 if (!other.width() && !other.height())
104 if (!width() && !height()) {
109 int left = min(x(), other.x());
110 int top = min(y(), other.y());
111 int right = max(maxX(), other.maxX());
112 int bottom = max(maxY(), other.maxY());
114 m_location.setX(left);
115 m_location.setY(top);
116 m_size.setWidth(right - left);
117 m_size.setHeight(bottom - top);
120 void IntRect::scale(float s)
122 m_location.setX((int)(x() * s));
123 m_location.setY((int)(y() * s));
124 m_size.setWidth((int)(width() * s));
125 m_size.setHeight((int)(height() * s));
128 IntRect unionRect(const Vector<IntRect>& rects)
132 size_t count = rects.size();
133 for (size_t i = 0; i < count; ++i)
134 result.unite(rects[i]);
139 } // namespace WebCore