KHtml

IntRect.cpp
1 /*
2  * Copyright (C) 2003, 2006 Apple Computer, 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 COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "IntRect.h"
27 
28 #include "FloatRect.h"
29 #include <algorithm>
30 
31 using std::max;
32 using std::min;
33 
34 namespace WebCore
35 {
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  }
84  if (isEmpty()) {
85  *this = other;
86  return;
87  }
88 
89  int l = min(x(), other.x());
90  int t = min(y(), other.y());
91  int r = max(right(), other.right());
92  int b = max(bottom(), other.bottom());
93 
94  m_location.setX(l);
95  m_location.setY(t);
96  m_size.setWidth(r - l);
97  m_size.setHeight(b - t);
98 }
99 
100 void IntRect::scale(float s)
101 {
102  m_location.setX((int)(x() * s));
103  m_location.setY((int)(y() * s));
104  m_size.setWidth((int)(width() * s));
105  m_size.setHeight((int)(height() * s));
106 }
107 
108 }
QTextStream & right(QTextStream &stream)
QTextStream & right(QTextStream &s)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Oct 26 2021 22:48:03 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.