comparison ext/guichan-0.8.1/src/rectangle.cpp @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 /* _______ __ __ __ ______ __ __ _______ __ __
2 * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\
3 * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
4 * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / /
5 * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / /
6 * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
7 * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
8 *
9 * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
10 *
11 *
12 * Per Larsson a.k.a finalman
13 * Olof Naessén a.k.a jansem/yakslem
14 *
15 * Visit: http://guichan.sourceforge.net
16 *
17 * License: (BSD)
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in
25 * the documentation and/or other materials provided with the
26 * distribution.
27 * 3. Neither the name of Guichan nor the names of its contributors may
28 * be used to endorse or promote products derived from this software
29 * without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
37 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
38 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
39 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
40 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 /*
45 * For comments regarding functions please see the header file.
46 */
47
48 #include "guichan/rectangle.hpp"
49
50 namespace gcn
51 {
52 Rectangle::Rectangle()
53 : x(0),
54 y(0),
55 width(0),
56 height(0)
57 {
58 }
59
60 Rectangle::Rectangle(int x_, int y_, int width_, int height_)
61 : x(x_),
62 y(y_),
63 width(width_),
64 height(height_)
65 {
66 }
67
68 void Rectangle::setAll(int x_, int y_, int width_, int height_)
69 {
70 x = x_;
71 y = y_;
72 width = width_;
73 height = height_;
74 }
75
76 bool Rectangle::isIntersecting(const Rectangle& rectangle) const
77 {
78 int x_ = x;
79 int y_ = y;
80 int width_ = width;
81 int height_ = height;
82
83 x_ -= rectangle.x;
84 y_ -= rectangle.y;
85
86 if (x_ < 0)
87 {
88 width_ += x_;
89 x_ = 0;
90 }
91 else if (x_ + width_ > rectangle.width)
92 {
93 width_ = rectangle.width - x_;
94 }
95
96 if (y_ < 0)
97 {
98 height_ += y_;
99 y_ = 0;
100 }
101 else if (y_ + height_ > rectangle.height)
102 {
103 height_ = rectangle.height - y_;
104 }
105
106 if (width_ <= 0 || height_ <= 0)
107 {
108 return false;
109 }
110
111 return true;
112 }
113
114 bool Rectangle::isPointInRect(int x_, int y_) const
115 {
116 return x_ >= x
117 && y_ >= y
118 && x_ < x + width
119 && y_ < y + height;
120 }
121
122 std::ostream& operator<<(std::ostream& out,
123 const Rectangle& rectangle)
124 {
125 out << "Rectangle [x = " << rectangle.x
126 << ", y = " << rectangle.y
127 << ", width = " << rectangle.width
128 << ", height = " << rectangle.height
129 << "]";
130
131 return out;
132 }
133 }