1 /* 2 * Copyright (C) 2003, 2006, 2009 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 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. 12 * 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. 24 */ 25 26 #include "config.h" 27 #include "IntRect.h" 28 29 #include "FloatRect.h" 30 #include <algorithm> 31 32 using std::max; 33 using std::min; 34 35 namespace WebCore { 36 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()))) 40 { 41 } 42 43 bool IntRect::intersects(const IntRect& other) const 44 { 45 // Checking emptiness handles negative widths as well as zero. 46 return !isEmpty() && !other.isEmpty() 47 && x() < other.right() && other.x() < right() 48 && y() < other.bottom() && other.y() < bottom(); 49 } 50 51 bool IntRect::contains(const IntRect& other) const 52 { 53 return x() <= other.x() && right() >= other.right() 54 && y() <= other.y() && bottom() >= other.bottom(); 55 } 56 57 void IntRect::intersect(const IntRect& other) 58 { 59 int l = max(x(), other.x()); 60 int t = max(y(), other.y()); 61 int r = min(right(), other.right()); 62 int b = min(bottom(), other.bottom()); 63 64 // Return a clean empty rectangle for non-intersecting cases. 65 if (l >= r || t >= b) { 66 l = 0; 67 t = 0; 68 r = 0; 69 b = 0; 70 } 71 72 m_location.setX(l); 73 m_location.setY(t); 74 m_size.setWidth(r - l); 75 m_size.setHeight(b - t); 76 } 77 78 void IntRect::unite(const IntRect& other) 79 { 80 // Handle empty special cases first. 81 if (other.isEmpty()) 82 return; 83 if (isEmpty()) { 84 *this = other; 85 return; 86 } 87 88 int l = min(x(), other.x()); 89 int t = min(y(), other.y()); 90 int r = max(right(), other.right()); 91 int b = max(bottom(), other.bottom()); 92 93 m_location.setX(l); 94 m_location.setY(t); 95 m_size.setWidth(r - l); 96 m_size.setHeight(b - t); 97 } 98 99 void IntRect::scale(float s) 100 { 101 m_location.setX((int)(x() * s)); 102 m_location.setY((int)(y() * s)); 103 m_size.setWidth((int)(width() * s)); 104 m_size.setHeight((int)(height() * s)); 105 } 106 107 IntRect unionRect(const Vector<IntRect>& rects) 108 { 109 IntRect result; 110 111 size_t count = rects.size(); 112 for (size_t i = 0; i < count; ++i) 113 result.unite(rects[i]); 114 115 return result; 116 } 117 118 } // namespace WebCore 119